Jump to content

[SOLVED] Catchable error?


YourNameHere

Recommended Posts

I have a select query. Then I have a that query populate a mysql_fetch_object().

I realized that the fetch object wasn't being populated. So I echoed it out and here are the results:

 

Catchable fatal error: Object of class stdClass could not be converted to string in...

 

Here is the relevant code:

 

$link = mysql_connect($server, $db_user, $db_pass);
mysql_select_db($db);
$cname = "Cody"; 
$getSkills = "Select * FROM skills where cname = '$cname'";
$b = mysql_query($getSkills);
$a = mysql_fetch_object($b);

...

echo $a;

 

I know that stdClass is the base class that all other classes extend in php. This is a new error that I haven't seen before.

Link to comment
https://forums.phpfreaks.com/topic/178810-solved-catchable-error/
Share on other sites

You cannot echo an object. Simple. And mysql_fetch_object always returns a stdClass unless you pass it a class via its second arg.

 

You need to debug your call to mysql_query.

 

So, you're saying that the query isn't returning any results and the is why the fetch_object is returning an object instead of an array (which I would be able to echo)?

So, you're saying that the query isn't returning any results and the is why the fetch_object is returning an object instead of an array (which I would be able to echo)?

 

No. As the name suggests mysql_fetch_object returns an object, mysql_fetch_array returns an array. Either way, you cannot simply echo an array either.

 

You can either use print_r to view the objects (or arrays) contents, or loop through its properties / indexes.

Then how would I reference a column from the result if I use fetch_array?

 

$a->columnName ?

Because that isn't working.

 

Because array indexes are referenced via [], you access an objects properties using ->

 

$a['columnName']

 

This really is beginners stuff and should probably be learnt well before trying to deal with databases.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.