1

I am making a simple blog in php and getting this error -

Illegal string offset 'name' in C:\xampp\htdocs\simple\index.php on line 28 and 29

  <?php
    foreach ( $posts as $post ) {
    if ( ! category_exists('name', $post ['name'] ) )  {   //line 28
                $post['name'] = 'Uncategorised';   
                }
                ?>

I know the string offset questions have been asked here a lot but still I could not figure out how to fix this one. Please help.

Here is the function category_exists

 function category_exists($field,$value)
 {
  $field=mysql_real_escape_string($field);  
  $value=mysql_real_escape_string($value);  

  $query=mysql_query("SELECT COUNT(1) FROM categories WHERE {$field}= '{$value}'");
   echo mysql_error();

    return (mysql_result($query,0)=='0')? false : true;


   }

This part of code hopefully will help you understand the $posts variable:

$posts=get_posts();

  function get_posts($id=null,$cat_id=null){
   $posts=array();
   $query=mysql_query("SELECT posts.id AS post_id, categories.id AS      category_id, title, contents, date_posted, categories.name FROM posts INNER JOIN categories ON categories.id = posts.cat_id ORDER BY post_id DESC");
   while($row=mysql_fetch_assoc($query))
   {
   $posts=$row; 
   }
   return $posts;
   }

I have the following attributes of posts table -
id,title,cat_id,contents,date_posted.
For the categories table:
id and name.

4
  • 1
    show the full code. according to yours $posts is undefined and u should at first get another error like 'invalid ..somt.. for foreach' Commented Sep 13, 2015 at 4:36
  • It looks like there is a space between $post and ['name'], could that be causing issues? Commented Sep 13, 2015 at 4:37
  • its not solve your problem. but you certainly shoult replace $posts=$row; whith $posts[]=$row; in last code-block Commented Sep 13, 2015 at 5:06
  • @M0rtiis Thanks man it worked Commented Sep 13, 2015 at 6:41

1 Answer 1

2

Your variable $post probably is not an array. Try this first:

foreach ( $posts as $post ) {
    var_dump($post);
}

If you see that it is an array, great! Use it as an array. It is very likely that $post will be a string. Therefore when you try to use $post['name'], PHP tells you that 'name' is not an offset for $post string. Had you used $post[0], that'd give you the first character and 0 would be legal offset...but 'name' wouldn't be a legal offset.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried your way and It's giving me this output -string(1) "3" string(1) "1" string(5) "first" string(5) "fdsdf" //which means its an array
No, it's not an array. Each of those 4 items is a string. string(1) means string of length 1. "first" is a string of 5 character length and therefore it shows string(5).

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.