When I run my php file it outputs:
ERROR: Could not fetch results,
I do not understand why though, I used this exact code in other files and it works fine.
Code:
<?php
$mysql = mysql_connect('localhost','root','password') or die('ERROR: Could not connect, ' . mysql_error($mysql));
mysql_select_db('twp',$mysql) or die('ERROR: Could not connect, ' . mysql_error($mysql));
$q = mysql_query("SELECT * FROM DenCrypt_Users",$mysql) or die('ERROR: Could not lookup users, ' . mysql_error($mysql));
if(!$q)
{
die('ERROR: Could not lookup users, ' . mysql_error($mysql));
}
while($row[] = mysql_fetch_assoc($q))
{
$lp[] = $row['lastPing'];
$usr[] = $row['usr'];
for($i = 0; $i < count($usr);$i++)
{
if($lp[$i] + 180 >= time())
{
$q = mysql_query("UPDATE DenCrypt_Users SET online='Offline' WHERE usr='$usr[$i]'") or die('ERROR: Failed to update user, ' . mysql_error($mysql));
}
}
}
?>
I have ran the same query on phpmyadmin and it works. Why isn’t php fetching the data?
One expects
mysql_fetch_assoc()to eventually return false, at the end of the resultset. Don’t die on this eventuality, just exit the loop:However, as others have mentioned, you really should stop using the now-deprecated
ext/mysql. Switching to the improved MySQLi extension is almost as simple as inserting the letteriinto each function call, although one also ought to invest in parameterising prepared statements in order to mitigate against SQL injection attacks.