如何显示具有多行php的两列

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

我采用了此示例中给出的代码:

https://www.w3schools.com/php/php_mysql_select.asp

这是来自Phpmyadmin的表格。

表格:帖子

-------------------------------------------------------
|  id  |   a   |   b   |   c   |   d   |   e   |  .... 
-------------------------------------------------------
|  1   |  AA1  |   BB2 |   CC3 |  DD4  |   EE5 |  .... 
-------------------------------------------------------

代码如下(PHP):

$query = "SELECT * FROM `post` WHERE id ORDER BY id ASC"; 
$result = mysqli_query($db, $query);
while($row = mysqli_fetch_array($result))  { 
    echo $row["a"];  // Out: AA1
    echo $row["b"];  // Out: BB2
    echo $row["c"];  // Out: CC3
    echo $row["d"];  // Out: DD4
    echo $row["e"];  // Out: EE5
    .
    .
    .
}

[当我向数据库中添加一列(phpmyadmin)或导入信息(sql)时,它给出了一条错误消息,其消息如下:

Error:
SQL query: 
MySQL said: Documentation
#1118 - Row size too large (> 8126). Changing some columns to TEXT or BLOB may help. 
In current row format, BLOB prefix of 0 bytes is stored inline.

我用上面的代码更改了表,如下所示:($ data)

----------------------------
|  id  |  name   |  value  |
----------------------------
|  1   |    a    |   AA1   |
----------------------------
|  2   |    b    |   BB2   |
----------------------------
|  3   |    c    |   CC3   |
----------------------------
|  4   |    d    |   DD4   |
----------------------------
|  5   |    e    |   EE5   |
----------------------------
.
.
.

现在修改的代码如下:

$query = "SELECT * FROM `post` (`name`,`value`) WHERE id ORDER BY id ASC"; 
$result = mysqli_query($db, $query);
$data = [];
while($row = mysqli_fetch_array($result))  { 
    echo $data[$row['value']]['a'];  // out : AA1
    echo $data[$row['value']]['b'];  // out : BB2
    echo $data[$row['value']]['c'];  // out : CC3
    echo $data[$row['value']]['d'];  // out : DD4
    echo $data[$row['value']]['e'];  // out : EE5
    .
    .
    .
}

当我刷新页面并显示错误消息时,其消息如下:

Notice: Undefined index: AA1
Notice: Undefined index: BB2
Notice: Undefined index: CC3
Notice: Undefined index: DD4
Notice: Undefined index: EE5

可能是什么原因,我在做什么错?

php mysql
1个回答
0
投票

change:$query = "SELECT * FROM post(name,value ) WHERE id ORDER BY id ASC";

收件人:

$query = "SELECT * FROM `post` WHERE id = id  ORDER BY id ASC"; 
 $result = mysqli_query($db, $query);
 $data = array();
     while($row = mysqli_fetch_array($result))  {
       $data[] = $row;
     }

我打赌您不知道如何将参数传递给where子句,并且您可能不需要查询。因此,尝试这样:

$query = "SELECT * FROM `post` ORDER BY id ASC"; 
$result = mysqli_query($db, $query);
$data = array();
     while($row = mysqli_fetch_array($result))  {
       $data[] = $row;
     }
© www.soinside.com 2019 - 2024. All rights reserved.