dean7 Posted April 20, 2010 Share Posted April 20, 2010 Hi all, Im trying to code a blacklist for my website but ive came accross a error which says: Catchable fatal error: Object of class stdClass could not be converted to string in /home/www/*********.com/cashbl.php on line 33 This is my code: <?php // Check errors thing: ini_set ('display_errors', 1); error_reporting (E_ALL); // End check errors thing. ?> <?php session_start(); // Start inculdes files: include ("includes/config.php"); include ("includes/functions.php"); // End includes files. // Login thing: if(!isset($_SESSION['username']) || !isset($_SESSION['password'])){ header("Location: index.php"); }else{ $fetch_users_data = mysql_fetch_object(mysql_query("SELECT * FROM `users` WHERE username='".$_SESSION['username']."'")); } // end login thing. $username = $_SESSION['username']; $getmoney = mysql_query("SELECT username FROM users ORDER BY money DESC"); $grr = mysql_fetch_object($getmoney); $account = mysql_query("SELECT money FROM users ORDER BY money DESC"); $ammount = mysql_fetch_object($account); echo "<ul>"; $i = 0; do { $i++; echo "<table width='50%' class='table' cellpadding='0' cellspacing='0'><tr><td class='header' colspan='4'><center>Money Blacklist</center></td></tr> <tr><td colspan='1' class='forum'>Place:</td><td colspan='1' class='forum'>Username:</td><td colpan='1' class='forum'>Money:</td></tr> <tr><td>{$i}</td><td><a href='profile.php/view_user={$grr}'>{$grr}</td><td>£{$fetch_users_data->money}</td></tr></table>"; // Line 33 } while($i < 10); echo "</ul>"; ?> <link href="regstyle.css" rel="stylesheet" type="text/css"> <body class="body"> </body> Anyone know why its displaying that errror? Thanks in advance . Quote Link to comment https://forums.phpfreaks.com/topic/199164-catchable-fatal-error-in-code/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 20, 2010 Share Posted April 20, 2010 $grr is an object (as in $grr = mysql_fetch_object($getmoney) but you are trying to put it into a quoted string. I suspect that you wanted to use - $grr->username Quote Link to comment https://forums.phpfreaks.com/topic/199164-catchable-fatal-error-in-code/#findComment-1045321 Share on other sites More sharing options...
Mchl Posted April 20, 2010 Share Posted April 20, 2010 $grr = mysql_fetch_object($getmoney); $grr is an object of stdClass type. It has no __toString() so it can not be echoed. Quote Link to comment https://forums.phpfreaks.com/topic/199164-catchable-fatal-error-in-code/#findComment-1045324 Share on other sites More sharing options...
dean7 Posted April 20, 2010 Author Share Posted April 20, 2010 Yeah thanks that worked . But now my code isnt working as it should.. Its displaying the username of the account your on.. not the username who has the most cash. Same with the money. Anyone see why? Quote Link to comment https://forums.phpfreaks.com/topic/199164-catchable-fatal-error-in-code/#findComment-1045327 Share on other sites More sharing options...
PFMaBiSmAd Posted April 20, 2010 Share Posted April 20, 2010 You are outputting $fetch_users_data->money, which is the money of the account you are on. I suspect you wanted to use $ammount->money You seem to be just throwing down some code without even looking at it to see if it is doing what you want. You are also executing two similar queries to get two pieces of data that a single query can return. You are also not doing anything inside of the do{}while() loop to get different data so the same data will be displayed 10 times. You can also use a LIMIT 10 in your query so that you only get the number of rows you are interested in. You can then using a simple while(){} loop. Quote Link to comment https://forums.phpfreaks.com/topic/199164-catchable-fatal-error-in-code/#findComment-1045340 Share on other sites More sharing options...
PFMaBiSmAd Posted April 20, 2010 Share Posted April 20, 2010 Based on the code in the first post in this thread, the following is the equivalent (with unused code removed and the two queries combined into one) - <?php // Check errors thing: ini_set ('display_errors', 1); error_reporting (E_ALL); // End check errors thing. ?> <?php session_start(); // Start inculdes files: include ("includes/config.php"); include ("includes/functions.php"); // End includes files. // Login thing: if(!isset($_SESSION['username']) || !isset($_SESSION['password'])){ header("Location: index.php"); exit; // prevent the remainder of the code on the page from being executed } ?> <link href="regstyle.css" rel="stylesheet" type="text/css"> <body class="body"> <?php $result = mysql_query("SELECT username, money FROM users ORDER BY money DESC LIMIT 10"); $row = mysql_fetch_object($result); echo "<ul>"; $i = 1; while($row = mysql_fetch_object($result)){ echo "<table width='50%' class='table' cellpadding='0' cellspacing='0'><tr><td class='header' colspan='4'><center>Money Blacklist</center></td></tr> <tr><td colspan='1' class='forum'>Place:</td><td colspan='1' class='forum'>Username:</td><td colpan='1' class='forum'>Money:</td></tr> <tr><td>{$i}</td><td><a href='profile.php/view_user={$row->username}'>{$row->username}</td><td>£{$row->money}</td></tr></table>"; $i++; } echo "</ul>"; ?> </body> Quote Link to comment https://forums.phpfreaks.com/topic/199164-catchable-fatal-error-in-code/#findComment-1045347 Share on other sites More sharing options...
PFMaBiSmAd Posted April 20, 2010 Share Posted April 20, 2010 Edit to the above: There is an extra $row = mysql_fetch_object($result); statement left over from modifying the code that would need to be removed. Quote Link to comment https://forums.phpfreaks.com/topic/199164-catchable-fatal-error-in-code/#findComment-1045358 Share on other sites More sharing options...
dean7 Posted April 20, 2010 Author Share Posted April 20, 2010 Yeah thanks that worked. just I got to make it into one table now. But the main part is sorted. Once agian thanks Quote Link to comment https://forums.phpfreaks.com/topic/199164-catchable-fatal-error-in-code/#findComment-1045400 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.