extrovertive Posted August 30, 2006 Share Posted August 30, 2006 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";}ORwhile($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_associs 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? Quote Link to comment Share on other sites More sharing options...
ronverdonk Posted August 30, 2006 Share Posted August 30, 2006 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) Quote Link to comment Share on other sites More sharing options...
Jenk Posted August 31, 2006 Share Posted August 31, 2006 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 Link to comment Share on other sites More sharing options...
extrovertive Posted August 31, 2006 Author Share Posted August 31, 2006 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? Quote Link to comment Share on other sites More sharing options...
Jenk Posted August 31, 2006 Share Posted August 31, 2006 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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.