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