使用jquery post连接到我的sql数据库时,如何返回错误消息

问题描述

|| 我正在尝试使用jquery post函数返回错误以检索错误。 到目前为止,我已经用PHP编写了
$connect = MysqL_connect(\"localhost\",\"username\",\"password\") or die(\"Error connecting to the database.\");
但我想像这样重新发送消息
$connect = MysqL_connect(\"localhost\",\"password\");

if(!$connect){              
  $data[\'error\'] = true;
  $data[\'message\'] = \"Error connecting to the database.\";
  echo json_encode($data);  
};
救命!!     

解决方法

尝试这个:
$connect = mysql_connect(\"localhost\",\"username\",\"password\") or die(json_encode(array(\'error\' => true,\'message\' => mysql_error())));
这会将错误编码为json字符串