将MySQL结果转换为数组字符串

时间:2021-04-03 21:16:17

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.

我从MySQL的产品表中选择我的产品名称然后我想将这些名称呈现为类似于$ names = array(name1,name2,...)的名称,其中name1,...是来自MySQL的产品的名称。我已经完成了类似的案例,但似乎没有解决我的问题。我是新手,只是想学习。

4 个解决方案

#1


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.

您需要循环,因为无法直接获取完整结果集的所有行。

#2


-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++;

    }

#3


-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.

所有循环和阵列的力量都在一条线上,以低廉的价格让宝宝耶稣哭泣。是的,你应该使用一个循环。除非你有一个恐惧症,因为你小时候在学校外面等待你并且打你的时候,我想。

#4


-1  

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

Dunno为什么永远坚持单线,但无论如何,让你的代码不那么臃肿总是好主意

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");

#1


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.

您需要循环,因为无法直接获取完整结果集的所有行。

#2


-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++;

    }

#3


-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.

所有循环和阵列的力量都在一条线上,以低廉的价格让宝宝耶稣哭泣。是的,你应该使用一个循环。除非你有一个恐惧症,因为你小时候在学校外面等待你并且打你的时候,我想。

#4


-1  

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

Dunno为什么永远坚持单线,但无论如何,让你的代码不那么臃肿总是好主意

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");