Jump to content

Is mysqli_stmt_get_result() in PHP 5.4?


farse

Recommended Posts

I want to convert an existing site to use prepared statements. This will be much easier if I can get the result of a SELECT query as an array to replace the array returned by mysqli_fetch_array. I am using PHP 5.4 with mysqlnd activated and should be able to use mysqli_stmt_get_result(stmt) to get such an array. But that function does not work and does not seem to be available at all. But all the documentation I'be read says it should be available in PHP 5.4.

 

$query = "SELECT col1 FROM contactinfo WHERE col2 = ? && col3 = ? && (col4 = ? || col5 = ?) ";
$stmt = mysqli_prepare($connect, $query);
mysqli_stmt_bind_param($stmt, "ssss", $var1, $var2, $var3, $var4);
mysqli_stmt_execute($stmt);
$row = mysqli_stmt_get_result(stmt);
$newvar = $row[0];

 

The actual query is much longer but this is the form.

 

If I can't use mysqli_stmt_get_result, I'll probably abandon the transition. Can someone offer advice on my next move?

Link to comment
Share on other sites

I probably should have put all the code up initially but here it is--with late edits:

 

$query = "SELECT col1 FROM contactinfo WHERE col2 = ? && col3 = ? && (col4 = ? || col5 = ?) ";

$stmt = mysqli_prepare($connect, $query);

mysqli_stmt_bind_param($stmt, "ssss", $var1, $var2, $var3, $var4);

mysqli_stmt_execute($stmt);

if (!mysqli_stmt_get_result($stmt)) {

report the result in a user message

}

else {

$result = mysqli_stmt_get_result($stmt);

$row = mysqli_fetch_array($result);

$newvar = $row[0];

echo "$newvar\n";

exit;

}

I'm not getting the user message and get a blank screen. The data is in the db and is an integer.

Also, the code works OK with:

mysqli_stmt_bind_result($stmt, $col1);

mysqli_stmt_fetch($stmt)

echo "$col1\n";

exit;

Edited by farse
Link to comment
Share on other sites

It sounds like an error is occurring but it's not showing up.

 

Put this at the beginning of your file:

ini_set('display_errors', 1);
error_reporting(E_ALL);

If there's a syntax error, though, you might need to look in your server's error logs.

Link to comment
Share on other sites

When I saw:

 

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /Applications/MAMP/htdocs/TEST/accountinsert.php on line 188

 

I realized I had not changed the ini file when I downloaded the latest MAMP a few days ago--and have done that.

 

Still, the error message seems to mean that mysqli_stmt_get_result is not returning data.

 

Also, I'm coding in TextWrangler which colors the PHP code pretty consistently and does not seem to recognize get_result as legitimate.

Link to comment
Share on other sites

We know the code works through mysqli_stmt_execute because it works fine when mysqli_stmt_get_result is replace with:

mysqli_stmt_bind_result($stmt, $col1);

mysqli_stmt_fetch($stmt)

echo "$col1\n";

exit;

Link to comment
Share on other sites

Here's the answer I found elsewhere:

 

// You were really close! I think the issue was that when you called

// mysqli_stmt_get_result in the if() you didn't realize it was consuming the

// data set up by the execute and thus your second call to it didn't get any

// records.

// you should assign into $result immediately and then test that in the if()

if ( $result = mysqli_stmt_get_result($stmt) ) {

$row = mysqli_fetch_array($result);

$col1 = $row[0];

echo phpversion();

echo "<p>$col1</p>";

exit;

}

else {

echo '<p>We got nothing</p>';

};

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...