Jump to content

sql error return json to jquery


stribor40

Recommended Posts

i have ajax call that send data to the pph script which inserts it into database.

If for some reasons I have error when connecting to database ot error on insertion how do i sent some sort of error to ajax.

  <?php
  error_reporting(0);
   
   $a = $_POST['user'];
   $b = $_POST['name'];
   $c = $_POST['comment'];
   
   
   $link = mysql_connect('localhost', 'rot', 'joker1');
  
  if (!$link) {
       $arr = array('connect' => 0);
       //die('Could not connect: ' . mysql_error());
  }
  
 
  
  header('Content-type: application/json');
  echo json_encode($arr);
  
  
 ?>

This is the only way i was able to pass json back to ajax. Can anyone suggest better way to do this. I would like to keep die statement but in order to get it working i had to comment it off

Link to comment
https://forums.phpfreaks.com/topic/280737-sql-error-return-json-to-jquery/
Share on other sites

Seems like you kind of got the idea of how I'd do it. Something like this should work.. Note that I added mysql_error() into the array it's self.
 

<?php
error_reporting(0);
 
$a = $_POST['user'];
$b = $_POST['name'];
$c = $_POST['comment'];

$link = mysql_connect('localhost', 'rot', 'joker1');

if (!$link) {
    $arr = array(
        'error' => mysql_error()
    );
}

header('Content-type: application/json');
echo json_encode($arr);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.