-1

First off, I'm very new to PHP and my English sometimes isn't that great, so I already apologize for the guesswork you guys may have to do.

The error I get is:

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

The code I have is:

mysql_connect("nope.com","nope","nope") or die ("Verbindung zur Datenbank konnte nicht hergestellt werden");
mysql_select_db("nope") or die ("Datenbank konnte nicht verbunden werden");
$nnamenow = $_SESSION['username']; 
echo(mysql_fetch_object(mysql_query("SELECT Name FROM USER WHERE Username LIKE '$nnamenow'")));

I have tried quite a number of different approaches with query-variables and so on, because until now rewriting code enough times in different ways always solved my issues.

halfer
  • 19,824
  • 17
  • 99
  • 186
R3alr0ad
  • 3
  • 2

4 Answers4

1

That's error is very clear, You're trying to echoing an object.

$result = mysql_fetch_object(mysql_query("SELECT Name FROM USER WHERE Username LIKE '$nnamenow'"));
var_dump($result); // echo $result->Name;

Waring: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rikesh
  • 26,156
  • 14
  • 79
  • 87
  • thank you, ill have a look at it. But honestly while the error is pretty clear do i still not understand it. The type of "data" im trying to echo from the database is varchar :/ – R3alr0ad Apr 21 '14 at 09:32
  • 1
    @R3alr0ad While you only select a single field `Name` from the database, that is irrelevant to the mysql API. You could also `SELECT Name, Foo, Bar, Baz`. What would you expect the result of an `echo` to be then? – deceze Apr 21 '14 at 09:39
  • @deceze I think i see what you mean. Im not echoing the data itself, im echoing the object it is. ( i hope my broken english will get across what i mean) Thanks for the patience btw. As i said, im still very new to PHP ( and coding in general ) – R3alr0ad Apr 21 '14 at 09:44
1

mysql_fetch_object returns an object. You then try to echo said object. You cannot echo an object, it will produce the error you see. You can only echo properties of said object.:

$obj = mysql_fetch_object(...);
echo $obj->Name;

You should also add a lot more error handling to your code and most of all stop using the mysql_ functions; they're deprecated. Not everything has to be a one-liner.

deceze
  • 510,633
  • 85
  • 743
  • 889
1

Try save query result to variable like

$result = mysql_query("SELECT Name FROM USER WHERE Username LIKE '$nnamenow'");

then You can iterate by row

while($row = mysql_fetch_assoc($result)) {
    echo $row['Name'];
}

because this querty can return more than one row :)

Line
  • 1,529
  • 3
  • 18
  • 42
  • Thanks for accept my answer, You can also vote up instead of write such comments :P Obviously You should also use mysqli, as anothers mentioned. – Line Apr 22 '14 at 07:02
0

in PHP you can't echo an array or an object. Well, for objects, that's not entirely true, as you may implement the __toString() method, which lets you manage how to display an object whenever you need to convert it to a string.

In your case, your object is an instance of StdClass, so it won't work. You need to do a loop to display each field, or display each field separately ($result->Name, for example).

Charles Sarrazin
  • 801
  • 7
  • 13