Jump to content

Archived

This topic is now archived and is closed to further replies.

extrovertive

advantage of using mysql_fetch_row vs. mysql_fetch_assoc?

Recommended Posts

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

[code=php:0]

$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";
}
[/code]

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:
[code=php:0]
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";
}


[/code]


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:

[code=php:0]
while($row = mysql_fetch_assoc($result))
{
$row = array_map("stripslashes", $row);   

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

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?


Share this post


Link to post
Share on other sites
You said it:
[quote]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.
[/quote]
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)

Share this post


Link to post
Share on other sites
Is up to you, but for your last comment - if you are needing to run stripslashes on your [i]database[/i] data, you are running addslashes or mysql_real_escape_string() one too many times on input.

Share this post


Link to post
Share on other sites
I see, ronverdonk. However, I rarely use select * from...since I always know what columns to select.


[quote author=Jenk link=topic=106307.msg425097#msg425097 date=1156984801]
Is up to you, but for your last comment - if you are needing to run stripslashes on your [i]database[/i] data, you are running addslashes or mysql_real_escape_string() one too many times on input.
[/quote]

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?

Share this post


Link to post
Share on other sites
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 [i]upon input[/i]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.