Page 1 of 1

MySQL resource checking?

Posted: Sat Aug 18, 2007 5:27 pm
by maliskoleather
because of the way something is being handled (and unfortunatly, i cant change in a reasonable amount of time), i need to check and see if a variable contains a valid MySQL resource handler (a connection, specifically).

I have no idea where to go on this one... any suggestions?

i want/need to do somthing like this:

Code: Select all

if(validMySQLResource($handle)){
    $sql = "SELECT * FROM foo";
    $res = mysql_query($sql,$handle);
}

Posted: Sat Aug 18, 2007 6:16 pm
by RhapX
Tell me if this is what you're looking for: get_resource_type()

Posted: Sat Aug 18, 2007 7:46 pm
by feyd

Posted: Sun Aug 19, 2007 9:06 am
by superdezign
If you don't do anything with $handle between the connection and it's usage, is_resource() should do the job nicely.

Posted: Sun Aug 19, 2007 12:01 pm
by volka
Also take a look at mysql_stat()
e.g.

Code: Select all

<?php
$con = mysql_connect('localhost', 'localuser', 'localpass') or die(mysql_error());

echo "<pre>\n";
echo get_resource_type($con), "\n";
echo mysql_stat($con), "\n";
echo "--\n";
sleep(5);
echo get_resource_type($con), "\n";
echo mysql_stat($con), "\n";
echo "</pre>\n";
If I shut the mysql server down while the script is sleeping the output is
mysql link
Uptime: 1207 Threads: 1 Questions: 2 Slow queries: 0 Opens: 12 Flush tables: 1 Open tables: 3 Queries per second avg: 0.002
--
mysql link
MySQL server has gone away