php - 错误 : Illegal String Offset in PHP

标签 php mysql

我一直在稳步开发我的网站,直到我遇到这个错误:

Warning: Illegal string offset 'user_post' in C:\xampp\xxxxxx\xxxx.php on line 15

我已经处理了三天了,我仍然不知道是什么原因造成的,这是我第一次遇到这个错误,所以我真的不知道如何解决,非常感谢对此的一些帮助.

这是我的 PHP 代码:

<?php
  $db = new mysqli("xxxxxxxxxxxxxxx", "xxxxxxxx", "xxxxxxxxxxxxxx", "xxxxxxxxx");
  if($db->connect_errno > 0) {
    die('Unable to connect to database [' . $db->connect_error . ']');
  }

  $sql = "SELECT * FROM page_posts";
  $post_arr = array();
  $post_arr = $db->query($sql);
  $post_rows = $post_arr->fetch_array()

  foreach($post_rows as $row)
  {
    echo $row['user_post'];
  }
?>

我使用 mysql,该列的数据类型是“文本”。

这是表结构:

post_id    int
user_id    int
post_title int
user_post  text
post_date  datetime
post_page  varchar(32)

还有其他列,但我省略了它们,因为它们与结果无关。

这是 vardump 结果:

array(24) { [0]=> string(1) "3" ["post_id"]=> string(1) "3" [1]=> string(1) "0" ["user_id"]=> string(1) "0" [2]=> string(13) "My First Post" ["post_title"]=> string(13) "My First Post" [3]=> string(329) "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse tincidunt neque in erat vestibulum, sed gravida odio venenatis. Nam ut nunc libero. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Phasellus volutpat ultricies enim. Nullam luctus odio urna, vitae posuere justo semper in." ["user_post"]=> string(329) "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse tincidunt neque in erat vestibulum, sed gravida odio venenatis. Nam ut nunc libero. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Phasellus volutpat ultricies enim. Nullam luctus odio urna, vitae posuere justo semper in." [4]=> string(19) "2013-11-22 00:00:00" ["post_date"]=> string(19) "2013-11-22 00:00:00" [5]=> string(12) "Post-an-idea" ["post_page"]=> string(12) "Post-an-idea" [6]=> NULL ["additional_details"]=> NULL [7]=> string(1) "0" ["up_votes"]=> string(1) "0" [8]=> string(1) "0" ["down_votes"]=> string(1) "0" [9]=> NULL ["voted_users"]=> NULL [10]=> string(1) "1" ["is_valid"]=> string(1) "1" [11]=> string(6) "active" ["post_status"]=> string(6) "active" }

最佳答案

您应该遍历行而不是同一行的列。

<?php
  $db = new mysqli("xxxxxxxxxxxxxxx", "xxxxxxxx", "xxxxxxxxxxxxxx", "xxxxxxxxx");
  if($db->connect_errno > 0) {
    die('Unable to connect to database [' . $db->connect_error . ']');
  }

  $sql = "SELECT * FROM page_posts";
  $post_arr = array();
  $post_arr = $db->query($sql);

  while ($row = $post_arr->fetch_assoc())
  {
    echo $row['user_post'];
  }
?>

关于php - 错误 : Illegal String Offset in PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20190760/

相关文章:

php - 当本地日期格式不同时处理 mySQL 日期格式

php - 无法使用嵌套的 while 循环执行准备好的语句

php - 良好的 CRM 商业或开源

php - 如何将数组元素传递给ajax?

PHP DOMDocument - 为什么破折号 "–"转换为 –

php - Laravel MethodNotAllowedHttpException 无消息 - 尝试插入 mysql 表

PHP mySQL ~回显表和列未正确显示

php - Symfony 3 找不到 pdo_mysql 驱动程序

javascript - Ajax 错误: Failed to load resource: the server responded with a status of 500 (Internal Server Error)

Php mysql 更新一个新的唯一 id