数组到字符串的转换

数组到字符串的转换

This question already has answers here:
Notice: Array to string conversion in
(6个答案)
四年前关闭。
我想得到一个表的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>

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

最佳答案

你需要这样做:-

<?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是一个具有1个或多个值的result-set array object。所以你需要这样迭代。

关于php - PHP MYSQLi:数组到字符串的转换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31010503/

10-09 20:10