mysql_fetch_array error

He has: 1,380 posts

Joined: Feb 2002

I've got a problem, I have many scripts that use this syntax, but it doesn't work. I don't know why, it comes up with _fetch_array is not a valid argument...anybody see why?

<?php
...connection...

$sql = mysql_query(\"SELECT * FROM data\");
while (
$qry = mysql_fetch_row($sql)) {
    echo \"<p class=\\"
title\\">$qry[header]<\/p>\";
      echo \"<p>&nbsp;&nbsp;&nbsp;
$qry[body]<\/p>\";
      echo \"<p class=\\"
line\\">---------------<\/p>\";
    }
mysql_close(
$dbh);
?>

thanks

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

mysql_fetch_array?

He has: 1,380 posts

Joined: Feb 2002

lol _fetch_row

sorry.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

http://www.webmaster-forums.net/showthread.php?t=22762&highlight=mysql_fetch_row+mysql_fetch_array from the last time we talked about this, in case you need the reference. Smiling

I tend to code it differently, so you may be getting an error in something I can't see because I'm not familiar with it:

<?php

...connection...

$sql = mysql_query(\"SELECT * FROM data\");
// this *should* work, but I use mysql_fetch_array usually
while (
$qry = mysql_fetch_row($sql)) {
   
$header = $qry[0];
   
$body = $qry[1];

echo<<<DISPLAY
<p class=\"title\">
$header</p>
<p>&nbsp;&nbsp;&nbsp;
$body</p>
<p class=\"line\">---------------</p>
DISPLAY;



mysql_close(
$dbh);
                                           
?>

In general, it looks fine, perhaps there is no data returned? Put in some error messages and see what you get?

They have: 447 posts

Joined: Oct 1999

also, even though I believe the php manual claims theres little performance difference, you should use mysql_fetch_assoc or mysql_fetch_row unless you specifically need both, because I've benchmarked them and mysql_fetch_array is significanlty slower.

druagord's picture

He has: 335 posts

Joined: May 2003

with fetch_row you have to use $qry[0] $qry[1] with fetch_array you can use $qry['header'] and $qry['body']

They have: 447 posts

Joined: Oct 1999

druagord wrote: with fetch_row you have to use $qry[0] $qry[1] with fetch_array you can use $qry['header'] and $qry['body']

fetch_row returns a number indexed array, fetch_assoc returns a string indexed hash, fetch_array returns both.

He has: 1,380 posts

Joined: Feb 2002

Thanks, suzanne...

That's the script i was referring to, and didn't understand what was wrong

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

I thought it was something like that, Druagord, I've amended my code sample to accomodate that.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.