0

i'm working on a project base on a tutorial, looks like my host can't fully support mysqlnd for using get_result. so after i've did some researches, i've found solution in using bind_result, after hours i've tried to fix it and still nothing.

please help me to find the problem or change it in proper way.

Here are the Codes base on Tutorial which they worked fine with XAMPP :

Function.php

public function getUserInformation($phone)
{
    $stmt = $this->conn->prepare("SELECT * FROM User WHERE Phone=?");
    $stmt->bind_param("s",$phone);

    if($stmt->execute()) {
        $user = $stmt->get_result()->fetch_assoc();
        $stmt->close();

        return $user;
    } else
        return NULL;
    }

And After I've changed them to this in function.php

public function getUserInformation($phone)
{
    $stmt = $this->conn->prepare("SELECT Phone, Name, Birthdate, Address FROM User WHERE Phone=?");
    $stmt->bind_param("s",$phone);

    if($stmt->execute()) {
        $stmt->bind_result($phone, $name, $birthdate, $address);
        $user = array();

        while($stmt->fetch()) {
            $tmp = array();
            $tmp["phone"] = $phone;
            $tmp["name"] = $name;
            $tmp["birthdate"] = $birthdate;
            $tmp['address'] = $address;
            array_push($user, $tmp);
        }


        $stmt->close();

        return $user;
    } else
        return NULL;
    }

and now i'm getting

{"phone":null,
"name":null,
"birthdate":null,
"address":null,
"avatarUrl":null
}

instead of

{"phone":"+18561523172",
"name":"Johb",
"birthdate":"1983-02-14",
"address":"Nxy 123",
"avatarUrl":""
}

thanks.

Edit 01.

to answer the questions about error, this is the error :

<br />
<b>Notice</b>:  Undefined index: Phone in <b>/home/meskand1/public_html/pasargad-    drinkshop/getuser.php</b> on line <b>24</b><br />
<br />
<b>Notice</b>:  Undefined index: Name in <b>/home/meskand1/public_html/pasargad-    drinkshop/getuser.php</b> on line <b>25</b><br />
<br />
<b>Notice</b>:  Undefined index: Birthdate in <b>/home/meskand1/public_html/pasargad-drinkshop/getuser.php</b> on line <b>26</b><br />
<br />
<b>Notice</b>:  Undefined index: Address in <b>/home/meskand1/public_html/pasargad-drinkshop/getuser.php</b> on line <b>27</b><br />
<br />
<b>Notice</b>:  Undefined index: avatarUrl in <b>/home/meskand1/public_html/pasargad-drinkshop/getuser.php</b> on line <b>28</b><br />
{"phone":null,"name":null,"birthdate":null,"address":null,"avatarUrl":null}

before this with get_result i had issue with

Call to undefied method mysqli_stmt::get_result(

and the solution was to change it to bind_result and host don't care about mysqlnd problem

omg After almost hours i've fixed it by changin to this :

public function getUserInformation($phone)
{
    $stmt = $this->conn->prepare("SELECT Phone, Name, Birthdate, Address FROM user WHERE Phone=?");
    $stmt->bind_param("s",$phone);

    if($stmt->execute()) {
        $stmt->bind_result($arr['Phone'], $arr['Name'], $arr['Birthdate'], $arr['Address']);
        while ($stmt->fetch()) {
            $user[] = $arr;
        }
        $stmt->close();

        return $user;
    } else
        return NULL;
}
  • Why are you telling `bind_param()` that the value is an integer in the edited code? In your working code you set it as a string and it is a string as it starts with a `+`. Don't change the `$stmt->bind_param("s",$phone);` line. – jeroen Aug 09 '18 at 08:38
  • Maybe if you add a bit of error checking you would see some errors – RiggsFolly Aug 09 '18 at 08:40
  • Add `ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` to the top of your script. This will force any mysqli_ errors to generate an Exception that you can see on the browser and other errors will also be visible on your browser. – RiggsFolly Aug 09 '18 at 08:41
  • You also bind to `$phone` and read `$id` – Nigel Ren Aug 09 '18 at 08:43
  • @jeroen man i'ts `s` not `i` i've changed, honestly i'm looking for this for a while, i've forgot to change things here, also $id changed to $phone – Mohammad Eskandari Aug 09 '18 at 08:56
  • @NigelRen i'm sorry it was phone, i've forgot to change it here – Mohammad Eskandari Aug 09 '18 at 08:57
  • @RiggsFolly Edited and u can see error now. everything was working great with get_result on localhost, but it just keep getting `Call to undefined method mysqli_stmt::get_result()` on on real host, and host admin just don't care. – Mohammad Eskandari Aug 09 '18 at 08:59
  • Looks like you are going to have to change the code to use [`bind_result()`](http://php.net/manual/en/mysqli-stmt.bind-result.php). If the server does not have `mysqlind` installed there is no other option – RiggsFolly Aug 09 '18 at 09:20
  • @RiggsFolly man i've fixed it. Thanks for responds. Thanks. – Mohammad Eskandari Aug 09 '18 at 09:22
  • Hi, then post an answer, so the question has some value to others that may find it – RiggsFolly Aug 09 '18 at 09:23
  • 1
    @RiggsFolly it's done. Thanks mate. – Mohammad Eskandari Aug 09 '18 at 09:26

1 Answers1

2

Solution was to change the code to this. Thanks for Responds from @jereon, @RiggsFolly, NiggelRen.

public function getUserInformation($phone)
{
    $stmt = $this->conn->prepare("SELECT Phone, Name, Birthdate, Address FROM user WHERE Phone=?");
    $stmt->bind_param("s",$phone);

    if($stmt->execute()) {
        $stmt->bind_result($arr['Phone'], $arr['Name'], $arr['Birthdate'], $arr['Address']);
        while ($stmt->fetch()) {
            $user[] = $arr;
        }
        $stmt->close();

        return $user;
    } else
        return NULL;
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149