MySQL PHP未定义偏移量错误

时间:2016-10-11 23:39:45

标签: php mysql

我有一个名为 lynked_v1 的表,MySQL中的一个名为 probability_single_free

的列
   +----------------------------------+
   | id  |   probability_single_free  | 
   | ---------------------------------|
   | 0   |         100.00             |
   | 1   |         100.00             |
   | 2   |         100.00             |
   | 3   |         100.00             |
   | 4   |         100.00             |
   | 5   |         100.00             |
   | 6   |         100.00             |
   | 7   |         100.00             |
   +----------------------------------+

我正在尝试使用以下命令回显每一行:

<?php
error_reporting(-1);
ini_set('display_errors', true);

require_once ('/var/www/html/MySQL/mysqli_connect.php');

echo "Connected successfully";

$query = "SELECT probability_single_free FROM lynked_v1";

$response = @mysqli_query($dbc, $query);

while($row = mysqli_fetch_array($response)) {

    echo $row[0];
    echo $row[1];
    echo $row[2];
    echo $row[3];
    echo $row[4];
    echo $row[5];
    echo $row[6];
    echo $row[7];

}

?>

$ row [0]在屏幕上打印好了。但是我在其余行中遇到以下错误:

Notice: Undefined offset: 1 in /var/www/html/index.php on line 56
Notice: Undefined offset: 2 in /var/www/html/index.php on line 57
Notice: Undefined offset: 3 in /var/www/html/index.php on line 58
Notice: Undefined offset: 4 in /var/www/html/index.php on line 59
Notice: Undefined offset: 5 in /var/www/html/index.php on line 60
Notice: Undefined offset: 6 in /var/www/html/index.php on line 61
Notice: Undefined offset: 7 in /var/www/html/index.php on line 62

如何单独打印每一行?

2 个答案:

答案 0 :(得分:0)

使用此代码:

<?php
error_reporting(-1);
ini_set('display_errors', true);

require_once ('/var/www/html/MySQL/mysqli_connect.php');

echo "Connected successfully";

$query = "SELECT probability_single_free FROM lynked_v1";

$response = @mysqli_query($dbc, $query);

while($row = mysqli_fetch_array($response)) {

    echo $row["probability_single_free"];

}

?>

它会像魅力一样工作)

答案 1 :(得分:0)

你误解了while($row = mysqli_fetch_array($response))是如何运作的。基本上,这将逐行循环遍历所有数据库结果,并且每次将变量放入名为$row的数组中。

结果行中的每一列都是键控数组中的值。如果您选择的只有一列(如您的示例所示),则每个循环中仅使用$row[0]

当数据库的行循环播放时,您只需在循环内echo $row[0]打印所有值。

while($row = mysqli_fetch_array($response)) {
    echo $row[0];
}