-4

我有这样的代码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”,我的代码的任何解决方案,谢谢

4

3 回答 3

3

因为当 if 构造解析为 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.";
于 2012-09-08T18:04:15.257 回答
1

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

对于 SELECT、SHOW、DESCRIBE、EXPLAIN 和其他返回结果集的语句,mysql_query() 成功时返回资源,错误时返回 FALSE。

因为您的查询不会产生错误,所以返回是通过的资源if ($result) { (无论数据库表中是否实际存在具有 phone LIKE 'hjkhkjh' 的条目)

于 2012-09-08T20:08:43.767 回答
1

我找到了解决方案,我像这样更改我的代码 $result =mysql_query("SELECT phone from user where phone ='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

于 2012-09-10T02:36:19.690 回答