0

I am getting this error when I run the following code:

PHP Fatal error: Uncaught Error: Call to undefined function mysqli_result() in chk_discount.php:21

Here's the full code:

<?php
include 'client_config.php';
foreach($_GET as $key => $value) {
    $get[$key] = filter($value);
}
$total = base64_decode($_GET['total']);
echo calculate_discount($_GET['code'],$total);
function calculate_discount($code, $total) {
    global $con;
    if ($code && $total) {
        $now = time();
        list($discount_offer_on_totals) = mysqli_fetch_row(mysqli_query($con, "select discount_offer from orders_discounts")); 
        if ( $total < $discount_offer_on_totals ) {
        return "Order total must be ".$curr_symbol. ' '. $discount_offer_on_totals." or more to qualify for discount!";
        }
        // check if code is valid and return result
        $sql = @mysqli_query($con, "SELECT * FROM orders_discounts WHERE codex = '$code' AND expiry > $now AND status = 1 AND $total > discount_offer");
        if (@mysqli_num_rows($sql) == 0) {
            return "Invalid coupon code. Please try again.";
        } else {
            return mysqli_result($sql, 0, "percentage");
            exit();
        }
    } else {
        return "Invalid request! Please enter correct code. ";
    }
}
?>

How should I edit the code?

  • "I know mysqli_result() does not exist in PHP 7" — You're wrong. It does exist in PHP 7. Possibly you just don't have the extension installed. Install it. – Quentin May 30 '18 at 08:56
  • That's kinda confusing. I was trying to use the recommendations on this thread https://stackoverflow.com/questions/34118031/call-to-undefined-function-mysqli-result Aint working either – Anthony Muchangi May 30 '18 at 08:58
  • Agreed with @Quentin. see here:- https://prnt.sc/jog3jn. Also where you used this function in your code? I am unable to see that – Alive to die - Anant May 30 '18 at 08:58
  • I would recommend that you stop using `@` in your code, this covers other errors up. – Nigel Ren May 30 '18 at 08:59

2 Answers2

0

mysqli_result() is indeed not what you're looking for here, which is sort of implied in the docs. It exists in PHP 7 though.

Based on your code, you're probably looking for something along the lines of mysqli_fetch_object() or mysqli_fetch_row()

Loek
  • 4,037
  • 19
  • 35
  • No. Sorry but SO is a specific problem solving website, not a place you come to so others can write your code for free. In the documentation of these functions, there are really clear examples. Fiddle around with your code and see what you can come up with. I'm happy to answer why your code doesn't work and what you can do to fix it, but I'm not going to rewrite your code because you're in over your head; no offence meant. – Loek May 30 '18 at 09:10
0

According to the doc, mysqli_result is a class, this is why you have a PHP Fatal error with an undefined function.

I suggest you trying to use mysqli_result::fetch_assoc to fetch your results into an associative array

elki42
  • 123
  • 1
  • 10
  • Take a look at the precise answer your question is a duplicate of, you will surely find what you are looking for. – elki42 May 30 '18 at 09:30