scarhand Posted September 25, 2007 Share Posted September 25, 2007 ok i know this works: $mainbgcolor_result = mysql_query("SELECT value FROM styles WHERE name = 'mainbgcolor'"); while($row = mysql_fetch_assoc($mainbgcolor_result)) { $mainbgcolor = $row['value']; } I am wondering if there is a shorter way of coding this? Quote Link to comment https://forums.phpfreaks.com/topic/70664-solved-shorter-way-of-selecting-1-specific-thing-from-db/ Share on other sites More sharing options...
pocobueno1388 Posted September 25, 2007 Share Posted September 25, 2007 Well, if you are only expecting one result, then you don't need the while loop. <?php $mainbgcolor_result = mysql_query("SELECT value FROM styles WHERE name = 'mainbgcolor'"); $row = mysql_fetch_assoc($mainbgcolor); echo $row['value']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/70664-solved-shorter-way-of-selecting-1-specific-thing-from-db/#findComment-355174 Share on other sites More sharing options...
scarhand Posted September 25, 2007 Author Share Posted September 25, 2007 well i am going to have the script like this: $mainbgcolor_result = mysql_query("SELECT value FROM styles WHERE name = 'mainbgcolor'"); while($row = mysql_fetch_assoc($mainbgcolor_result)) { $mainbgcolor = $row['value']; } $maintextcolor_result = mysql_query("SELECT value FROM styles WHERE name = 'maintextcolor'"); while($row = mysql_fetch_assoc($maintextcolor_result)) { $maintextcolor = $row['value']; } $mainbordercolor_result = mysql_query("SELECT value FROM styles WHERE name = 'mainbordercolor'"); while($row = mysql_fetch_assoc($mainbordercolor_result)) { $mainbordercolor = $row['value']; } etc....so you see why i want it shortened. Quote Link to comment https://forums.phpfreaks.com/topic/70664-solved-shorter-way-of-selecting-1-specific-thing-from-db/#findComment-355180 Share on other sites More sharing options...
rlindauer Posted September 25, 2007 Share Posted September 25, 2007 Wouldnt it be better to use mysql_result if you are only grabbing one row? http://us.php.net/manual/en/function.mysql-result.php Quote Link to comment https://forums.phpfreaks.com/topic/70664-solved-shorter-way-of-selecting-1-specific-thing-from-db/#findComment-355182 Share on other sites More sharing options...
scarhand Posted September 25, 2007 Author Share Posted September 25, 2007 Wouldnt it be better to use mysql_result if you are only grabbing one row? http://us.php.net/manual/en/function.mysql-result.php mysql_result is slower than mysql_fetch_assoc (according to w3schools), and its about the same amount of coding. Please don't answer my question with another question. Quote Link to comment https://forums.phpfreaks.com/topic/70664-solved-shorter-way-of-selecting-1-specific-thing-from-db/#findComment-355190 Share on other sites More sharing options...
roopurt18 Posted September 25, 2007 Share Posted September 25, 2007 <?php $sql = "SELECT name, value FROM styles WHERE name IN ('mainbgcolor', 'maintextcolor', 'mainbordercolor')"; $q = mysql_query($sql); if($q){ while($row = mysql_fetch_assoc($q)){ ${$row["name"]} = $row["value"]; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/70664-solved-shorter-way-of-selecting-1-specific-thing-from-db/#findComment-355191 Share on other sites More sharing options...
scarhand Posted September 25, 2007 Author Share Posted September 25, 2007 thanks, solved. Quote Link to comment https://forums.phpfreaks.com/topic/70664-solved-shorter-way-of-selecting-1-specific-thing-from-db/#findComment-355192 Share on other sites More sharing options...
Jessica Posted September 25, 2007 Share Posted September 25, 2007 Wouldnt it be better to use mysql_result if you are only grabbing one row? http://us.php.net/manual/en/function.mysql-result.php mysql_result is slower than mysql_fetch_assoc (according to w3schools), and its about the same amount of coding. Please don't answer my question with another question. Haven't you ever heard of the Socratic Method? Quote Link to comment https://forums.phpfreaks.com/topic/70664-solved-shorter-way-of-selecting-1-specific-thing-from-db/#findComment-355196 Share on other sites More sharing options...
GingerRobot Posted September 25, 2007 Share Posted September 25, 2007 Wouldnt it be better to use mysql_result if you are only grabbing one row? http://us.php.net/manual/en/function.mysql-result.php mysql_result is slower than mysql_fetch_assoc (according to w3schools), and its about the same amount of coding. Please don't answer my question with another question. Haven't you ever heard of the Socratic Method? Thats just rude jesi, you asked another question! Shock horror! Quote Link to comment https://forums.phpfreaks.com/topic/70664-solved-shorter-way-of-selecting-1-specific-thing-from-db/#findComment-355208 Share on other sites More sharing options...
gromer Posted September 25, 2007 Share Posted September 25, 2007 Please don't answer my question with another question. I think what he was trying to do was help make your code more efficient....lol Quote Link to comment https://forums.phpfreaks.com/topic/70664-solved-shorter-way-of-selecting-1-specific-thing-from-db/#findComment-355212 Share on other sites More sharing options...
scarhand Posted September 25, 2007 Author Share Posted September 25, 2007 Wouldnt it be better to use mysql_result if you are only grabbing one row? http://us.php.net/manual/en/function.mysql-result.php mysql_result is slower than mysql_fetch_assoc (according to w3schools), and its about the same amount of coding. Please don't answer my question with another question. Haven't you ever heard of the Socratic Method? obviously not. Please don't answer my question with another question. I think what he was trying to do was help make your code more efficient....lol ahhh i saw his post count was only 54, so assumed he was as new as i am to php/mysql. Quote Link to comment https://forums.phpfreaks.com/topic/70664-solved-shorter-way-of-selecting-1-specific-thing-from-db/#findComment-355215 Share on other sites More sharing options...
rlindauer Posted September 26, 2007 Share Posted September 26, 2007 So post count is a reliable metric to determine someones skill level with PHP? Good to know I posted the link to mysql-result since the OP only wanted to pull a single value from the table. It seemed a good method to use. I have not been able to find any information about whether or not one method is faster than another. Is it really the same amount of coding though? mysql_result() <?php $result = mysql_query('SELECT name FROM work.employee'); if (!$result) { die('Could not query:' . mysql_error()); } echo mysql_result($result, 2); // outputs third employee's name ?> mysql_fetch_assoc() <?php $result = mysql_query('SELECT name FROM work.employee'); if (!$result) { die('Could not query:' . mysql_error()); } while ($row = mysql_fetch_assoc($result)) { echo $row['name']; } ?> mysql_fetch_assoc() w/out the loop <?php $result = mysql_query('SELECT name FROM work.employee'); if (!$result) { die('Could not query:' . mysql_error()); } $row = mysql_fetch_assoc($result); echo $row['name']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/70664-solved-shorter-way-of-selecting-1-specific-thing-from-db/#findComment-355359 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.