MySQLi prepared statement incapable of get_result ()

I am completely confused by mySQLi. Although I have been using mysql procedural calls for many years, I want to get used to preparing prepared instructions for the db security / mySQL protection that it offers. I am trying to write a simple select statement (yes, I know that a procedure call procedure for this offers a performance improvement). When I start, I get all the echoes until I hit the component $result = $stmt->get_result(); . It all seems pretty simple to me, but I'm wasting time after reading mySQLi instructions. Any ideas why this would be unsuccessful?

* note: this is a test environment, and although character processing / escaping is not performed, I only pass valid content to the $ username and $ email variables. In addition, I went through all the SOs to find a solution to my problem.

 function checkUsernameEmailAvailability($username, $email) { //Instantiate mysqli connection @$mysqli = new mysqli(C_HOST,C_USER,C_PASS,C_BASE) or die("Failed to connect to MySQL database..."); if (!$mysqli) { echo 'Error: Could not connect to database. Please try again later...'; exit; } else { echo 'mysqli created'; } /* Create a prepared statement */ if($stmt = $mysqli -> prepare("SELECT username,email FROM tb_users WHERE username=? OR email=?")) { echo '<br />MYSQLi: '; /* Bind parameters s - string, b - boolean, i - int, etc */ $stmt -> bind_param("ss", $username, $email); echo '<br />paramsBound...'; /* Execute it */ $stmt -> execute(); echo '<br />Executed'; $result = $stmt->get_result(); echo '<br />Result acquired'; /* now you can fetch the results into an array - NICE */ $myrow = $result->fetch_assoc(); echo '<br />Fetched'; /* Close statement */ /$stmt -> close(); echo '<br />Done mysqli'; } } 

Also, do I need to instantiate mysqli every time I call a function? I assume that they are not persistent db connections, as in procedural mysql. Yes, I know that this is a scope problem, and no, I could not understand the scope of this class variable. When I declared it outside the function, it was not available when I entered the function.

UPDATE if I change line 12 from:

 if($stmt = $mysqli -> prepare("SELECT username,email FROM tb_users WHERE username=? OR email=?")) { 

in

  $stmt = $mysqli->stmt_init(); if($stmt = $mysqli -> prepare("SELECT username,email FROM tb_users WHERE username=? OR email=?")) { if(!stmt) echo 'Statement prepared'; else echo 'Statement NOT prepared'; 

I get instructions NOT prepared. Now I'm even more confused ....

UPDATE: I contacted my hosting provider and apparently supports mySQLi and the mysqlnd driver is present. Perhaps there is a way to just check this out? Although in the past they used to give me fairly well-informed answers.

UPDATE ... AGAIN: I checked the capabilities of my server myself and found that while mysqli and PDO are present, mysqlnd is not. Thus, I understand why get_result () will not work (mysqlnd is required, I think), I still do not understand why the prepared statement itself will not work.

+8
php mysqli prepared-statement
source share
3 answers

After some checking, although mysqli is installed on my host, apparently the mysqlnd driver is missing. Therefore, get_result () cannot be used (php manual defines get_result as mysqlnd dependent), and additional coding will need to be done instead to process my result as we would like.

So I decided to try and find out how PDO works, and within minutes, voila !!!

Replace the above code as follows:

 function checkUsernameEmailAvailability($username, $email) { echo 'pdo about to be created'; $dsn = 'mysql:dbname='.C_BASE.';host='.C_HOST; $user = C_USER; $password = C_PASS; try { $dbh = new PDO($dsn, $user, $password); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); } $params = array(':username' => $username, ':email' => $email); try{ $sth = $dbh->prepare('SELECT username,email FROM tb_users WHERE username = :username OR email = :email '); } catch(PDOException $e) { echo 'Prepare failed: ' . $e->getMessage(); } try{ $sth->execute($params); } catch(PDOException $e) { echo 'Execute failed: ' . $e->getMessage(); } $result = $sth->fetch(PDO::FETCH_ASSOC); print_r($result); } 

How many errors were checked, as I might have thought, and no problems at all to crack in it!

Final code

 function checkUsernameEmailAvailability($username, $email) { $dsn = 'mysql:dbname='.C_BASE.';host='.C_HOST; $user = C_USER; $password = C_PASS; new PDO($dsn, $user, $password); $params = array(':username' => $username, ':email' => $email); $sth = $dbh->prepare('SELECT username,email FROM tb_users WHERE username = :username OR email = :email '); $sth->execute($params); $result = $sth->fetch(PDO::FETCH_ASSOC); print_r($result); } 
+12
source share

What version of php? You should check this:

What happened to mysqli :: get_result?

It seems like you need a bit-bit-too-relevant.

+1
source share

mysqli_stmt :: get_result is only available with the mysqlnd package. remove the php5-mysql package and install php5-mysqlnd instead

+1
source share

All Articles