比较数据sql

时间:2012-09-08 17:57:03

标签: php mysql

我有像这样的代码php

$result = mysql_query("SELECT phone FROM user where phone LIKE 'hjkhkjh');


// check if row inserted or not
if ($result) {
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "Account wasnt created.";

    // echoing JSON response
    echo json_encode($response);
}
else  {
    // failed to insert row
    $response["success"] = 0;
    $response["message"] = "Account was created.";

我想比较数据库中的电话号码,但是当我运行此代码时,此代码显示“帐户未创建”,而我的表中有数据电话号码'hjkhkjh',我的代码的任何解决方案,谢谢

3 个答案:

答案 0 :(得分:3)

因为当if-construct解析为true时,你写的是“帐户未创建”。 试试这个

if ($result) {
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "Account was created.";

    // echoing JSON response
    echo json_encode($response);
}
else  {
    // failed to insert row
    $response["success"] = 0;
    $response["message"] = "Account wasnt created.";

答案 1 :(得分:1)

www.php.net/manual/en/function.mysql-query.php说:

  

对于SELECT,SHOW,DESCRIBE,EXPLAIN和其他语句返回   resultset,mysql_query()在成功时返回资源,或者返回FALSE   错误。

因为您的查询不会产生错误,所以return是资源,它传递if ($result) { (无论数据库表中是否存在具有电话LIKE'hjkhkjh'的实际条目)

答案 2 :(得分:1)

我找到了解决方案,我改变了我的代码 $ result = mysql_query(“来自用户的选择电话=电话='hjkhkjh'”);

// check if row inserted or not
if($row = mysql_fetch_array($result)){
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "Account was created.";

    // echoing JSON response
    echo json_encode($response);
}
else  {
    // failed to insert row
    $response["success"] = 0;
    $response["message"] = "Account wasnt created.";

    // echoing JSON response
    echo json_encode($response);
} 

谢谢大家:D