sql query

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jamal
Forum Commoner
Posts: 57
Joined: Sat Oct 26, 2002 7:53 pm
Location: London

sql query

Post 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 )) { <-
User avatar
Elmseeker
Forum Contributor
Posts: 132
Joined: Sun Dec 22, 2002 5:48 am
Location: Worcester, MA

Post 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 :)
User avatar
gaagaagui
Forum Newbie
Posts: 7
Joined: Fri Dec 27, 2002 6:04 pm
Location: Crown Point, IN USA

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