Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fetch Array problem 3

Status
Not open for further replies.

M2E

IS-IT--Management
Aug 1, 2005
28
GB
Having trouble creating a while loop to view records in my db (guestbook)
I keep getting the following error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home2/rgevans/public_html/guestbook/guestbook-view.php on line 139


this is my code - can anyone see what ive done wrong?


Code:
$sql = "SELECT * from m2e_guestbook order by time desc limit 3";
				$rs = @mysql_query($sql);
				
				while ($row = mysql_fetch_array($rs))
				{
			?>
				
            <table border="1" width="544">
              <tr>
				<td><b>Name:</b> <? echo $row["name"]; ?></td>
				<td><b>Email:</b>
				<a href="mailto:<? echo $row["email"]; ?>">
				<? echo $row ["email"]; ?></a> </td></tr>
				<tr>
                <td colspan="2"> 
                  <?php	$datetime = $row["time"];
					$year = substr ($datetime,0,4);
					$mon = substr ($datetime,4,2);
					$day = substr ($datetime,6,2);
					$hour = substr ($datetime,8,2);
					$min = substr ($datetime,10,2);
					$sec = substr ($datetime,12,2);
					
					$orgdate = date("1 F dS, Y h:i A", mktime($hour,$min,$sec,$mon,$day,$year));
			?>
                  <strong>Date:</strong> <? echo $orgdate; ?></td>
              </tr>
			<tr><td colspan="2"><b>Comments:</b>
			<? echo $row["comments"]; ?></td></tr>
			</table>
			<br>
          
		  <? } ?>



----Ment2Excel (M2E)----
--LEARN BY EXAMPLE--
 
Please tread carefully, as I am a complete nothing on PHP, but
I noticed something different in a bit of mine. You could be correct so please stay on the ground.

$rs = @mysql_query($sql);

My bit does not have a @ in front of mysql. If it's legitimate
then I have learnt a bit more and sorry. Regards
 
Try this:
Code:
$rs = mysql_query($sql) or die ("Could not execute query: " . mysql_error());
This will tell you what's wrong with the query. I assume you have connected to the database server before trying to execute queries as well as selected the database (if needed). Also, what could be the problem is the column [tt]time[/tt], since it could be a reserved MYSQL word. To fight that, try wrapping it in backticks:
Code:
$sql = "SELECT * FROM m2e_guestbook ORDER BY `time` DESC LIMIT 3";
 
Oh, as an addendum:

To answer ZOR's observation. @ is the error supression operator. It will prevent displaying any errors if the function is not executed sucessfully.
 
Thanks all for your feedback - problem was with column name - all is working now!!!

----Ment2Excel (M2E)----
--LEARN BY EXAMPLE--
 
Thankyou Vragabond, now I know. Have a star.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top