Jaswinder Posted May 16, 2014 Share Posted May 16, 2014 hi i am trying to fetch all my data from table here is the code function premiumview($cn,$table) { $table=mysqli_real_escape_string($cn,$table); $result=$cn->query("select * from $table"); if($result->num_rows >0) { while($f=$result->fetch_all(MYSQL_ASSOC)) { $result[]=$f; } return $result; } else { return $result=$cn->error; } } and the error is Fatal error: Cannot use object of type mysqli_result as array ( in while loop line ) What i am doing wrong? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 16, 2014 Share Posted May 16, 2014 (edited) the error is because you are reusing the $result variable. why do you have a loop in your code? the ->fetch_all() method fetches all the rows. Edited May 16, 2014 by mac_gyver Quote Link to comment Share on other sites More sharing options...
Solution Jaswinder Posted May 16, 2014 Author Solution Share Posted May 16, 2014 (edited) hey thanks for reply its working now , just removed the loop if($result->num_rows >0) { $f=$result->fetch_all(); return $f; } Edited May 16, 2014 by Jaswinder Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 16, 2014 Share Posted May 16, 2014 The code generally doesn't make sense. Why do you regard an empty result set as an error? Empty tables are perfectly normal, there's nothing inherently wrong about them. What's the point of your mysqli_real_escape_string() stuff? Do you realize that escaping only works for quoted values? In your case, it has no effect whatsoever. Why do you use this weird kind of error handling where you return the error message as a string? Why not use real errors as intended by PHP? Frankly, what's the whole point of this function? Quote Link to comment Share on other sites More sharing options...
Jaswinder Posted May 16, 2014 Author Share Posted May 16, 2014 Thanks Jacques1 for reply and help me to improve my code. I started learning oops few days back, so there may be many mistakes. Firstly, mysqli_real_escape_string, i admit its of no use here , as no user input is there. Secondly, regarding errors, i want to display my own error messages , so i found this method comfortable This function is basically in a class in a separate file, so i am just returning result after fetching from database Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 16, 2014 Share Posted May 16, 2014 (edited) Firstly, mysqli_real_escape_string, i admit its of no use here , as no user input is there. That's not what I meant. Escaping does not work at all when there are no quotes. The point of mysqli_real_escape_string() is to prevent a value from breaking out of a quoted string. When there is no quoted string, you cannot use this function. Secondly, regarding errors, i want to display my own error messages , so i found this method comfortable Passing around error messages as return values is very messy, cumbersome and unclear. How do you know that there was an error when you call the function? Do you check the data type of the return value? Not exactly a good solution. If you want to convert MySQli errors into your own errors, use exceptions. They allow you to catch errors and throw other errors instead: <?php // make MySQLi throw an exception when there's an error $mysqli_driver = new mysqli_driver(); $mysqli_driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT; $database = new mysqli('localhost', 'user', 'password', 'db'); // catch a MySQLi exception and throw your own exception instead try { $database->query('THIS IS NOT VALID'); } catch (mysqli_sql_exception $test_exception) { throw new MyOwnException('This is my own exception. The original error message was: ' . $test_exception->getMessage()); } class MyOwnException extends Exception { } You didn't answer my first question: Why is an empty table an error for you? Edited May 16, 2014 by Jacques1 Quote Link to comment Share on other sites More sharing options...
Jaswinder Posted May 16, 2014 Author Share Posted May 16, 2014 I got your first point , there is no quoted string, so i will not use this function Regarding error handling, this is new to me, i will surf the web more for this, Thanks for bringing this thing in notice. Lastly, i was not trying to make empty table as error, just trying to print an error, if any occurred . This will also get clear after learning error handling Thanks a lot Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted May 16, 2014 Share Posted May 16, 2014 Lastly, i was not trying to make empty table as error, just trying to print an error, if any occurred . Well, but that's not what the code does. You check if there are any rows. And if there aren't, you jump to the error handling procedure. In other words, even perfectly valid queries are treated as errors only because they have an empty result set. Yes, exceptions will fix this, because you no longer have to manually check for errors. MySQLi will do this for you. However, if you sticked to your old approach, I guess what you actually wanted is this: <?php $result = $cn->query('...'); // Check the return value: If and only if it's false, there was an error. if ($result === false) { // now the error handling } else { return $result->fetch_all(MYSQLI_ASSOC); } But like I said, this is no longer needed. Quote Link to comment Share on other sites More sharing options...
Jaswinder Posted May 16, 2014 Author Share Posted May 16, 2014 ok, i give this a try Quote Link to comment 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.