Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Looking for a best solution:

$.getJSON("InsertData.php", {fullName:val1, course_id:course_id, occupation:val2}, function(data) {
        $.each(data, function(i, user) {
            //alert(user.aryA.status);
            if(user.aryA.status == 'true'){
                currentPosition = 2;
                checkData();
                nextSlide();
            }else{
                 nextSlide();
            }
        });

    })

Here is php code:

    mysql_select_db("db", $con);

    $Query="SELECT * from table WHERE fullName='".$fullName."' and course_id='".$cid."'";
    $result = mysql_query($Query);
    $totalRecords = mysql_num_rows($result);
    if($totalRecords) {
        while ($row = mysql_fetch_array($result)) {
                $returnData[]=array(    //for Json data array
            'userName' => $row['fullName'],
            'aryA' => array(
                'status' => $row['status']
                )
            );
        }

    }

    if(!$totalRecords) {
        $insertQuery="INSERT INTO table (fullName,course_id,occupation) VALUES ('".addslashes($fullName)."','".addslashes($cid)."','".addslashes($d3)."')";
        $result1 = mysql_query($insertQuery);

    }else{
        if($stat == "true"){$value = 1;}
        }

mysql_close($con);

echo json_encode($returnData);

So In first case when I hit the php through jquery it saves data in database but give me error or length. Because $returnData is empty. Is there a way if $totalRecords is false, how to send json_encode to say there is no data or any value through json_encode to my jQuery function.

Thanks in advance.

share|improve this question
5  
You are using an obsolete database API and should use a modern replacement. You are also vulnerable to SQL injection attacks that a modern API would make it easier to defend yourself from. –  Quentin Apr 25 at 14:42
1  
Please, please please... for the umptieth time: stop using mysql_*. It's unsafe and being deprecated, and will be removed from PHP altogether in time. use mysqli_* or PDO –  Elias Van Ootegem Apr 25 at 14:42
 
Just add $returnData = array() at the top of your code. This will be encoded as '[]'. When $.each is passed an empty array, it will just do nothing. –  Rocket Hazmat Apr 25 at 14:47
add comment

2 Answers

up vote 0 down vote accepted
$returnData = array(); //add this
 $totalRecords = mysql_num_rows($result);
if($totalRecords) {
while ($row = mysql_fetch_array($result)) {
        $returnData[]=array(    //for Json data array
    'userName' => $row['fullName'],
    'aryA' => array(
        'status' => $row['status']
        )
    );
}

}
else
{
$returnData[] = 'no Record'; //add this
}
share|improve this answer
add comment

Just setup an else statement, and add a 'success' key to your array:

if($totalRecords){
    while ($row = mysql_fetch_array($result)) {
            $returnData[]=array(    //for Json data array
        'success'=>'true',
        'userName' => $row['fullName'],
        'aryA' => array(
            'status' => $row['status']
            )
        );
    }
}else{
    $returnData = array('success'=>'false');
}

Then check the value of 'success' in your jQuery.

Also, you really shouldn't be using mysql_*.

share|improve this answer
1  
Thanks pmandell. I'll update my code with mysqli –  fguru Apr 25 at 14:55
1  
Just be sure to use the bind_param feature or you'll be missing the point. SQL injection bugs are no joke. –  tadman Apr 25 at 15:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.