在php中打印最后一条错误消息

问题描述 投票:-1回答:1

我正在尝试使用PHP连接到MySQL数据库,但是在控制台中出现32767错误报告。我尝试使用echo error_reporting()显示错误消息,但未显示错误消息。

这里是代码:

$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();

如何获取错误消息的内容?

php mysql mamp
1个回答
3
投票

error_reporting() => Sets which PHP errors are reported

要获取/处理错误,您必须使用error_get_lastset_error_handler之类的功能>

在您的情况下,您想获取MySql错误,因此您必须使用mysqli_errormysqli_errno

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

就像Quentin指出的那样,您容易受到SQL注入攻击的攻击。因此,请使用mysqli_real_escape_stringmysqli_prepare

© www.soinside.com 2019 - 2024. All rights reserved.