I dont know why but I get this error
:
Catchable fatal error: Object of class stdClass could not be converted to string
For this code:
$sql = "SELECT * FROM player ORDER BY score DESC LIMIT $begin";
$arr = array();
while($obj = mysql_fetch_object(mysql_query($sql))) {
//$arr[] = $obj;
echo $obj;
}
You’re using
mysql_fetch_object(which returns an object) and then trying to output it as a string. That won’t work.In your case, you should use a function that is capable of printing the contents of the object. There are many but the most straight-forward ones are
print_rorvar_dump. if you’re outputting in an HTML context, you might wan to wrap a<pre>tag around the output to make it more readable or click “View source” in your browser.If you’re writing your own objects, they can also be “converted” to strings by implementing the
__toString()magic methodAlso, as people have said in the comments, your code will run the query on each pass through the loop. Check out the documentation here and read the examples.
Note: You might have simplified the example for SO’s sake, but make sure to sanitize your variables (e.g.
$begin) to avoid SQL injections!