null7238 Posted December 5, 2011 Share Posted December 5, 2011 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.... Quote Link to comment https://forums.phpfreaks.com/topic/252479-potential-php-bug-mysqli/ Share on other sites More sharing options...
null7238 Posted December 5, 2011 Author Share Posted December 5, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252479-potential-php-bug-mysqli/#findComment-1294464 Share on other sites More sharing options...
kicken Posted December 5, 2011 Share Posted December 5, 2011 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) { } Quote Link to comment https://forums.phpfreaks.com/topic/252479-potential-php-bug-mysqli/#findComment-1294465 Share on other sites More sharing options...
PFMaBiSmAd Posted December 5, 2011 Share Posted December 5, 2011 AND you cannot use mysql_num_rows (non-i ) function on a mysqli result. You would need to use mysqli_num_rows (with an i ) Quote Link to comment https://forums.phpfreaks.com/topic/252479-potential-php-bug-mysqli/#findComment-1294468 Share on other sites More sharing options...
null7238 Posted December 5, 2011 Author Share Posted December 5, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/252479-potential-php-bug-mysqli/#findComment-1294472 Share on other sites More sharing options...
kicken Posted December 5, 2011 Share Posted December 5, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252479-potential-php-bug-mysqli/#findComment-1294475 Share on other sites More sharing options...
null7238 Posted December 5, 2011 Author Share Posted December 5, 2011 Unknown function mysqli_stmt_get_result, looks like this might just be in an alpha build. This is a partially prod environment so I can't be running alpha builds Can anyone confirm? Thank you again for your super fast replies Quote Link to comment https://forums.phpfreaks.com/topic/252479-potential-php-bug-mysqli/#findComment-1294476 Share on other sites More sharing options...
kicken Posted December 5, 2011 Share Posted December 5, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252479-potential-php-bug-mysqli/#findComment-1294478 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.