Jump to content
Sign in to follow this  
cfrost

Query Failing With No Error Message (mysqli)

Recommended Posts

I haven't been using PHP for a while now so this is my first time trying to test with mysqli. I believe I am getting connected properly as my error message does not display. This php code is on the same server as the database. I've left out the success case in this code because I know I'm failing before it and this is exactly what's on my live test page:

<?php
require('cfd.php');

$mysqli = new mysqli($dbhost, $dbuser, $dbpassword, $dbdatabase);
if ($mysqli->connecterrno) {
  echo "Failed to connect to MySQL: (" . $mysqli->connecterrno . ") " . $mysqli->connect_error;
}

$testSQL = "SELECT * FROM testTable WHERE id = 1;";

if (!$testResult = $mysqli->query($testSQL)) {
  printf("Error message: %s\n", $mysqli->error);
  exit;
}

$testResult->free();
$mysqli->close();

?>

This is showing the output "Error message:" with no error message afterwards. Is anyone seeing something I've done wrong? I've been staring at this piece of code for 2 hours and I know it's going to be something simple but not having the experience I'm also worried I could just be missing something.

 

*Edit* This query has been run directly on the server in a terminal and it runs successfully.

Thanks!

Edited by cfrost

Share this post


Link to post
Share on other sites

your connection isn't working for some reason, additionally you have misspelled the connect_error property in your connection error detection (you should actually be using exceptions to handle the connection error and all other mysqli statement errors), and you are not seeing any of the php errors that would help you because you don't have php's error_reporting set to E_ALL and display_errors set to ON (preferably in the php.ini on your development system), so all you are seeing the follow-on error of a query that appears to fail, but for which there is no connection to execute the query at all, so there's no ->error property to display.

 

if you set the error_reporting/display_errors as suggested, you will be getting at least the following list of errors - 

 

Warning: mysqli::mysqli(): some mysqli related error here.. the one i got was intentionally triggered and may not be the same as your actual error... in your_file.php on line 4

Notice: Undefined property: mysqli::$connecterrno in your_file.php on line 5

Warning: mysqli::query(): Couldn't fetch mysqli in your_file.php on line 11

Warning: main(): Couldn't fetch mysqli in your_file.php on line 12
Error message: 

 

 

 

 

once you fix the spelling of the connect_error property name on line 5, your connection error handling will work and tell you why the connection didn't work (may repeat the error shown on line 4.) you should also exit; as part of the connection error handling since this is a fatal problem or more simply, just enable exceptions and let php catch them and handle the errors for you.

Share this post


Link to post
Share on other sites

To avoid similar problems in the future, you should

  • abandon mysqli in favor of a database interface that doesn't suck (PDO is a much better alternative)
  • get a proper IDE which analyzes your code and points out obvious mistakes (like when you try to access nonexistent attributes)
  • learn how to handle errors properly (not with echo statements on the website) and possibly fix your PHP configuration as described

Share this post


Link to post
Share on other sites

your connection isn't working for some reason, additionally you have misspelled the connect_error property in your connection error detection (you should actually be using exceptions to handle the connection error and all other mysqli statement errors), and you are not seeing any of the php errors that would help you because you don't have php's error_reporting set to E_ALL and display_errors set to ON (preferably in the php.ini on your development system), so all you are seeing the follow-on error of a query that appears to fail, but for which there is no connection to execute the query at all, so there's no ->error property to display.

 

if you set the error_reporting/display_errors as suggested, you will be getting at least the following list of errors - 

 

 

once you fix the spelling of the connect_error property name on line 5, your connection error handling will work and tell you why the connection didn't work (may repeat the error shown on line 4.) you should also exit; as part of the connection error handling since this is a fatal problem or more simply, just enable exceptions and let php catch them and handle the errors for you.

 

Thanks for the response! I pulled parts of this code from different places and made an assumption that I was taking working code. I will look into exceptions but I just wanted to do this quick and dirty first and figured echo's would suffice for now.

 

After fixing connecterrno to connect_errno (I still have to look into the difference between this and connect_error) I did get an error message that I searched and was able to find out that I couldn't connect to root the way I was trying so I created a new user and it worked!

 

 

 

To avoid similar problems in the future, you should

  • abandon mysqli in favor of a database interface that doesn't suck (PDO is a much better alternative)
  • get a proper IDE which analyzes your code and points out obvious mistakes (like when you try to access nonexistent attributes)
  • learn how to handle errors properly (not with echo statements on the website) and possibly fix your PHP configuration as described

 

 

As I mentioned I am not with the times on PHP so I used mysqli since it was in the first couple examples I found when Googling connections. I'll look into PDO for sure. I'm open to recommendations on IDE but as far as this little snippet goes I was trying to do it quick and dirty so the next step is a proper IDE and learning Exceptions.

 

Thanks again!

Share this post


Link to post
Share on other sites

The thing is that “quick and dirty” in programming often means long and painful. You've now spent at least two days fixing code which you will then throw away and start all over again. To me, that sounds pretty frustrating.

 

Alternatively, you could have spent, say, an hour on comparing the different interfaces, learning the basics of PDO and then writing code which you can actually keep. Not only is this rewarding; you'll definitely learn more from this than from chasing a misspelled attribute for two days.

Share this post


Link to post
Share on other sites

The thing is that “quick and dirty” in programming often means long and painful. You've now spent at least two days fixing code which you will then throw away and start all over again. To me, that sounds pretty frustrating.

 

Alternatively, you could have spent, say, an hour on comparing the different interfaces, learning the basics of PDO and then writing code which you can actually keep. Not only is this rewarding; you'll definitely learn more from this than from chasing a misspelled attribute for two days.

 

It was a starting point man ease up. I had no way of knowing what the industry standard was at this point so I went with top result on google which was dated recently as well. The 2 days you speak of was 2.5 hours and I could have spent that just trying to figure out what the industry standard is for db connections but now I learned it from a more knowledgeable source without realizing it was an issue at all. Any way around it I fixed my original problem which was unrelated to anything you responded with.

 

I'll give you exceptions, I could have benefited from looking into those initially. The fact that I spent a few hours over a 28 hour period does not bother me at all. It's a side project and I'm just getting my feet wet. I'll look into docs more but they don't tend to be that easy to read and with garbage code all over the internet I'm bound to have more questions that seem like a waste of time to a seasoned vet. I do apologize but at the same time you're free to not read my questions and/or not respond to them.

 

Can I ask which IDE you do prefer?

Share this post


Link to post
Share on other sites

No need to get butthurt.

 

This is an IT forum, so what you'll get here is technical advice from people who know what they're doing and see the full picture. When you encounter a problem, you get confused, ask others to “fix the code”, run into the next problem etc. When I encounter a problem, I solve it and then make sure it doesn't happen again. The second part is the important one.

 

People without IT experience often struggle to understand this and think it's all about chasing the one bug they've found. But your bugs are irrelevant. There will be many more typos in the code you copy and paste from the Internet, and none of them will teach you anything about PHP. So the real question is whether you want to use this forum as a spell checker or as a way to learn programming.

 

 

 

Can I ask which IDE you do prefer?

 

If you're looking for free IDEs, Netbeans and Eclipse are decent. PhpStorm is a more powerful IDE, but it's fairly expensive if you're neither a student nor have an employer to pay for the license.

Share this post


Link to post
Share on other sites

No need to get butthurt.

 

This is an IT forum, so what you'll get here is technical advice from people who know what they're doing and see the full picture. When you encounter a problem, you get confused, ask others to “fix the code”, run into the next problem etc. When I encounter a problem, I solve it and then make sure it doesn't happen again. The second part is the important one.

 

People without IT experience often struggle to understand this and think it's all about chasing the one bug they've found. But your bugs are irrelevant. There will be many more typos in the code you copy and paste from the Internet, and none of them will teach you anything about PHP. So the real question is whether you want to use this forum as a spell checker or as a way to learn programming.

 

 

 

 

If you're looking for free IDEs, Netbeans and Eclipse are decent. PhpStorm is a more powerful IDE, but it's fairly expensive if you're neither a student nor have an employer to pay for the license.

I'm not butthurt. I just felt Mac answered my question in a more productive way. Your first post had valuable information in it but it came off as you talking down to me which adds no value. Again, it's hard for someone with tons of experience to relate to someone with none. Experience has put you in a position where if you don't have an answer you know where to look. That's not the case for people just starting out. I assure you I will not turn to the forum to fix every little issue I run into, I did put some time into trying to fix it but at some point asking someone is much more effective since the fix was made in a few minutes.

 

On top of that I got some good feedback on what interface is preferred to connect to a DB, a reminder that I should look into exceptions and a couple good recommendations for IDE's which will help me avoid simple mistakes in the future. I got a lot out of that post that 2 people were kind enough to respond to.

 

On the topic of IDE's I have used eclipse for Java and although I liked it, it was quite resource heavy. I think I'll give Netbeans a shot. I did look into PhpStorm after your initial post and it seems pretty awesome but the price tag is too much for a hobby. If I ever decide to get serious about the language then I will lean that way.

Edited by cfrost

Share this post


Link to post
Share on other sites

I'm not butthurt. I just felt Mac answered my question in a more productive way.

 

Like I said, you don't see the full picture.

 

Do you know the saying about teaching a man to fish? mac_gyver gave you the solution to one particular problem, which is great and of course makes you happy -- for a day. I'm explaning how to solve problems. You may wonder what PDO, exceptions and IDE have to do with your question, like somebody who just wants a fish may wonder what the fuck that strange rod is for. The answer is: It solves your problem for a lifetime.

 

I understand that programming and finding defects often looks like black magic to a layman. Like something which requires special knowledge. The truth is: It's largely a matter of using the right tools. I'd say that ¾ of the problems wouldn't even exists if people read the manual, fixed their PHP configuration and used a proper IDE.

 

For example, this is how your code looks like in PhpStorm:

post-167590-0-11613400-1491305184.png

 

You seem to think I'm blaming you and don't want you to ask those questions. No. You can come back with any question you want. What I'm trying to do is give you a deeper understanding of how to write code, because you seem to be clever enough for the next level. If I got this wrong, just say so. Some people need more time, and some never get it. This is all fine.

post-167590-0-11613400-1491305184_thumb.png

Edited by Jacques1

Share this post


Link to post
Share on other sites

Like I said, you don't see the full picture.

 

Do you know the saying about teaching a man to fish? mac_gyver gave you the solution to one particular problem, which is great and of course makes you happy -- for a day. I'm explaning how to solve problems. You may wonder what PDO, exceptions and IDE have to do with your question, like somebody who just wants a fish may wonder what the fuck that strange rod is for. The answer is: It solves your problem for a lifetime.

 

I understand that programming and finding defects often looks like black magic to a layman. Like something which requires special knowledge. The truth is: It's largely a matter of using the right tools. I'd say that ¾ of the problems wouldn't even exists if people read the manual, fixed their PHP configuration and used a proper IDE.

 

For example, this is how your code looks like in PhpStorm:

post-167590-0-11613400-1491305184.png

 

You seem to think I'm blaming you and don't want you to ask those questions. No. You can come back with any question you want. What I'm trying to do is give you a deeper understanding of how to write code, because you seem to be clever enough for the next level. If I got this wrong, just say so. Some people need more time, and some never get it. This is all fine.

I followed everything you said and got a lot of value out of it. Again, I thank you for that added value. I just felt those things could have been said in a different way. Your responses have all had parts that came off as a guy who's seen too many beginners and forgot what it was like to be one. That's understandable. I just thought it might be worth noting that this could push new coders away from the field and although that may be good job security  :tease-03: it isn't the best thing for the industry.

Share this post


Link to post
Share on other sites

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.

Sign in to follow this  

×
×
  • 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.