By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,466 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,466 IT Pros & Developers. It's quick & easy.

Help: mysql_connect() doesn't seem to return resource link indentifier.

P: n/a
I have a small test script connecting to a MySQL database. It seems to
work, unless I try to use the resource link identifier returned by
mysql_connect();

This works and returns all the rows in the table:

<?php
include("../config.php");

$dbi = mysql_connect($dbhost,$dbuname,$dbpass);
mysql_select_db($dbname);

$res = mysql_query("SELECT * FROM temp_stories");

if($dbi)
echo "PASS<BR>";
else
echo "FAIL<BR>";

while($row = mysql_fetch_array($res)) {
echo $row['title']."<br/>";
}
?>
This does NOT work and returns no rows:

<?php
include("../config.php");

$dbi = mysql_connect($dbhost,$dbuname,$dbpass);
mysql_select_db($dbname);

$res = mysql_query("SELECT * FROM temp_stories",$dbi);

if($dbi)
echo "PASS<BR>";
else
echo "FAIL<BR>";

while($row = mysql_fetch_array($res)) {
echo $row['title']."<br/>";
}
?>
As you can see, the only change was including ",$dbi" in the mysql_query()
function.

Both scripts echo "FAIL" from the if($dbi) check, yet I do get the rows I
ask for when not including the resource link in mysql_query().

Any idea what's going on here? Everything worked fine before a server
upgrade (which I did not do and don't have the details on).

(Yes, I have tried including the resource link in the mysql_select_db()
function, but that too causes failure.)

No errors are echoed to the screen even using "or die(mysql_error())";

--
Sugapablo
http://www.sugapablo.net

Dec 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Wed, 14 Dec 2005 10:42:56 -0500, Sugapablo <ru**@REMOVEsugapablo.com> wrote:
I have a small test script connecting to a MySQL database. It seems to
work, unless I try to use the resource link identifier returned by
mysql_connect();

This works and returns all the rows in the table:

<?php
include("../config.php");

$dbi = mysql_connect($dbhost,$dbuname,$dbpass);
Add error handling at this point.

$dbi = mysql_connect($dbhost,$dbuname,$dbpass)
or die(mysql_error());

And call error_reporting(E_ALL) first and make sure display_errors is on - no
point turning away any help you can get.
mysql_select_db($dbname);

$res = mysql_query("SELECT * FROM temp_stories");

if($dbi)
echo "PASS<BR>";
else
echo "FAIL<BR>";
Why are you testing $dbi this late? You should also test $res here, but you
should have tested $dbi earlier.
$res = mysql_query("SELECT * FROM temp_stories",$dbi);

As you can see, the only change was including ",$dbi" in the mysql_query()
function.

Both scripts echo "FAIL" from the if($dbi) check, yet I do get the rows I
ask for when not including the resource link in mysql_query().
The manual has a possible clue:

http://uk.php.net/mysql_query
"link_identifier

The MySQL connection. If the link identifier is not specified, the last
link opened by mysql_connect() is assumed. If no such link is found, it will
try to create one as if mysql_connect() was called with no arguments. If by
chance no connection is found or established, an E_WARNING level warning is
generated."

Perhaps you have your MySQL database set up so that mysql_connect() with no
args actually succeeds. In which case the first version will succeed (link
identifier missing, so it auto-creates one), whereas the second one is being
passed "false" and so fails immediately?

Bit of a long shot - but could make sense if the default username and password
are set in php.ini, or even your database is wide open and allows null
usernames and passwords (can you even set MySQL up this way?), and it's the
only thing I can think of that's consistent with $dbi being false in BOTH cases
despite one of them actually returning data.
No errors are echoed to the screen even using "or die(mysql_error())";


Put this check on each and every mysql_ call, not just the mysql_query.
--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Dec 14 '05 #2

P: n/a
On Wed, 14 Dec 2005 19:12:46 +0000, Andy Hassall wrote:
Bit of a long shot - but could make sense


Solved.

The php script was actually failing on the mysql_connect() function, and
then going right ahead and selecting the database and going ahead with the
queries. It would accept any connection that did NOT try to connect with
a password.

That's why it returned a false resource link, and that's why it wuoldn't
query using that link but would query without it.

To fix it, I basically had to reiterate the passwords (enter them again)
into the mysql.user table. I have no idea why as the value in the table
was identical before and after, but it DID work. (Perhaps the privileges
were not flushed during the system upgrade?)
--
Sugapablo
http://www.sugapablo.net

Dec 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.