PHP MySQLi : Array to string conversion

标签 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>

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

最佳答案

你需要这样做:-

<?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 个或多个值。所以你需要像这样迭代。

关于PHP MySQLi : Array to string conversion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31010503/

相关文章:

PHP:将 Google 的 SMTP 中继与 SwiftMailer 结合使用

php - 批量操作不适用于 Magento 后端网格序列化程序

javascript - 带有隐藏按钮的登录脚本

mysql - 在新模型上设置 ID

sql - 如何反转 MySQL 中的默认顺序?

mysql - 使用 HAVING 条件时遇到 COUNT 问题

php - mysqli_fetch_assoc()需要参数/调用成员函数bind_param()错误。如何获取并修复实际的mysql错误?

php - fatal error : Uncaught exception 'mysqli_sql_exception' with message 'No index used in query/prepared statement'

php - mysqli_fetch_assoc()需要参数/调用成员函数bind_param()错误。如何获取并修复实际的mysql错误?

php - mysql(i)_real_escape_string,安全可靠吗?