Jump to content
Sign in to follow this  
farse

Is mysqli_stmt_get_result() in PHP 5.4?

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?

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

If you're getting a boolean that means that your query failed. Check to see what value mysqli_prepare() returns. If it returns a boolean false then you have to see the message that mysqli_error() shows.

Share this post


Link to post
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;

Share this post


Link to post
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>';

};

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
Sign in to follow this  

×
×
  • Create New...