Question on mysql_fetch_object()
| From: | Walter Wojcik | Date: | Sat, 23 Oct 2004 05:05:33 +0000 |
| Subject: | Question on mysql_fetch_object() | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-200141@lists.php.net to get a copy of this message | ||
I am trying to write a script that checks a table for a value in MySQL. It opens the table and
queries it fine but if my query returns nothing i have no way of testing to see if it returned
anything. I have tried testing for "false" and "" and ! $result in a if
statement but i don't know what to do. How do I check to see if i got anything or not without
getting this: "Notice: Trying to get property of non-object ". This is my most current
version of the code:
$id = session_id();
function logOut($session__id)
{
$link = mysql_connect( "localhost", "shopper", "shopper" );
if ( ! $link )
{
die("Couldn't connect to MySQL");
}
mysql_select_db( "shopingsystem" ) or die("Couldn't open Datatabase");
$query1 = "select * from userInfo WHERE session_id=\"$session__id\"";
$result = mysql_query( $query1, $link ); // query for username of person with this session id
if ( $result ) // Here i test for query failure but it does no good for empty but successful
results
{
if (! $result) // Here is where i want to test for empty result but it does no good!
// I have tried ($result == false), ($result == "") and (! $result)
// None do any good they all let an empty result pass.
{
print "Query failed for $session__id <br>";
}
print "Query successful for $session__id <br>";
$temp = mysql_fetch_object( $result );
$un = $temp->username; // Here is where i get "Notice: Trying to get property of
non-object" error!
$query2 = "UPDATE userinfo SET session_id='NULL', is_logged_on='F',
last_logon='NULL' WHERE username='$un'";
$result = mysql_query( $query2, $link );
if ( $result )
print "Logout successful for $un";
session_unset();
session_destroy();
}
else
print "Query Failed for $session__id";
mysql_close( $link );
}
Can you help me?
"Knowledge is power, Those who have it must share with those who don't"
-Walter Wojcik
---------------------------------
Do you Yahoo!?
vote.yahoo.com - Register online to vote today!