MySQL resource checking?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

MySQL resource checking?

Post 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);
}
RhapX
Forum Commoner
Posts: 30
Joined: Mon Dec 05, 2005 5:24 pm
Location: Seattle, Washington

Post by RhapX »

Tell me if this is what you're looking for: get_resource_type()
Last edited by RhapX on Sat Aug 18, 2007 9:43 pm, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

If you don't do anything with $handle between the connection and it's usage, is_resource() should do the job nicely.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
Post Reply