Page 1 of 1

sql query

Posted: Fri Dec 27, 2002 1:37 am
by jamal
I am having problems with the query below.
I did not understand why it did not work for me.
Please can anyone assist me???
Merry Xmas.

Code: Select all

$sql =  "SELECT received FROM $dbsql ORDER BY id LIMIT 1" ;   
$results  =  mysql_query ( $sql );   
while ( $myrow  =  mysql_fetch_array ($results )) {   
$date  = $myrow ["received" ];   
echo  $date ;   
}
This is the error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource.
This error referred to the line -> while ( $myrow = mysql_fetch_array ($results )) { <-

Posted: Fri Dec 27, 2002 2:39 am
by Elmseeker
have you thought about using quotes in your select statement?

Code: Select all

<?
$sql =  "SELECT 'received' FROM $dbsql ORDER BY 'id' LIMIT 1" ;
?>
This should take care of it :)

Posted: Fri Dec 27, 2002 6:04 pm
by gaagaagui
The error you are getting is most likely because you have not established a connection to your database. Assuming your variable $dbsql correctly contains a valid mysql table and the field received exists in that table, your query statement is syntactically correct and you do not need to place single quotes around your field names. Questionable advice...

Instead of using a while loop, try using the function mysql_num_rows to capture the number of rows selected by your query, assigning that to a variable, say $nr (number of
rows), and using a for loop to iterate through your results:

$nr = mysql_num_rows($results);
echo $nr;
for ($a=0; $a<$nr; $a++) {
get a record
... other code here
}

echo $nr before this loop executes troubleshoots and determines whether or not any results are being returned by your mysql_query function.

Also, instead of monkeying around with the raw php functions, write your own functions that you can call; they can include the php functions, adding in your own debugging echo statements, capturing invalid return values from the php functions.