dounong5373 2014-09-09 10:59
浏览 52
已采纳

如何获取PHP中的最后一个错误?

i just tried to connect my form the my sql using PHP but it always report 32767 with the function echo error_reporting() and i am using MAMP 2.1 on mac and this is my whole 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 do i fix this ?!!

  • 写回答

1条回答 默认 最新

  • douwen2072 2014-09-09 11:32
    关注

    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: %s
    ", mysqli_error($connectionString));
    }
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请提供一个符合要求的网页链接。
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码