Jump to content

Potential PHP Bug (mysqli)


null7238

Recommended Posts

Hi everyone, I wanted to some investigating before I label this as a bug, but from what I can see I'm not getting expected results. It appears as if mysqli_stmt_result_metadata does not return the proper object. It should be of mysqli_result type, but yet when I run mysqli_num_rows against it, I get 0, always. If I do a direct mysqli_query and take the resultset back, I can call mysqli_num_rows and it returns the proper value.

 

Example #1(doesn't work):

 

if($this->statement = mysqli_prepare($this->databaseConnection, "SELECT 1 FROM DIGIUSERS WHERE UNAME = ?"))

{

mysqli_stmt_bind_param($this->statement, 's', $username);

mysqli_stmt_execute($this->statement);

$this->errorQuery();

$result = mysqli_stmt_result_metadata($this->statement);

//Ooops, the username is already taken! exit with status of -7

if(mysql_num_rows($result) > 0) {

    mysqli_rollback($this->databaseConnection);

    exit("<STATUS>-7</STATUS></USER>");

}

mysqli_stmt_close($this->statement);

}

 

Example #2 (works but i cant bind variables):

$result = mysqli_query($this->databaseConnection, "SELECT 1 FROM DIGIUSERS WHERE UNAME = $username");

 

$this->errorQuery();

 

//Ooops, the username is already taken! exit with status of -7

if(mysqli_num_rows($result) > 0) {

    mysqli_rollback($this->databaseConnection);

    exit("<STATUS>-7</STATUS></USER>");

}

 

I have variable binding working just fine elsewhere on the site so I know it's not that. The only thing I can think of is that mysqli_stmt_result_metadata is not returning the proper object. For now i will use #2 but, very begrudgingly....

Link to comment
Share on other sites

As per my usual fashion, I kept cracking at this to find a solution that would work for me. If anyone runs into this problem where you need to see if rows exist, do this instead.

 

if($this->statement = mysqli_prepare($this->databaseConnection, "SELECT 1 FROM DIGIUSERS WHERE UNAME = ?"))

{

mysqli_stmt_bind_param($this->statement, 's', $username);

mysqli_stmt_execute($this->statement);

$this->errorQuery();

//$result = mysqli_stmt_result_metadata($this->statement);

//Ooops, the username is already taken! exit with status of -7

if(mysqli_stmt_fetch($this->statement)) {

    mysqli_rollback($this->databaseConnection);

    exit("<STATUS>-7</STATUS></USER>");

}

mysqli_stmt_close($this->statement);

}

 

that way if you fail to fetch a row, it will return null...or an error, either way it only returns true if it grabs row(s). Hope this helps someone out in the future.

 

Still hoping someone can key in on the metadata issue, I have a feeling this is a bug in the API.

Link to comment
Share on other sites

If you want to check for number of rows, the result meta data is not the function you want.  The result set that contains the row data (and thus number of rows) is the one returned by get_result();

 

 


mysqli_stmt_bind_param($this->statement, 's', $username);
mysqli_stmt_execute($this->statement);
$result = mysqli_stmt_get_result($this->statement);
if(mysql_num_rows($result) > 0) {
}

 

Link to comment
Share on other sites

Thank you for the replies. I was using the mysqli function sorry, it's been hectic today and I just slapped the code in here quickly.

 

It still does not make sense to me why the meta data does not work. Either documentation needs to be changed to let users know it's not an actual resultset...or something...

 

kicken, thank you for that but I believe at this point the solution I found is more efficient anyways :)

Link to comment
Share on other sites

Either documentation needs to be changed to let users know it's not an actual resultset...or something...

 

The documentation mention that:

Note:

This result set pointer can be passed as an argument to any of the field-based functions that process result set metadata, such as:

[...snip list of valid functions...]

 

 

However I agree attempting to fetch a row is better than checking for num rows returned.  Another alternative would be to use the COUNT(*) function which will always return a row and would have a value of 0 or 1 based on if there was a match found.

 

Link to comment
Share on other sites

User comments in the manual say:

Please note that this method requires the mysqlnd driver. Othervise you will get this error: Call to undefined method mysqli_stmt::get_result()

 

If your going to test by fetching a row though, you do not need this function.

 

 

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.