mysqli_fetch_array(): Argument #1 must be of type mysqli_result

Cryptic eh?

Head to forums and try to find the answer?

The main problem is, it could be one of a bazillion things. The good news is that MySQLi includes some fantastic error reporting. The bad news – it doesn’t happen automagically. Keep on reading to find out how to turbo charge your debugging skills by getting an error message that you can work with a lot better.

MySQLi Connection

We’ll presume at this stage that you’ve got your MySQLi connection statement embedded and it’s all going well. Connection complete.

$mysqli = new mysqli("localhost","my_user","my_password","my_db");

Parameter one is usually fine to leave (assuming you’re doing some local development on your machine or you’re executing your PHP code on the same server that your database is hosted on).

Parameters two and three are your username and password – ensure that any user you have added has appropriate permissions for what you want it to do.

Parameter four is the name of your database table.

Now, you didn’t just leave you connect statement like that did you?

Hopefully, it’s something more like this:

<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");

// Check connection
if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}
?>

That’s a load more code I hear you say? YES! But it sure makes life a lot easier. If you get an errors now:

  1. Your code won’t continue to execute as if nothing is wrong if you’ve either made a syntax error or a parameter error. Equally, even after you’ve tested that the code works perfectly, it’ll also help track down any future errors relating to connection to your database such as the DB server being stopped or going down.
  2. Error handling is the most key part of the software and web development. Trust me, I’ve seen a lot of boo-boos over 10 years working in development teams to say that a little bit of error handling can go a long way. It stops servers crashing, code falling over (even worse, silently) and lots of head-scratching trying to solve a seemingly mysterious problem.

Now we’re ready to move onto the main event error. If you post the above error on a forum you’re unlikely to get the fix you desire as, frankly, it’s not been narrowed down enough. Just as error handling is important during connection, it’s also vitally important throughout the rest of your code. Thankfully, the fix is simple…

Always before your MySQLi connect

The first statement you right for your database code should be this one:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

Always, always, always write that statement first. It’s the basis of you (and others) not going insane whilst coding and using MySQLi and will also:

  1. Give you error messages that give you half a chance of fixing your problem.
  2. Even if it doesn’t, it gives you error messages that you give you half a chance getting an appropriate answer for fixing your problem whether you reach out to a co-worker, friendly web developer or on a web development enthusiast forum online.

But the tutorials never start with that!

Correct. They don’t. Maybe they should. I certainly think they should and anyone who’s worked in web development for a few weeks or more probably wish they did!

I mean, it’s easy to understand why. The tutorials that you start with are possibly (probably) aimed at novices. Much to the annoyance of web development pros, any type of error handling is seen as unnecessary fluff. It distracts from the main event and when everything is peachy, it feels like it’s for no benefit. As you can see with the mysqli_connect code above, it transforms a single one liner into eight lines. And, as you’re following a tutorial and everything has been tested to death for you, excluding any typos or misconfigured installs it’s likely to work just fine for you. It doesn’t offer any visible benefit – indeed you can’t even really tell it’s there when looking at the code when everything is working. However, most of your time as a web developer is likely to be spent on maintenance and fixing things rather than the excitement of creating brand new things. Fixing broken things is also the most stressful part of development that I have encountered.

When things go wrong, rather than having to deal with some pseudo-cryptic techno-hoopla message on the command line you can get a snazzy PHP error with full stack trace that you can investigate which should solve your woes not only for a dreaded “mysqli_fetch_array(): Argument #1 must be of type mysqli_result” error but also any other weird and wonderful errors you may come across.

P.S. You’ll come across loads!

Happy coding!

You may also like...

2 1 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x