【问题标题】:PHP MYSQLi: Array to string conversion [duplicate]PHP MYSQLi:数组到字符串的转换[重复]
【发布时间】:2015-09-09 16:59:33
【问题描述】:

我想获取表的 id 并将其设为变量,以便在链接中使用它。

但我收到了通知:数组到字符串的转换。

$getpID = "SELECT id from posts";
$res = mysqli_query($conn, $getpID) or die(mysqli_error()); 
$pid = mysqli_fetch_assoc($res);

<a id='del' href='deletepost.php?del=$pid'>Delete</a>

这只是我遇到问题的代码的一部分。

【问题讨论】:

  • SELECT id from posts 很可能会返回多个记录,mysqli_fetch_assoc 正在将该结果转换为关联数组,您需要对其进行迭代或定位数组。

标签: php mysql mysqli


【解决方案1】:

你需要这样做:-

<?php
$getpID = "SELECT id from posts";
$res = mysqli_query($conn, $getpID) or die(mysqli_error($conn)); 
while($pid = mysqli_fetch_assoc($res)){
echo "<a id='del' href='deletepost.php?del=$pid['id']'>Delete</a><br/>"; 
}

注意:- 这是因为 $res 是一个 result-set array object 具有 1 或大于值。所以你需要像这样迭代。

【讨论】:

    【解决方案2】:

    $pid 是一个数组,你需要从数组中访问ID,然后你才能在链接中使用它。 示例:

    $id = $pid['id'];
    
    <a id="del" href="deletepost.php?del=$id">Delete</a>
    

    【讨论】:

      猜你喜欢
      • 2016-04-28
      • 1970-01-01
      • 2021-07-15
      • 2012-08-07
      • 1970-01-01
      • 2016-06-26
      • 1970-01-01
      • 1970-01-01
      • 2014-07-11
      相关资源
      最近更新 更多