Forum Moderators: coopster

Message Too Old, No Replies

Newb - Is it obvious what I'm doing wrong?

supplied argument is not a valid MySQL result

         

cuce

10:36 pm on Jun 1, 2006 (gmt 0)

10+ Year Member



I'm new at this. if anybody can lend a hand that would be GREATLY appreciated. I don't even really understand what the error messsage that I'm getting means.

Here's my code:
<?
$dbh=mysql_connect ("localhost", "usr", "pw") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("mydb");
$sql = "SELECT title, link, description FROM reports order by title";
$result = mysql_query($sql);
for($r=1;$r<=mysql_num_rows($result);$r++){
$row = mysql_fetch_array($result);
echo'
<div class="books">
<div class="iframe">
<iframe src="$row[link]" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<h3>$row[title]</h3>
<p>$row[description]</p>
</div>
';} ;
?>

I get this when I open the page:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result

Thanks!

jatar_k

10:44 pm on Jun 1, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> supplied argument is not a valid MySQL result

that means the query is dying, change this line

$result = mysql_query($sql);

to this

$result = mysql_query($sql) or die(mysql_error());

that will give you the real error message from mysql

eelixduppy

12:35 am on Jun 2, 2006 (gmt 0)



Also, just a little performance tip. Change this line:
for($r=1;$r<=mysql_num_rows($result);$r++){

To:
$num_rows = mysql_num_rows($result);
for($r=1;$r<=$num_rows;$r++){

This makes it so the 'mysql_num_rows' function isn't called each time the loop executes.

eelixduppy

2:16 am on Jun 2, 2006 (gmt 0)



Also, i just noticed this. When you get your query to work, its not going to display it correctly. Using single quotes will print the variable name, not the value. Change it to this:
echo "
<div class=\"books\">
<div class=\"iframe\">
<iframe src=\"$row[link]\" scrolling=\"no\" marginwidth=\"0\" marginheight=\"0\" frameborder=\"0\"></iframe>
</div>
<h3>$row[title]</h3>
<p>$row[description]</p>
</div>
";

This should work ;)

cuce

3:25 pm on Jun 2, 2006 (gmt 0)

10+ Year Member



thanks guys very helpful!

eelixduppy

3:39 pm on Jun 2, 2006 (gmt 0)



You're welcome. I'm assuming that you got the query to work correctly.

cuce

4:48 am on Jun 5, 2006 (gmt 0)

10+ Year Member



yep works like a charm :)