简体   繁体   English

如何将所有查询结果保存到数组中

[英]How to save all query results into an array

I'm trying to store the query results into an array. 我正在尝试将查询结果存储到数组中。 I've a table with 50 rows and 6 columns, how to save this 50 values into an array? 我有一个包含50行6列的表,如何将这50个值保存到数组中? My problem is save into array like: 我的问题是保存到数组,如:

Value1 - red - metal - 100 - in stock - price 值1-红色-金属-100-库存-价格

So each cell of array must have this organization. 因此,阵列的每个单元必须具有此组织。

You can use 2 dimensional array in this manner 您可以通过这种方式使用二维数组

 $sql = "select * from table";
    $result = mysql_query($query); 
    $myArray = array();
    while(*emphasized text*$arrayresult = mysql_fetch_array($result)) {
       $myArray[] = array(
                        'id'=>$arrayresult['id'],
                        'title'=>$arrayresult['title']
                    );
    }

Use 2 dimensional array. 使用二维数组。

You may use like 您可以使用像

$multi_array = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    array_push($multi_array, $row);
}

您可以使用concat从数据库本身获取它

  $query = "select concat(id,' - ',first_name,' - ' ,last_name) from table_name";

MySQL CONCAT() function is used to add two or more strings in query MySQL CONCAT()函数用于在查询中添加两个或多个字符串

Example: 例:

SELECT CONCAT(pub_city,'--> ',country)
FROM publisher;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM