-2

I am trying to save error message but its not showing errors

$selectquery=mysql_query("select * from employee where eid='$eid' and name='$name' and  ssn='$ssn'");
if(mysql_num_rows($selectquery)>0)
{
   $state="success";
   $sql="insert into  pfupload(eid1,name,ssn,state) values('$eid','$name','$ssn','$state')";  
   $sqlquery=mysql_query($sql); 
}
else
{
     $state1= mysql_error();
   $sql1="insert into  pfupload(eid1,name,ssn,state) values('$eid','$name','$ssn','$state1')";  
   mysql_query($sql1); 
}
Sandy
  • 41
  • 7
  • Pass connection variable to `mysql_error()` – Saty May 04 '16 at 09:14
  • Why do you have a `state` and a `state1` variable? What is the output for `var_dump($state)`and for `var_dump($state1)`. If the `$selectquery ` is empty you don't get an ERROR. Also, be ware of mysql injections by reading here: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php and the use of `mysql` is deprecated you should use `mysqli`, read here: http://php.net/manual/en/intro.mysql.php – Mr.Web May 04 '16 at 09:15
  • What error would you expect here...?! – deceze May 04 '16 at 09:17
  • 1
    mysql is deprecated.I would recommend using mysqli – Saurabh May 04 '16 at 09:18
  • maybe `error_reporting(E_ALL)` would show you the errors – Velimir Tchatchevsky May 04 '16 at 09:19

2 Answers2

1

You must pass de conection variable to mysql_error().

Also, you should use mysqli_ functions instead of mysql_. All mysql_ functions were deprecated in PHP 5.5.0.

More info.

miguelmald
  • 70
  • 9
1

First be sure that you have enabled errors logging:

ini_set('display_errors', 1);
error_reporting(E_ALL);

Additionally I would reorganize your code a bit to remove duplication:

$selectquery = mysql_query("select * from employee where eid='$eid' and name='$name' and  ssn='$ssn'");
$state = mysql_num_rows($selectquery)>0 ? 'success' : mysql_error();
$sql1 = "insert into pfupload(eid1,name,ssn,state) values('$eid','$name','$ssn','$state')";  
mysql_query($sql1); 

It should work, if not pass the connection to mysql_error(). If you want to check only the rows count do the SELECT count(1) FROM employee WHERE eid='$eid' AND name='$name' AND ssn='$ssn' query, and then check the count. It will make your execution faster and memory footprint much lower because you don't have to load all results into PHP.

PolishDeveloper
  • 930
  • 5
  • 17