JSON没有读取php mysql num rows count

时间:2012-10-18 10:01:58

标签: php jquery mysql

include_once "connect_to_mysql.php";
$query = mysql_query("SELECT * FROM  data WHERE Email = '$email'");
$num = array();
//Caculate the number of rows that have macthing username
$numrows = mysql_num_rows($query);
$num[] = $numrows;

if ($num != 0){
    $data = array('success' => 'true', 'message' => 'Incorrect Email');
    echo json_encode($data);
}

不处理if语句,也不回显JSON数据,而是冻结脚本 我做错了什么?

4 个答案:

答案 0 :(得分:1)

$result = mysql_query("SELECT COUNT(*) AS num FROM data WHERE Email = '$email'");
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$num = intval($row['num']);
if ($num != 0) {
    $data = array('success' => 'true', 'message' => 'Email found');
} else {
    $data = array('success' => 'false', 'message' => 'Incorrect Email');
}
echo json_encode($data);

答案 1 :(得分:1)

我不明白你为什么要使用$num[]

include_once "connect_to_mysql.php";
$email = $_POST['email'];
$query = mysql_query("SELECT * FROM  data WHERE Email = '$email'");
 $numrows = mysql_num_rows($query);
    if ($numrows != 0){
        $data = array('success' => 'true', 'message' => 'Correct Email');
        echo json_encode($data);
    }else{
        $data = array('success' => 'false', 'message' => 'Incorrect Email');
        echo json_encode($data);
    }

尝试使用以下Jquery:

$.post('your_file.php',{email: 'your_email'},function(data){
  alert(data.success + ' ' + data.message);
},'json');

答案 2 :(得分:0)

您正在向数组$numrows中提取标量值$num[],因此您遇到了错误。 尝试直接在if语句中使用$numrows,如下所示:

if ($numrows != 0){

答案 3 :(得分:0)

$query = mysql_query("SELECT * FROM  data WHERE Email = '$email'");
if(mysql_num_rows($query)){
$data = array('success' => 'true', 'message' => 'Email found');
}
else{
$data = array('success' => 'false', 'message' => 'Incorrect Email');
}
echo json_encode($data);