0

While developing on localhost I used mysqli_fetch_all in my code. But after uploading on godaddy shared hosting, its not working, and keep getting error message:

PHP Fatal error: Uncaught Error: Call to undefined function mysqli_fetch_all()

Have checked the PHP version (7.1), and even checked the "mysqlnd" option and nothing works.

I need exact alternative of this code. Any suggestions?

<?php

include('conn.php');


if (isset($_POST["RegionRequest"])) {
    $Region = $_POST["Region"];
    $sql = "SELECT DISTINCT country FROM test where region='" . mysqli_real_escape_string($conn, $Region) . "' AND status='Y' ORDER BY country ASC";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        echo json_encode(mysqli_fetch_all($result, MYSQLI_ASSOC));      
    }
}

if (isset($_POST["CountriesRequest"])) {
    $Countries = $_POST["Countries"];
    $Region = $_POST["Region"];
    $sql = "SELECT DISTINCT state FROM test where country='" . mysqli_real_escape_string($conn, $Countries) . "'  AND region='" . mysqli_real_escape_string($conn, $Region) . "' AND status='Y' ORDER BY state ASC";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        echo json_encode(mysqli_fetch_all($result, MYSQLI_ASSOC));
    }
}


if (isset($_POST["stateRequest"])) {
    $state = $_POST["state"];
    $cid = $_POST["cid"];
    $Region = $_POST["Region"];
    $sql = "SELECT DISTINCT city FROM test where region='" . mysqli_real_escape_string($conn, $Region) . "' AND state='" . mysqli_real_escape_string($conn, $state) . "' and country='" . mysqli_real_escape_string($conn, $cid) . "' AND status='Y' ORDER BY city ASC";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
    echo json_encode(mysqli_fetch_all($result, MYSQLI_ASSOC));      
    }
}


if (isset($_POST["cityRequest"])) {
    $city = $_POST["city"];
    $state = $_POST["state"];
    $cid = $_POST["cid"];
    $Region = $_POST["Region"];
    $sql = "SELECT suburb FROM test where region='" . mysqli_real_escape_string($conn, $Region) . "' AND city='" . mysqli_real_escape_string($conn, $city) . "' AND state='" . mysqli_real_escape_string($conn, $state) . "' and country='" . mysqli_real_escape_string($conn, $cid) . "' AND status='Y' ORDER BY suburb ASC";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        echo json_encode(mysqli_fetch_all($result, MYSQLI_ASSOC));
    }
}


if (isset($_POST["suburbRequest"])) {
    $suburb = $_POST['suburb'];
    $city = $_POST["city"];
    $state = $_POST["state"];
    $cid = $_POST["cid"];
    $Region = $_POST["Region"];
    $sql = "SELECT * FROM test where region='" . mysqli_real_escape_string($conn, $Region) . "' AND state='" . mysqli_real_escape_string($conn, $state) . "' AND country='" . mysqli_real_escape_string($conn, $cid) . "' AND suburb='" . mysqli_real_escape_string($conn, $suburb) . "' AND status='Y'";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        echo json_encode(mysqli_fetch_all($result, MYSQLI_ASSOC));
    }
}


?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Limm007
  • 21
  • 3
  • 2
    _"Call to undefined function"_ typically indicates that the mysqli module is not installed in your PHP server. You can run `phpinfo();` to verify. – Alex Howansky Jan 12 '18 at 16:24
  • 2
    Also, don't rely on the `real_escape_string()` functions to prevent SQL injection, [they alone are not sufficient](https://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string). You should use prepared statements with bound parameters, via either the [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) driver. [**This post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has some good examples. – Alex Howansky Jan 12 '18 at 16:25
  • *"even checked the "mysqlnd" option"* – What does that mean exactly? What exactly did you check? Did you confirm with `phpinfo()` that mysqlnd is active? – deceze Jan 12 '18 at 16:30
  • 1
    Why not use `$result->fetch_all()` instead, since you're using OO style for the rest? – aynber Jan 12 '18 at 16:31
  • @aynber Well if `mysqli_fetch_all()` does not exist then neither will `$result->fetch_all()` – RiggsFolly Jan 12 '18 at 16:32
  • @RiggsFolly Then wouldn't `$result->num_rows` and `$conn->query` fail before it even got to that point? – aynber Jan 12 '18 at 16:33
  • 2
    @aynber Not if the issue is that the OP did not do a proper check for `mysqlnd` – RiggsFolly Jan 12 '18 at 16:35

0 Answers0