Forum Moderators: coopster

Message Too Old, No Replies

Database fetching gives warning

         

kkonline

1:36 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



I am using the following code to extract info from db and display it. The extracted info is basically a link to a stored image.

I get the following warning after $sql data is printed as shown

SELECT * FROM reflections WHERE `trusted` = 1 AND `catid` = 1 ORDER BY `id` ASC LIMIT 8, 1
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/mysite/public_html/addons/class.upload/pages.php on line 66

$sql = "SELECT * FROM reflections WHERE `trusted` = 1 AND `catid` = 1 ORDER BY `id` ASC LIMIT $from, $max_results";

mysql_query($sql);
echo $sql;
while($row = mysql_fetch_array($sql))
{
if(strlen(isset($_GET['tcheck']))>0 && $_GET['tcheck']==$safeurl->make_safe_url($row['title'])){
$tcheck=trim(mysql_real_escape_string($_GET['tcheck']));
}else{
echo "invalid query";
exit;}

echo "<!--start-->";
echo "<font size=3><b>";
echo $row['title']."</b><br /></font>";
echo "<font size=2>";
echo "Added On ";
echo date("jS F Y H:i:s", $row['date'])."<p>";
echo '<img src="/addons/class.upload/test/'.$name.'" border="0" height="50%" width="50%" alt="' . $row['title'] .'" />';
echo "<!--end-->";
}

jatar_k

1:46 pm on Sep 18, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



usually means there was an error, add an or die statement to your query call

mysql_query($sql) or die('select died: ' . mysql_error());

kkonline

1:54 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



I did as u suggested, still the same errors

$sql = "SELECT * FROM reflections WHERE `trusted` = 1 AND `catid` = 1 ORDER BY `id` ASC LIMIT $from, $max_results";

$result = mysql_query($sql) or die('select died: ' . mysql_error());
echo $sql;
echo $result;
while($row = mysql_fetch_array($sql))
{
... some data
}

To this it gives me $sql,$result followed by the warning as shown

SELECT * FROM reflections WHERE `trusted` = 1 AND `catid` = 1 ORDER BY `id` ASC LIMIT 8, 1

Resource id #6

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/mysite/public_html/addons/class.upload/pages.php on line 67

jatar_k

1:57 pm on Sep 18, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



now I see it

this
while($row = mysql_fetch_array($sql))

should be
while($row = mysql_fetch_array($result))

Resource id #6 is exactly what it should show when you echo $result, that's a valid resource

and you didn't get the die message so we know that the query worked

just the wrong var is all

kkonline

2:13 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



$sql = "SELECT * FROM reflections WHERE `trusted` = 1 AND `catid` = 1 ORDER BY `id` ASC LIMIT $from, $max_results";

$result = mysql_query($sql) or die('select died: ' . mysql_error());
echo $sql;
echo $result;
while($row = mysql_fetch_array($result)){
echo "inside while loop";
}


Now the output is

SELECT * FROM reflections WHERE `trusted` = 1 AND `catid` = 1 ORDER BY `id` ASC LIMIT 8, 1
Resource id #6

No warnings but still it does not go into the while loop and print "inside while loop"... what could be the reason now? Any problem with the query

eelixduppy

2:18 pm on Sep 18, 2007 (gmt 0)



Run the query through the command line or phpmyadmin to see if it is returning any results. If it isn't, then that is why it isn't going into the loop. Also, you shouldn't be echoing $result as it is a resource variable.

kkonline

2:25 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



SELECT * FROM reflections WHERE `trusted` = 1 AND `catid` = 1 ORDER BY `id` ASC LIMIT 0, 1

works but as the query returned LIMIT 8,1 it was not going into the loop

Secondly
why not to display the resource 06 thing( i displayed for debugging although), please put some light on this resource thing

jatar_k

2:36 pm on Sep 18, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



when you echo a resource, it just tells you that the var contains a resource, not overly useful

adding the or die after your query call as I did will show you the actual error from mysql if there is one. If the query works then it will not be displayed. It makes echoing the resource redundant plus gives you information you can use to fix it.

kkonline

2:47 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



Ok now i understand.

I am using the code below to display the image

  echo '<img src="/addons/class.upload/test/'.$name.'" border="0" height="50%" width="50%" alt="' . $row['title'] .'" />';

it doesn't display the image and on clicking on image properties it gives http://example.com/addons/class.upload/test/

instead of http://example.com/addons/class.upload/test/confidence_1.jpg
which is the actual image , i asked this question in the morning also and then someone gave me the above code but now it doesn't display the image

eelixduppy

2:54 pm on Sep 18, 2007 (gmt 0)



What does the html source look like? Also, are you saying your while look is functioning correctly now?

kkonline

3:00 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



Yes the while loop is functioning corrected there was problem with the limits it said limit 8,1 and didnot give any result in phpmyadmin i made it to 0,1 and it was fine

secondly
I solved image thing, by writing

  $name = $row['name'];
echo '<img src="/addons/class.upload/test/'.$name.'" border="0" height="50%" width="50%" alt="' . $row['title'] .'" />';

i was missing the first statement

ALL OK NOW Thanks to you all :)

eelixduppy

3:02 pm on Sep 18, 2007 (gmt 0)



hehe - ok, glad you found your problem :)