Jump to content

Fetching error in while loop , OOPs


Jaswinder
Go to solution Solved by Jaswinder,

Recommended Posts

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?

Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 by Jacques1
Link to comment
Share on other sites

  • 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 :)

Link to comment
Share on other sites

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.

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.