3

I am selecting names of my products from products table in MySQL then I want to present these names to something like
$names = array(name1, name2, ...)
Where name1, ... are the names of the products from MySQL. I have gone through suggested similar cases but none seems to solve my problem. I am a newbie and just trying to learn.

4 Answers 4

5
$result = mysql_query("SELECT name FROM products");
$names=array();
while ($row = mysql_fetch_row($result)) $names[]=$row[0];
mysql_free_result($result);

You need the loop, as there is no way to directly get all rows of the complete result set.

Sign up to request clarification or add additional context in comments.

Comments

-1
$result = mysql_query("

SELECT `productname` FROM `products`

")or die($result."<br/><br/>".mysql_error());

    $numrows  = mysql_num_rows($result);  

    while ($row = mysql_fetch_assoc($result))
    {

    $productname[$i] = $row['productname'];

            // to print out use the following
             echo $productname[$i];
            $i++;

    }

Comments

-1

Well, from an academic point of view, I suppose there's a "viable" alternative in doing something like

$names = explode('<|>', mysql_result(mysql_query(
    "SELECT GROUP_CONCAT(name separator '<|>') FROM products GROUP BY 'name'"
), 0));

All the power of loops and arrays in single line, for the low, low price of making baby Jesus cry. And yes, you should probably use a loop. Unless you have a phobia ever since a loop waited for you outside your school when you were little and beat you up, I guess.

5 Comments

from academic point of view one have to create a function. But it seems no PHP user (at least on this site) ever heard of such a feature.
Only if you intend to reuse the code. Otherwise functions themselves will lead to code bloat.
..otherwise oneliner will make no sense. (Note that this case is among most reusable ones)
That depends on the application purpose. Functions are not a silver bullet. If you think it won't make sense, that falls under the comment your code appropriately clause of programming. Not that I advocate the use of this kind of code, just stating an opinion on practices.
this practice indeed requires an advocate
-1

Dunno why everione sticked to one-liner but anyway it's always good idea to make your code less bloated

create a function

  function dbGgetCol($sql) {
    $ret = array(); 
    $res = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql); 
    if ($res) {
      while ($row = mysql_fetch_row($result)) {
        $ret[] = $row[0]; 
      }
      mysql_free_result($res); 
    }
    return $ret;
  }

place it in some config file included into each script
and then get your array with this simple code

$names = dbGetCol("SELECT name FROM products");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.