2017-03-16 9 views
0

私はクーポンのコードを書いています。関数を使用してクーポンコードが存在するかどうかを確認するたびに、trueを返してそのステートメントを出力します。しかし、私は誤った返品を得ることができず、虚偽の場合には声明を印刷することができません。私はそれが印刷さCODE50としてCOUPON_CODEを供給する際PHPが偽の値を返さない

function db_connect() { 
    static $connection; 
    if (!isset($connection)) { 
    $connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
    } 
    if ($connection === false) { 
    return mysqli_connect_error(); 
    } 
    return $connection; 
} 

function db_query($query) { 
    $connection = db_connect(); 
    $result = mysqli_query($connection, $query); 
    return $result; 
} 

function db_error() { 
    $connection = db_connect(); 
    return mysqli_error($connection); 
} 

function db_select($query) { 
    $rows = array(); 
    $result = db_query($query); 
    if ($result === false) { 
    return false; 
    } 
    while ($row = mysqli_fetch_assoc($result)) { 
    $rows[] = $row; 
    } 
    return $rows; 
} 

function db_rows($query) { 
    $result = db_query($query); 
    if ($result === false) { 
    return false; 
    } 
    $total_rows = mysqli_num_rows($result); 
    return $total_rows; 

} 

function couponExists($cc) { 
    $results = db_rows("SELECT * from ms_coupons where coupon_code='".$cc."'") or die(db_error()); 
    if ($results > 0) { 
    return true; 
    } else { 
    return false; 
    } 
} 

DB table : ms_coupons 
coupon_id coupon_code 
1   CODE50 

check_coupon.php

$coupon = $_REQUEST['coupon_code']; 
if (!couponExists($coupon)) { 
    echo "Does not exist!"; 
} else { 
    echo "Coupon exists!"; 
} 

、クーポンが存在します!しかし、私がCODE51のような何かを供給すると、支払いは何も印刷されません。

+0

"何も印刷していません"は、エラーがある可能性があることを示しています。 PHPのログには何かがありますか?エラー報告を有効にすると、ページに何かが表示されますか?意図されたロジック全体に一連の 'echo'ステートメントを置いて、どこが期待されたパスから逸脱しているのかを具体的に見極めることができますか? – David

+0

checkdを実行すると、mysqlのnum行が0になります。あまりにも多くのコード.... – JustOnUnderMillions

+3

結果が0のときに 'die(db_error());'も死ぬでしょうか? DBエラーも空の場合、何も印刷されません。 – apokryfos

答えて

1

あなたの問題は、あなたのコードでこの行を次のとおりです。

$results = db_rows("SELECT * from ms_coupons where coupon_code='".$cc."'") or die(db_error()); 

db_rows機能は、あなたの場合には、それは2つだけの値とすることができ、行の数を返します - または0。 db_rowsが0を返すと、スクリプトはdie(db_error())の部分を実行します(ただし、mysqli_errorはありません)。

このように、or die(db_error())一部を削除します。あなたはmysqli_errorsをチェックしたい場合は

$results = db_rows("SELECT * from ms_coupons where coupon_code='".$cc."'"); 

、あなたのdb_query機能に移動します。

+0

あなたは本当に鋭い目を持っています – C2486

関連する問題