因为我从 MySQL 转换为 PDO,我收到错误,在我转换为 PDO 之后,大部分行都解决了,但其中一个与字符串有关...我是
得到错误提示
Array to string conversion in C:header.php on line 573
我遇到的错误是
<?php if($msg_cnt>0) { echo '('.$msg_cnt.')'; }?></i></a>
请查看最后两行的代码...我该如何解决!
<?php if(!isset($_SESSION['account_type'])){?><a href="#"><?php }?>
<?php if(isset($_SESSION['account_type'])){
$msg_cnt_q = $conn ->query("select * from messages where receiver_id='".$_SESSION['user_id']."' and receiver_read_status='unread'");
$msg_cnt = $msg_cnt_q->fetch(PDO::FETCH_ASSOC);?>
<a href="http://www.xxxxxx/inbox.php">
<i class="fa fa-bell fa-lg fa-color" style="padding-top:17px;">
<?php if($msg_cnt>0) { echo '('.$msg_cnt.')'; }?></i></a>
<?php }?>
感谢您的宝贵时间....
最佳答案
当您尝试打印本应具有单个标量值的数组时,该错误是一个常见的 PHP 错误。就像将值与其他字符串连接一样。
PDO 函数 fetch()
返回一个包含一行数据的数组,而不是结果集中的行数。
PDO 提供了一个不同的函数 rowCount()
所以你可以知道有多少行。
$row = $msg_cnt_q->fetch(PDO::FETCH_ASSOC);
$msg_cnt = $msg_cnt_q->rowCount();
关于mysql - PDO 返回结果的数组到字符串转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41494309/