开发者

How to return an error message when connecting to my sql database useing jquery post

开发者 https://www.devze.com 2023-03-08 13:23 出处:网络
I am trying to return an error using jquery post function to retrieve the error. So far I have开发者_如何学编程 written in php

I am trying to return an error using jquery post function to retrieve the error.

So far I have开发者_如何学编程 written in php

$connect = mysql_connect("localhost","username","password") or die("Error connecting to the database.");

but i want to retunrn the message like so

$connect = mysql_connect("localhost","username","password");

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

Help!!


Try this:

$connect = mysql_connect("localhost","username","password") or die(json_encode(array('error' => true, 'message' => mysql_error())));

This will encode the error to a json string

0

精彩评论

暂无评论...
验证码 换一张
取 消