0

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.

tadman
  • 208,517
  • 23
  • 234
  • 262
fguru
  • 71
  • 2
  • 9
  • 5
    You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Apr 25 '13 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 '13 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. – gen_Eric Apr 25 '13 at 14:47

2 Answers2

1

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_*.

pmandell
  • 4,288
  • 17
  • 21
  • 1
    Just be sure to use the [bind_param](http://php.net/manual/en/mysqli-stmt.bind-param.php) feature or you'll be missing the point. [SQL injection bugs](http://bobby-tables.com/) are no joke. – tadman Apr 25 '13 at 15:03
0
$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
}
JOE LEE
  • 1,058
  • 1
  • 6
  • 6