php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43947 mysql_select_db fails to select database using variables
Submitted: 2008-01-27 22:38 UTC Modified: 2008-03-10 20:14 UTC
Votes:3
Avg. Score:3.7 ± 1.9
Reproduced:1 of 2 (50.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: joel dot a dot villarreal at gmail dot com Assigned:
Status: Not a bug Package: MySQL related
PHP Version: 5.2.5 OS: Windows XP Service Pack 2
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: joel dot a dot villarreal at gmail dot com
New email:
PHP Version: OS:

 

 [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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-01-27 22:40 UTC] joel dot a dot villarreal at gmail dot com
Just in case, these are the versions of the server components being used at the moment:

PHP Version 5.2.5
MySQL 5.0.46
Apaache 2.2.6
 [2008-01-29 00:33 UTC] davidc@php.net
What do you get if you do:

echo $MySQL_DatabaseName . PHP_EOL;

right after your connect() ?
 [2008-01-29 17:28 UTC] joel dot a dot villarreal at gmail dot com
I've executed:

echo $MySQL_DatabaseName . PHP_EOL;

After calling to connect() and it prints out "foo", the value of $MySQL_DatabaseName.
 [2008-03-10 19:27 UTC] will dot fitch at gmail dot com
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.
 [2008-03-10 20:14 UTC] joel dot a dot villarreal at gmail dot com
I reckon, Will.

I've tried the code and now it works. I had to reinstall several system components to make it work properly. 

My apologies. I'm switching status to bogus.

Regards,
Joel A. Villarreal Bertoldi.
 [2008-03-10 20:14 UTC] joel dot a dot villarreal at gmail dot com
Thanks for your assistance.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 05:01:33 2024 UTC