mysqli_fetch_array()不产生数组

时间:2019-09-25 23:35:19

标签: php arrays wordpress resultset

我似乎无法将结果集放入数组中,然后使用print_r打印出数组。这是脚本:

<?php

require( 'wp-load.php' );

$local = 'xxx';
$user  = 'xxx'; 
$pass  = 'xxx';
$data  = 'xxx';

$testConnection  = mysqli_connect($local,$user,$pass, $data); 


if (!$testConnection) {
die('Error: ' . mysqli_connect_errno() . PHP_EOL);
}
echo 'Database connection working!';


global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM $wpdb->options" );


/* this is commented out but works 

foreach ( $result as $row )   {
echo $row->option_id.'<br>';
}   
*/  

//PART NOT WORKING    

$results = [];
while($row = mysqli_fetch_array($result,MYSQLI_NUM))
{
    echo "hello";  //never executes?????
    $results[] = $row;

}

//$results has all that you need
print_r($results);  //empty array??? 




 $testClosed = mysqli_close($testConnection);



 if ($testClosed) {
     echo "closed";
 }


?>

1 个答案:

答案 0 :(得分:0)


while($row = mysqli_fetch_array($result,MYSQLI_NUM))
{
    echo "hello";  //never executes?????
    $results[] = $row;
}

尝试从此处删除while语句。数组将与mysqli_fetch_array一起提供给您。所以就这样写:

 $row = mysqli_fetch_array($result,MYSQLI_NUM);
 $results[] = $row;

您可以仅使用$ row [...]访问索引,但是仍然取决于您。

另一方面:

在该函数的文档中有这样的声明: Return Value: Returns an array of strings that corresponds to the fetched row. NULL if there are no more rows in result-set 。因此,您可能正在从数据库中调用无效的表。