Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将两个表中的数据存储到一个php数组中_Php_Mysql_Database - Fatal编程技术网

将两个表中的数据存储到一个php数组中

将两个表中的数据存储到一个php数组中,php,mysql,database,Php,Mysql,Database,我试图将$tables中列出的表中的所有数据存储到一个数组中,然后将其编码为json。但是,仅存储orderInfo中的最后一个条目。我该如何解决这个问题?我认为当处理单个表中的数据时,mysqli_num_行的行为不同 $tables = array('userInfo', 'orderInfo'); for ($i = 0; $i < count($tables); $i++) { $sql = "SELECT * FROM `{$tables[$i]

我试图将$tables中列出的表中的所有数据存储到一个数组中,然后将其编码为json。但是,仅存储orderInfo中的最后一个条目。我该如何解决这个问题?我认为当处理单个表中的数据时,mysqli_num_行的行为不同

$tables = array('userInfo', 'orderInfo');
    for ($i = 0; $i < count($tables); $i++) {
        $sql = "SELECT * FROM `{$tables[$i]}`;";
        $result = mysqli_query($conn, $sql);

        $data = array();
        if (mysqli_num_rows($result) > 0) {
          while ($row = mysqli_fetch_assoc($result)) {
              $data[$tables[$i]] = $row;
          }
        }
    }
    die(json_encode($data));
$tables=array('userInfo','orderInfo');
对于($i=0;$i0){
while($row=mysqli\u fetch\u assoc($result)){
$data[$tables[$i]]=$row;
}
}
}
die(json_编码($data));

在for循环之前移动空数组
$data
,因为每个循环
$data
都将重置,直到最后一个条目

同时将while循环中的
$data[$tables[$i]]]
更改为
$data[$tables[$i]]][

<?php

$data = array();
$tables = array('userInfo', 'orderInfo');
for ($i = 0; $i < count($tables); $i++) {
    $sql = "SELECT * FROM `{$tables[$i]}`;";
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
      while ($row = mysqli_fetch_assoc($result)) {
          $data[$tables[$i]][] = $row;
      }
    }
}
die(json_encode($data));

在for循环之前移动空数组
$data
,因为每个循环
$data
都将重置,直到最后一个条目

同时将while循环中的
$data[$tables[$i]]]
更改为
$data[$tables[$i]]][

<?php

$data = array();
$tables = array('userInfo', 'orderInfo');
for ($i = 0; $i < count($tables); $i++) {
    $sql = "SELECT * FROM `{$tables[$i]}`;";
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
      while ($row = mysqli_fetch_assoc($result)) {
          $data[$tables[$i]][] = $row;
      }
    }
}
die(json_encode($data));

如前所述,您需要将$data移出for循环,此外,您应该能够在while之前禁止if语句。这是多余的,因为如果没有行,while无论如何都不应该运行:

$data = [];
$tables = array('userInfo', 'orderInfo');
foreach($tables as $tableName){
    $sql = "SELECT * FROM `{$tableName}`;";
    $result = mysqli_query($conn, $sql);
    
    while ($row = mysqli_fetch_assoc($result)) {
        $data[$tableName] = $row;
    }
}
die(json_encode($data));

如前所述,您需要将$data移出for循环,此外,您应该能够在while之前禁止if语句。这是多余的,因为如果没有行,while无论如何都不应该运行:

$data = [];
$tables = array('userInfo', 'orderInfo');
foreach($tables as $tableName){
    $sql = "SELECT * FROM `{$tableName}`;";
    $result = mysqli_query($conn, $sql);
    
    while ($row = mysqli_fetch_assoc($result)) {
        $data[$tableName] = $row;
    }
}
die(json_encode($data));

将空数组
$data
移动到loopsNote之前:明显减少了冗余,使代码更易于阅读和审核,并且不容易与过时的
mysql\u查询
接口混淆,因为缺少单个
i
可能会导致问题。使用这种风格:
$db=newmysqli(…)
$db->prepare(“…”)
过程接口是PHP4时代的产物,不应在新代码中使用。此外,过程接口没有严格的错误检查和报告,这会阻碍调试工作。像这样使用
die
是非常病态的。为什么不
echo
然后退出呢?将空数组
$data
移动到loopsNote之前:这样可以大大减少冗余,使代码更易于阅读和审核,并且不容易与过时的
mysql\u查询
接口混淆,因为缺少单个
i
可能会导致问题。使用这种风格:
$db=newmysqli(…)
$db->prepare(“…”)
过程接口是PHP4时代的产物,不应在新代码中使用。此外,过程接口没有严格的错误检查和报告,这会阻碍调试工作。像这样使用
die
是非常病态的。为什么不
echo
然后退出?