2

i have been steadily developing my website until i got this error:

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

I have been at it for three days now i still don't know what is causing this, this is the first time i have encountered this error so i do not really know how to solve and would really appreciate some help on this.

This is my PHP code:

<?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'];
  }
?>

I use mysql and the datatype for that column is 'text'.

This is the table structure:

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

There are other columns but i have omitted them since they have nothing to do with the result.

This is the vardump result:

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" }
5
  • 1
    You dont have column "user_post" in your page_posts table. Commented Nov 25, 2013 at 11:15
  • Oh but i do, i have already double checked that. Commented Nov 25, 2013 at 11:15
  • Provide table structure please, as well as let us know which line is line 15, is it $row['user_post'] because we can guess, but knowing helps Commented Nov 25, 2013 at 11:16
  • 5
    mysqli::fetch_assoc fetches a single row. Your foreach iterates over the fields in that row. Commented Nov 25, 2013 at 11:16
  • var_dump($row) to see what you're trying to treat as an array here. Commented Nov 25, 2013 at 11:19

2 Answers 2

4

You should iterate over the rows and not the colums of the same row.

<?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'];
  }
?>
Sign up to request clarification or add additional context in comments.

Comments

2

It's very late for the reply but still i would like to share this.. as you are using

foreach() loop

therefore

$post_rows must be an array. you are using it as variable.

so just use the following code

$post_arr[] = $db->query($sql);
$post_rows[] = $post_arr->fetch_array();

everything else is perfect.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.