mysql_errno() and mysql_error not returning anything


When I use the following statement:

$objResult = mysql_query($strQuery, $resConnectionId) or die("mysql_errno = " . mysql_errno() . "<br />mysql_error = " . mysql_error());

and there is definitely an error e.g. duplicate insert, mysql_no and mysql_error return nothing. I need to be able to do things based on the value of mysql_errno.

Could anyone suggest what could cause this?


Are you sure there is an error? Note that the DIE statement will halt all execution of the code…you may want to change to this:

$objResult = mysql_query($strQuery, $resConnectionId) ;
if (mysql_errno($objResult)!= 0){
  //handle the error here

  //no error so make good things happen

}//end if

your code looks fine, i used a similiar one for a long time.

try using a 100% wrong query for testing, e.g. “SELCT foo FROM table”

Yes, there is definitely a detectable error - the current one I’m trying to handle is a duplicate insert, which should return a 1062. I just put the die in my post to illustrate that mysql_errno and mysql_error doesn’t return anything in the first place. I use an error routine normally.

Without a return code, I can’t do anything. That’s the bit I’m trying to solve i.e. why isn’t it returning anything when there is definitely a detectable error.

If there are any other suggestions that I could look at to see if they are causing the problem. I thought it might be something to do with a setting in the mysql or php config files, but I don’t know which ones would affect something like this.


There’s nothing wrong with the post using the die. Have you actually tried using a die in your code?
It sounds more like the error routine is at fault. Maybe post that?

Yes, I tried the die, but it still showed nothing. I don’t think it can be anything to do with my code as the mysql_errno function itself doesn’t return anything.

I just need some suggestions as to what else I could take a look at, like are there any config settings that could affect this kind of thing?


Sorry, just clarify.
Is it executing the die and showing empty values:
mysql_errno =
mysql_error =

or just not executing the die and continuing on with your code?

Yes exactly that, the die works, but it shows no values.

Can you try using mysql_db_query($strQuery, $resConnectionId) instead?

I’ve just found the solution. If I pass the connection id to the functions, it works perfectly. I knew it was something too simple to see. Woods and trees, I suppose.