Search code examples
phpapinullmysqlnd

how to fix get_result and fetch_assoc with bind_result and fetch(wtithout getting Null)?


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

Solution

  • 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;
    }