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
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)?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.