-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-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.";
于 2012-09-08T18:04:15.257 に答える
1

www.php.net/manual/en/function.mysql-query.phpによると:

SELECT、SHOW、DESCRIBE、EXPLAIN、および結果セットを返すその他のステートメントの場合、mysql_query()は成功した場合はリソースを返し、エラーの場合はFALSEを返します。

クエリはエラーを生成しないため、returnはresourceであり、これは渡されますif ($result) { (dbテーブルに電話LIKE'hjkhkjh'を持つエントリがあるかどうかに関係なく)

于 2012-09-08T20:08:43.767 に答える
1

解決策が見つかりました。コードを次のように変更します $result =mysql_query("SELECT phone from user where phone ='hjkhkj'");

// 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 に答える