Mysql检查mysql数据库中的值失败了吗?

时间:2016-08-04 06:55:11

标签: php mysql

我有一个非常简单的用例,我正在检查表中是否存在某个值并且它似乎总是失败。这是我的PHP代码。

<?php

            include "config.php";

            $con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$dbname);

                    if(!$con)
             {
                             echo "Connection Error".mysqli_connect_error();
             }
             else{
            //echo "";
             }
                     $device_id = $_POST["device_id"];
                     $check = "SELECT magazine_id FROM registered_buyers WHERE device_id = $device_id";
                     $rs = mysqli_query($con,$check);
                     if(mysqli_num_rows($con,$rs) == 0)
                     {
                     $jsonarray = $_POST["jsonarray"];
                     echo "This will be inserted".$jsonarray;

                     }else
                     {
                            echo "User already registered";
                    }

    ?>   

任何人都可以指出我的错误。欢迎任何帮助或建议。谢谢。

2 个答案:

答案 0 :(得分:1)

因为我没有足够的代表来添加评论,我会认为device_id是字符串,如果有的话尝试这样的事情:

"SELECT magazine_id FROM registered_buyers WHERE device_id = '$device_id'";

添加'

答案 1 :(得分:1)

您可以尝试遵循此代码。

<?php
include "config.php";
$con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$dbname);
if(!$con){
    echo "Connection Error".mysqli_connect_error();
}
$device_id = $_POST["device_id"];
$check = "SELECT magazine_id FROM registered_buyers WHERE device_id = ".$device_id;
$rs = mysqli_query($con, $check);
if(mysqli_num_rows($rs) == 0){
    $jsonarray = $_POST["jsonarray"];
    echo "This will be inserted".$jsonarray;
}else{
    echo "User already registered";
}
?>