0

I have written a piece of code for coupon. whenever I use the function to check if the Coupon Code exists, it returns true and prints the statement. But I can't get the false return and cannot get the statement printed for false case.

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!";
}

when i supply coupon_code as CODE50 it prints, Coupon exists! but when i supply something different like CODE51 then the pays prints nothing.

4
  • "prints nothing" suggests that there may be an error. Is there anything in the PHP logs? When you enable error reporting, is anything displayed on the page? Maybe place a series of echo statements throughout the intended logic to see more specifically where it's deviating from the expected path? Commented Mar 16, 2017 at 16:37
  • Did you now that mysql num rows of 0 will equal to false when checkd. So much code .... Commented Mar 16, 2017 at 16:37
  • 3
    Wouldn't or die(db_error()); also die when there's 0 results? DB error would also be empty then so nothing will be printed. Commented Mar 16, 2017 at 16:37
  • As note to comment from @apokryfos. OP what do you thing will be printed here: print $var = 0 OR 5; please remove all or in lines where you bind stuff to an variable. Commented Mar 16, 2017 at 16:40

1 Answer 1

1

Your problem is with this line in your code:

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

db_rows function returns number of rows, in your case it can be only two values - 1 or 0. If db_rows return 0 your script executes the die(db_error()) part (but there is no mysqli_error).

Remove the or die(db_error()) part, like this:

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

If you want to check for mysqli_errors, move it to your db_query function.

Sign up to request clarification or add additional context in comments.

1 Comment

You really have sharp eyes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.