Jump to content


Photo

advantage of using mysql_fetch_row vs. mysql_fetch_assoc?


  • Please log in to reply
4 replies to this topic

#1 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 30 August 2006 - 07:46 PM

In all of my PHP scripts when interacting with the mysql database, I always use mysql_fetch_row with list:


$query = "SELECT col1, col2, col3 FROM table";
$result = mysql_query($query) or die ("Error in table: " . mysql_error());

while(list($mycol1, $mycol2, $mycol3) = mysql_fetch_row($result))
{
  echo $mycol1 . " " . $mycol2 . " " . $mycol3 . "<br />\n";
}

To me, I think that's very efficient if you're just going to retrieve values from a table and just output it - assuming you know what columns you want to retrieve.

Other methods:
while($row = mysql_fetch_row($result))
{
echo $row['col1'] . " " . $row['col2'] . " " . $row['col2']. "<br />\n";
}

OR
while($row = mysql_fetch_row($result))
{
$mycol1 = $row['col1'];
$mycol2 = $row['col2'];
$mycol2 = $row['col3'];

echo $mycol1 . " " . $mycol2 . " " . $mycol3 . "<br />\n";
}




The former requires you to output the value as an associative array rather than a single variable. The latter requires you to initialize the associative array to a local variable.


The only advantage for using mysql_fetch_array or (mysql_fetch_array(MYSQL_ASSOC) )/mysql_fetch_assoc

is to clean the up the column array first. Example:

while($row = mysql_fetch_assoc($result))
{
$row = array_map("stripslashes", $row);    

echo $row['col1'] . " " . $row['col2'] . " " . $row['col2']. "<br />\n";
}

However, if you're not doing that...what's are the advantages/disadvantages of using list..with mysql_fetch_row vs. mysql_fetch_array/assoc?




#2 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 30 August 2006 - 08:19 PM

You said it:

To me, I think that's very efficient if you're just going to retrieve values from a table and just output it - assuming you know what columns you want to retrieve.

IF you always know what columns and IF you just output it: why not use the list construction? Makes you also very rigid.
Personally, I prefer and mostly use the associative array. And very often I use the SELECT * statement. It makes me feel less rigid in my handling of the data.

Ronald  8)
RTFM is an almost extinct art form, it should be subsidized.

#3 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 31 August 2006 - 12:40 AM

Is up to you, but for your last comment - if you are needing to run stripslashes on your database data, you are running addslashes or mysql_real_escape_string() one too many times on input.

#4 extrovertive

extrovertive
  • Members
  • PipPipPip
  • Advanced Member
  • 235 posts

Posted 31 August 2006 - 12:43 AM

I see, ronverdonk. However, I rarely use select * from...since I always know what columns to select.


Is up to you, but for your last comment - if you are needing to run stripslashes on your database data, you are running addslashes or mysql_real_escape_string() one too many times on input.


Well, when you use mysql_fetch_assoc/array, you're getting data from the database right? So, why do you need to addslashes? It should be strip slashes, shouldn't it?

#5 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 31 August 2006 - 12:45 AM

Read my post again. If you need to stripslashes() your data from the database, you are using addslashes() or mysql_real_escape_string() one too many times upon input




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users