|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-01-27 22:38 UTC] joel dot a dot villarreal at gmail dot com
Description:
------------
When calling a function to connect to MySQL, while supplying a database name for mysql_select_db() through a variable defined at an include file, the database is not selected.
Reproduce code:
---------------
## connection.cfg.php says:
<?
$MySQL_DatabaseName = "foo";
?>
## main.php says:
<?
include("connection.cfg.php");
connect();
function connect() {
global $MySQL_DatabaseName;
mysql_connect("localhost", "root", "########");
mysql_select_db($MySQL_DatabaseName);
$row = mysql_fetch_array(mysql_query("SELECT * FROM footable WHERE ID = 1"), MYSQL_ASSOC) or die(mysql_error());
print_r($row);
}
?>
Expected result:
----------------
Expected result is to obtain a view of the row's data through the print_r() function.
Actual result:
--------------
mysql_error() prints: "No database selected".
PHP warns mysql_fetch_array() has been given an invalid MySQL result resource.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 10:00:02 2025 UTC |
I can't reproduce this. Here is my code: connection.cfg.php <? $MySQL_DatabaseName = "mysql"; ?> test.php <? include("connection.cfg.php"); connect(); function connect() { global $MySQL_DatabaseName; mysql_connect("localhost", "root", "########"); mysql_select_db($MySQL_DatabaseName); $row = mysql_fetch_array(mysql_query("SELECT * FROM user"), MYSQL_ASSOC) or die(mysql_error()); print_r($row); } ?> It prints out exactly as expected. I am using PHP 5.2.5 on RHEL5.