Skip to content
Advertisement

issues printing last error message in php

I am trying to connect to a MySQL database using PHP, but I am getting 32767 error reporting in the console. I tried to show the error message using echo error_reporting() but it is not showing the error message.

Here is the code:

$name = $_POST[name];
$topic = $_POST[topic];
$tell = $_POST[tell];
$birthDay = $_POST[birthDay];
$job = $_POST[job];
$email = $_POST[email];
$lastMajor = $_POST[lastMajor];
$major = $_POST[major];
$add = $_POST[add];
$today = "test";

$connectionString = mysqli_connect("localhost","root","root","myDB");
 if (mysqli_connect_errno()) 
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

mysqli_query($connectionString,"INSERT INTO coWork (topic , name , tell , birthDay , job , email , lastMajor , major, add , date )
     VALUES ('$topic', '$name','$tell','$birthDay','$job' , '$email' , '$lastMajor' , '$major' , '$add' , '$today')");

mysqli_close($connectionString);
echo error_reporting();

How can I get the content of the error message?

Advertisement

Answer

error_reporting() => Sets which PHP errors are reported

To get/handle errors you have to use functions like error_get_last or set_error_handler

In your case you want to get the MySql error, so you have to use mysqli_error or mysqli_errno

if (!mysqli_query($connectionString, 'INSERT ...')) {
    printf("Error: %sn", mysqli_error($connectionString));
}

Like Quentin noted you are vulnerable to SQL injection attacks. So please use mysqli_real_escape_string or mysqli_prepare.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement