mysql_error()
returns a string depending on what is duplicate
error number is 1062
is there a way how I can reference this error number?
something like if mysql_error()=='1062'
or am I thinkinking in the wrong direction
[RESOLVED] mysql_error() return Value
You would use [man]mysql_errno/man. That said, have you considered using the MySQLi extension or the PDO extension instead?
if(!$sql)
{
echo "Error-No. " . mysql_errno()." - " .mysql_error();
}
for some reason I dont get the number
I get the error description ok
All I want to avoid is posting data twice.
If the user is using back and forward buttons on the browser
Unfortunately, if you are not getting the error number with mysql_errno() when you should, then it implies that you have discovered a bug. What version of PHP and MySQL are you using, and on which operating system?
Apache 2.2.14
PHP 5.3.1
MySql mysql Ver 14.14 Distrib 5.1.42, for Win32 (ia32)
Win XP Pro SP3
Do some more tests
with
echo mysql_errno();
I find it not very probable there is such a bug in any of the common mysql functions.
Because they are used by million and million users and tester in the world.
And even long before PHP 5.3.1 was released, you can count many many used
the pre-release from SVN, development version.
ok I did a little more testing
error number is displayed once (only the first time) I can do my error handling based on that.
however if I dont do the proper error handling and just disregard everything and move with the browsers back and forward buttons the error number disappears and only the error warning text is displayed.
Note that this function only returns the error code from the most recently executed MySQL function (not including mysql_error() and mysql_errno()),
so if you want to use it,
make sure you check the value before calling another MySQL function.
Return Values
Returns the error number from the last MySQL function,
or 0 (zero) if no error occurred.
So, this function [man]mysql_errno[/man] should be put right after
the mysql function you want to catch an error from.
And I would use it preferably with an exit(),
to stop further execution and messing up my database
For example:
<?php
// more here
$result = mysql_query("SELECT * FROM nonexistenttable");
// test eventual error in this query
if( mysql_errno() ) exit( mysql_errno() .' :: '. mysql_error() );
// more here
?>
I only want to catch the 1062 duplicate entry one
then send the user back to the page he came from
there another query picks up the next number automatically
and the form can be resubmitted
$sql=mysql_query( "INSERT INTO......
if (mysql_errno($sql)==1062)
{
echo "Somebody was faster." . header("Refresh: 3; URL=pageyoucamefrom.php");//
}
elseif(!$sql)
{
echo "Error-No. " . mysql_errno()." - " .mysql_error();
but there is something wrong in the statement only the elseif is executed
You should not put the $result ($sql) in mysql_errno/mysql_error
mysql_errno ([ resource $link_identifier ] )
Just put nothing.
There can be cases when you need to put
mysql_errno($link);
where $link is your mysql connection.
I would do something like this, for your idea:
$sql=mysql_query( "INSERT INTO......");
$eno = mysql_errno();
if($eno==1062)
{
echo "Somebody was faster." . header("Refresh: 3; URL=pageyoucamefrom.php");
exit();
}
elseif($eno!=0)
{
echo "Error-No. " . $eno . " - " .mysql_error();
}
// else do nothing, errno=0