Jump to content

bind_result() empty


gmc1103

Recommended Posts

Hi

 

I'm having problems with this function, it doesn't bind_result() any variables. So my "While" doesn't work either

public function getUserByEmailAndPassword($email, $password) {
		 
		$stmt = $this->conn->prepare("SELECT unique_id,name,email,created_at,updated_at, encrypted_password , salt FROM users WHERE email = ?");
		$stmt->bind_param("s", $email);
		if ($stmt->execute()) {
		$stmt->store_result();
		$num_of_rows = $stmt->num_rows;
		$stmt->bind_result($aid, $aname, $aemail, $acreated_at, $aupdated_at , $aencrypted_password , $asalt);
		echo $aemail; //no echo
		while ($user=$stmt->fetch()) {
			echo "inside while"; // no echo
		$user[0] = $aid;
		$user[1] = $aname;
		$user[2] = $aemail;
		$user[3] = $acreated_at;
		$user[4] = $aupdated_at;
		$user[5] = $aencrypted_password;
		$user[6]= $asalt;
		$user[7] = $password; 
		}
		$stmt->free_result();
		$stmt->close();
		return $user;

		} else {
			echo "no $stmt->execute()"; //no echo
		return NULL;
		}
	}

Any error in this code?

 

Thanks

Link to comment
Share on other sites

Given that the query will only return a single row, then you don't want a while() loop. Also, as you want to put the returned row into an indexed array use get_result() instead of store_result(). EG

            $result = $stmt->get_result();
            $num_of_rows = $result->num_rows;
            $user = array();
            if ($num_of_rows > 0) {
                $user = $result->fetch_row();
                $user[] = $password;
            } 
            $stmt->close();
            return $user;
Link to comment
Share on other sites

Hi Barand

 

Thank's for your reply

I'm having issues

Call to undefined method mysqli_stmt::get_result();

 

But the strange is i have this method in another function and i don't have this error.

 

This is the function and this one works

public function storeUser($name, $email, $password) {
        $uuid = uniqid('', true);
        $hash = $this->hashSSHA($password);
        $encrypted_password = $hash["encrypted"]; // encrypted password
        $salt = $hash["salt"]; // salt
 
        $stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, NOW())");
        $stmt->bind_param("sssss", $uuid, $name, $email, $encrypted_password, $salt);
        $result = $stmt->execute();
        $stmt->close();
 
        // check for successful store
        if ($result) {
            $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
            $stmt->bind_param("s", $email);
            $stmt->execute();
            $user = $stmt->get_result()->fetch_assoc();
            $stmt->close();
 
            return $user;
        } else {
            return false;
        }
    }

Strange....

Link to comment
Share on other sites

what php version are you using?

 

and for the the code in post #3, is that code being ran on the same server as the current problem code and are you sure it is actually taking the if ($result) { execution path? i can think of at least one case where the INSERT query in the code in post #3 is failing due to an error, when there is already a row in that database table due to previous code-version/testing, and it isn't trying to run the code where the ->get_result() method call is at.

 

are you open to using the PDO database class instead or the mysqli class? PDO is much more constant and easy to use, especially with prepared queries.

 

edit: i just looked at your previous threads. you are/were using PDO. why did you take a step backwards with this code and use mysqli?

 

edit2: and the Call to undefined method mysqli_stmt::get_result() error is exactly the same error you were getting in a thread on November the 2nd. so, i doubt your code in post #3 in this thread is actually doing what you think or if it is running, it's not running on the same server as the current code.

Edited by mac_gyver
Link to comment
Share on other sites

Hi

 

Ok, to avoid any mistake this is the original code i'm trying to handle.

<?php


class DB_Functions {
    private $conn;
    function __construct() {
        require_once 'DB_Connect.php';
        // connecting to database
        $db = new Db_Connect();
        $this->conn = $db->connect();
    }

    function __destruct() {
        
    }
    public function storeUser($name, $email, $password) {
        $uuid = uniqid('', true);
        $hash = $this->hashSSHA($password);
        $encrypted_password = $hash["encrypted"]; // encrypted password
        $salt = $hash["salt"]; // salt

        $stmt = $this->conn->prepare("INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, NOW())");
        $stmt->bind_param("sssss", $uuid, $name, $email, $encrypted_password, $salt);
        $result = $stmt->execute();
        $stmt->close();

        if ($result) {
            $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
            $stmt->bind_param("s", $email);
            $stmt->execute();
            $user = $stmt->get_result()->fetch_assoc();
            $stmt->close();

            return $user;
        } else {
            return false;
        }
    }

    public function getUserByEmailAndPassword($email, $password) {
		 
 
        $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
 
        $stmt->bind_param("s", $email);
 
        if ($stmt->execute()) {
            $user = $stmt->get_result()->fetch_assoc();
            $stmt->close();
            return $user;
        } else {
            return NULL;
        }
    }
	
}

The first function "storeUser($name, $email, $password)" works since all the data is inserted into my DB

 

The second function "getUserByEmailAndPassword($email, $password)" i'm getting the following error.

 

Fatal error: Call to undefined method mysqli_stmt::get_result() in /home/xxxxx/public_html/mobile/include/DB_Functions.php on line 48

 

The line 48 is

$user = $stmt->get_result()->fetch_assoc();

Yes, Barand, i'm puzzled too..and if you check the first function i have get_result() there.

 

So...i'm lost

Link to comment
Share on other sites

what php version are you using?

 

and for the the code in post #3, is that code being ran on the same server as the current problem code and are you sure it is actually taking the if ($result) { execution path? i can think of at least one case where the INSERT query in the code in post #3 is failing due to an error, when there is already a row in that database table due to previous code-version/testing, and it isn't trying to run the code where the ->get_result() method call is at.

 

are you open to using the PDO database class instead or the mysqli class? PDO is much more constant and easy to use, especially with prepared queries.

 

edit: i just looked at your previous threads. you are/were using PDO. why did you take a step backwards with this code and use mysqli?

 

edit2: and the Call to undefined method mysqli_stmt::get_result() error is exactly the same error you were getting in a thread on November the 2nd. so, i doubt your code in post #3 in this thread is actually doing what you think or if it is running, it's not running on the same server as the current code.

 

 

Variable_name            Value                         

-----------------------  ------------------------------

innodb_version           5.5.46                        

protocol_version         10                            

slave_type_conversions                                 

version                  5.5.46-cll                    

version_comment          MySQL Community Server (GPL)  

version_compile_machine  x86_64                        

version_compile_os       Linux  

 

I must check how to use PDO in that case, do you have an example?

Link to comment
Share on other sites

there are two possibilities, either the ->get_result() method is not available (your php version is right, but the driver that php uses must also be the mysqlnd driver) or your code has some non-printing/character-encoded characters as part of that statement.

 

1) for the first possibility, does the output from a phpinfo() statement have a main section for the mysqlnd driver? the msyqlnd name and version number should also show up under the msyqli main section as part of the Client API library version information.

 

2) if you copy/pasted the code or are typing code on a non-English keyboard/operating system, i would delete and retype the entire line of code where the error is occurring at, making sure that only ASCII characters are being used.

 

as to PDO examples, your previous code using PDO would be where to look. all you are doing is preparing the sql statement, binding any input data, executing the query, and fetching any result.

Link to comment
Share on other sites

Hi

 

This is what i have with phpinfo();

MysqlI Support	enabled
Client API library version	5.5.46
Active Persistent Links	0
Inactive Persistent Links	0
Active Links	0
Client API header version	5.5.45
MYSQLI_SOCKET	/var/lib/mysql/mysql.sock

Directive	Local Value	Master Value
mysqli.allow_local_infile	On	On
mysqli.allow_persistent	On	On
mysqli.default_host	no value	no value
mysqli.default_port	3306	3306
mysqli.default_pw	no value	no value
mysqli.default_socket	no value	no value
mysqli.default_user	no value	no value
mysqli.max_links	Unlimited	Unlimited
mysqli.max_persistent	Unlimited	Unlimited
mysqli.reconnect	Off	Off

Regarding the code i'm changing to pdo, but i have problems regarding the password match

 

I'm using this

public function getUserByEmailAndPassword($email, $password) {
        $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = :email");
        $stmt->bindparam(":email", $email);
        if ($stmt->execute()) {
            $user = $stmt->fetch();
            $salt = $user['salt'];
            $encrypted_password1 = $user['encrypted_password'];
            echo $encrypted_password1;
            $hash = $this->checkhashSSHA($salt, $password);
            if ($encrypted_password1 == $hash) {
                return $user;
            }
        } else {
            return NULL;
        }
    }

But $user is always empty

Thanks

Edited by gmc1103
Link to comment
Share on other sites

As mentioned earlier, mysqli_stmt::get_result() is only available with the mysqlnd driver. If you don't have that method then you are using the old libmysqlclient, which is unfortunate. If you keep with the mysqli extension then you'll have to go the long way to get results, meaning bind_result() and fetch() and all that.

 

Speaking of that, your problem with $user. mysqli_stmt::fetch() only returns true/false/null as to whether there were any more results to fetch. To get actual data you need to bind the result values to variables, just like how you bound input values to variables. Each time you call fetch those variables will be updated with appropriate values.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.