mysqli_fetch_array()不产生数组

问题描述 投票:0回答:1

我似乎无法将结果集放入数组,然后使用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";
 }


?>
php arrays wordpress resultset
1个回答
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。因此,您可能正在从数据库中调用无效的表。

© www.soinside.com 2019 - 2024. All rights reserved.