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
4  
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 45 mins ago
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 45 mins ago
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 41 mins ago

2 Answers

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 32 mins ago
1  
Just be sure to use the bind_param feature or you'll be missing the point. SQL injection bugs are no joke. – tadman 24 mins ago
$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

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.