goelakash
goelakash

Reputation: 2519

mysql_error() not printing the error

I am a beginner in php-mySQL. My code is a as follows-

<?php

echo "test_script running!!<br/>";

$con=mysqli_connect("localhost","root","root","netHealth");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
else
  echo "Connection established <br/>" ;

for($i=1;$x<4;$i+=1)
{
    $sql1="INSERT INTO `netHealth`.`role_perm` (`role_id` ,`perm_id`) VALUES ($i, '1')";
    $sql2="INSERT INTO `netHealth`.`role_perm` (`role_id` ,`perm_id`) VALUES ($i, $i+1)";

try {
  $res1=mysqli_query($con,$sql1);
  if (!$res1) throw new Exception("mysql Error");
} catch(Exception $e) {
  echo $e->getMessage();
echo mysql_error();
break;
}
try {
  $res2=mysqli_query($con,$sql2);
  if (!$res2) throw new Exception("mysql Error");
} catch(Exception $e) {
  echo $e->getMessage();
echo mysql_error();
break;
}
}

?>

The error is fine, as the list items cannot be duplicated in the role_perm table. But the mysql_error() function doesn't return any error description. The output is as follows-

test_script running!!
Connection established
mysql Error

What's the issue here?

Upvotes: 1

Views: 520

Answers (1)

John Conde
John Conde

Reputation: 219804

You're using mysqli_* so you need to use mysqli_error(). The mysql and mysqli libraries are not compatible.

echo mysqli_error($con);

Upvotes: 3

Related Questions