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
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);
?>
Edited by Joshua F
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.