PHP MYSQLi:数组到字符串转换

时间:2015-06-23 18:19:07

标签: php mysql mysqli

我想获取表的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>
&#13;
&#13;
&#13;

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

2 个答案:

答案 0 :(得分: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/>"; 
}

注意: - 这是因为$resresult-set array object,其值大于或等于1。所以你需要像这样迭代。

答案 1 :(得分:0)

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

$id = $pid['id'];

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