php - Can't print stdClass property -


in following example, i'm trying print file_already_exists inside string, query, error:

catchable fatal error: object of class stdclass not converted string in ... 

$db_items = (object) [     "cover" => (object) [         "file_already_exists" => 0, // can't print     ],     "file_already_exists" => 0, // can print ];  $str = "insert albums (file_on_system) values ('$db_items->cover->file_already_exists')";  echo $str; 

using $db_items->file_already_exists works fine, not $db_items->cover->file_already_exists. why that? there way print 1 in cover?


in simpler way

echo "$db_items->file_already_exists"; // works echo "$db_items->cover->file_already_exists"; // doesn't work 

$str = "insert [...] values ('$db_items->cover->file_already_exists')"; 

the parser not know variable name ends, tries insert $db_items string - , causes conversion issue.

either use string concatenation

$str = "insert albums [...] values ('".$db_items->cover->file_already_exists."')"; 

or complex (curly) syntax,

$str = "insert albums [...] values ('{$db_items->cover->file_already_exists}')"; 

depending on value originates, not neglect sql injection!


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -