Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.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 使用mysqli在数组中存储列值_Php_Mysql_Arrays_Mysqli - Fatal编程技术网

Php 使用mysqli在数组中存储列值

Php 使用mysqli在数组中存储列值,php,mysql,arrays,mysqli,Php,Mysql,Arrays,Mysqli,我试图将列值收集到一个数组中,这样就可以得到所有值的平均值。以下是我目前的代码: $conn = new mysqli($hn, $un, $pw, $db); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $average_hdpe = "SELECT hdpe FROM $region

我试图将列值收集到一个数组中,这样就可以得到所有值的平均值。以下是我目前的代码:

    $conn = new mysqli($hn, $un, $pw, $db);
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

$average_hdpe = "SELECT hdpe FROM $region";
$average_hdpe_result = $conn->query($average_hdpe) or die(mysqli_error($conn)) ;
$average_hdpe_array = $average_hdpe_result->fetch_array();

print_r($average_hdpe_array);
运行上述代码将打印:

阵列[0]=>1147[hdpe]=>1147

我期待着4个条目:11471152 11571157。
有人能告诉我我做错了什么吗?大概fetch_数组并没有像我想的那样

创建一个数组将提取的数据存储在数组中

  $average_hdpe = "SELECT hdpe FROM $region";
    $average_hdpe_result = $conn->query($average_hdpe) or die(mysqli_error($conn)) ;
     $array = array();//create empty array
     while($row = $average_hdpe_result->fetch_array()){//loop to get all results
         $array[] = $row;//grab everything and store inside array
     }
print_r($array);//this should give you everything
$conn = new mysqli($hn, $un, $pw, $db);
/* check connection */
if ($conn->connect_error) {
    printf("Connect failed: %s\n", $conn->connect_error);
exit();
}

$average_hdpe = "SELECT hdpe FROM $region";
$average_hdpe_result = $conn->query($average_hdpe) or die() ;
$average_hdpe_array = array();
while($row = $average_hdpe_result->fetch_assoc()){
$average_hdpe_array[] = $row;
}

print_r($average_hdpe_array);

您只获取第一行。尝试使用fetch_all而不是fetch_数组,或者保留fetch_数组并循环结果。这些功能的手册中有很好的例子。您还获得了双值,因为它同时获取数值和关联,您可以获取_assoc以仅获取关联。您混淆了过程方法和面向对象方法为什么不使用SELECT AVGhdpe FROM$region?