Jump to content

[SOLVED] shorter way of selecting 1 specific thing from DB?


scarhand

Recommended Posts

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?

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'];

?>

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.

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.

<?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"];
    }
  }
?>

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?

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!

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.

So post count is a reliable metric to determine someones skill level with PHP? Good to know ;D

 

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']; 
?>

Archived

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

×
×
  • Create New...

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.