简体   繁体   English

如何将mySQL数据库中的数据存储到数组中?

[英]How can I store data from mySQL database into an array?

I want to make an array from mysql database. 我想从mysql数据库中创建一个数组。 This is my table data : 这是我的表data

+--------+-------+--------+--------+
| order  |   id  |  name  |   age  |
+--------+-------+--------+--------+
|   12   |  121  |  fred  |   23   |
|   12   |  122  |  sam   |   24   | 
|   12   |  123  |  joe   |   42   |
|   12   |  124  |  alan  |   33   |
+--------+-------+--------+--------+

The array should look in the end like this: 数组的结尾应如下所示:

array(4) {
  ["121"]=>
  array(2) {
    ["name"]=>
    string(4) "fred"
    ["age"]=>
    string(2) "23"
  }
  ["122"]=>
  array(2) {
    ["name"]=>
    string(3) "sam"
    ["age"]=>
    string(2) "24"
  }
  ["123"]=>
  array(2) {
    ["name"]=>
    string(3) "joe"
    ["age"]=>
    string(2) "42"
  }
  ["124"]=>
  array(2) {
    ["name"]=>
    string(4) "alan"
    ["age"]=>
    string(2) "33"
  }
  }

This is how I am creating the array: 这就是我创建数组的方式:

$sql = "SELECT * FROM data WHERE order = '12'";                 
$q = $pdo->prepare($sql);
$q->execute();
$result = $sql->fetchAll();
var_dump($result); 

But my result is: 但是我的结果是:

array(4) {
  [0]=>
  array(20) {
    ["order"]=>
    string(10) "12"
    [0]=>
    string(10) "12"
    ["id"]=>
    string(32) "121"
    [1]=>
    string(32) "121"
    ["name"]=>
    string(12) "fred"
    [2]=>
    string(12) "fred"
    ["age"]=>
    string(32) "23"
    [3]=>
    string(32) "23"
  }
[0]=>
  array(20) {
    ["order"]=>
    string(10) "12"
    [0]=>
    string(10) "12"
    ["id"]=>
    string(32) "122"
    [1]=>
    string(32) "122"
    ["name"]=>
    string(12) "sam"
    [2]=>
    string(12) "sam"
    ["age"]=>
    string(32) "24"
    [3]=>
    string(32) "24"
  }
and so on...

I do not know how to get the array into the right form 我不知道如何将数组转换为正确的形式

To start, you're combining calls from different APIs, which doesn't work, and second, you're trying to fetch records from a text string, not from a result set. 首先,您要组合来自不同API的调用,这是行不通的;其次,您试图从文本字符串而不是从结果集中获取记录。

NOTE: The following replaces a previous answer which did not provide the desired results. 注意:以下内容替换了先前的答案,该答案未提供期望的结果。

Here's what I've come up with, after changing some column names that my database didn't like: 在更改了数据库不喜欢的某些列名称之后,我想出了以下方法:

<pre>
<?php
    $some_value = 12;
    $dbh = new PDO(/*Your connect string goes here*/);
    $sql = "SELECT id, name, age FROM some_data WHERE order_id = :order_key";                 
    $stmt = $dbh->prepare($sql);
    $stmt->execute(array(':order_key'=>$some_value));
    $my_output = array();
    foreach($stmt->fetchAll(PDO::FETCH_ASSOC) as $rec)
    {
        $my_output[$rec['id']] = array('name' => $rec['name'], 'age' => $rec['age']);
    }
    var_dump($my_output);
?>
</pre>

Changing the value of the variable $some_value will allow you to run the query against other values of order_id . 更改变量$some_value的值将使您可以对order_id其他值运行查询。

The output produced looks like this: 产生的输出如下所示:

array(4) {
  [121]=>
  array(2) {
    ["name"]=>
    string(4) "fred"
    ["age"]=>
    int(23)
  }
  [122]=>
  array(2) {
    ["name"]=>
    string(3) "sam"
    ["age"]=>
    int(24)
  }
  [123]=>
  array(2) {
    ["name"]=>
    string(3) "joe"
    ["age"]=>
    int(42)
  }
  [124]=>
  array(2) {
    ["name"]=>
    string(4) "alan"
    ["age"]=>
    int(33)
  }
}

Wrangling the types I leave as an exercise for the reader. 整理我留下的类型以供读者练习。

If you using PDO, there is elegant method 如果您使用PDO,则有一种优雅的方法

$sql = "SELECT `id`, `name`, `age` FROM `data` WHERE `order` = '12'"; 
$q = $pdo->prepare($sql);
$q->execute();
$result = $q->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_UNIQUE | PDO::FETCH_ASSOC);
var_dump($result);

Also, if you need id as keys and as values in $result, then declare id column in SQL query twice: 另外,如果需要id作为键和$ result中的值,则在SQL查询中两次声明id列:

SELECT `id`, `id`, `name`, `age` FROM `data` WHERE `order` = '12'

I think this will work fine: 我认为这可以正常工作:

$ps = array();
while ($row = $result->fetch_assoc()) {
 $p=array();
 $p["name"]=$row["name"];
 $p["age"]=$row["age"];
 $ps[$row["id"]]=$p;
}

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

相关问题 如何将数组中的数据存储到mySQL数据库中? - How can I store data from an array into the mySQL database? 如何将MySQL数据库中的数据存储到ID索引的数组中? - How can I store data from MySQL database into array indexed by id? 如何将区域语言 php 数组数据(古吉拉特语、印地语、旁遮普语)存储和检索到 mysql 数据库? - How can I store and retrieve regional language php array data (gujarati, hindi, punjabi) to mysql database? 如何使用mysql php将数组值存储在数据库中 - how can i store the array values in database using mysql php 如何在php中从mysql数据库将数据存储为关联数组 - how to store data as an associative array from mysql database in php 如何将嵌套/多维数组中的值存储在 MySQL 数据库中? - How can I store values from a nested/multidimensional array in a MySQL database? 我如何将大量内容从多阵列数组存储到mysql数据库中? - How can I store a huge amount of content from multidiemsional array into mysql database? 如何在MySQL数据库中存储对象的颜色数据? - How can I store an object's color data in MySQL database? 如何将敏感数据安全地存储在MySQL数据库中? - How can I store sensitive data securely in a MySQL database? 如何使用PHP从另一个网站获取数据并将其存储在MySQL数据库中? - How can I use PHP to fetch data from another website and store it in a MySQL database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM