emilysnothere Posted December 8, 2009 Share Posted December 8, 2009 Please help me, I'm trying to check whether the username already exists but I keep getting the error "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in" and I have no idea why. Here is my php code. The fieldname (username) and $number (of rows) get sent through to a formClass. if($_POST['submit']) { // store all the values in an array to use when checking the data and when re-writing the form $form->storeValues($_POST); //check the username doesnt exist $userquery = "SELECT username FROM tbl_user WHERE username = '{$_POST['username']}'"; $form->query($userquery); $number = mysql_num_rows($form); // b) Check each field $form->checkUsername('username', $number); $form->checkEmpty('firstname'); $form->checkEmpty('lastname'); .....etc Here's what happens in the formClass //checks whether the input field is empty. If it is a sticky form will appear, requiring the user to fill out the input field. function checkUsername($fieldName, $number){ //checks if the field has been filled out correctly if (strlen($this->valuesArray[$fieldName])==0){ $this->totalErrors ++; $this->messageArray[$fieldName]= 'This field cannot be empty'; //checks if more than one of the usernames exist }else if($number > 0){ $this->messageArray[$fieldName]= 'Sorry, username already exists.'; }else{ $this->messageArray[$fieldName]='Good'; $this->escapeData ($fieldName); } } So yeah, I'm stumped, though its probably something obvious. Also if anyone has any suggestions of a better way to do this please share. Quote Link to comment https://forums.phpfreaks.com/topic/184385-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/ Share on other sites More sharing options...
cags Posted December 8, 2009 Share Posted December 8, 2009 You are getting the error because the value of $form is not a resource object. What does the function $form->query() function look like? I would guess it's essentially along the lines (though hopefully more involved) of... function query($sql) { return mysql_query($sql); } That being the case you should have something more like... $result = $form->query($userquery); $number = mysql_num_rows($result); Obviously that's highly theoretical because I've made assumptions about the code. Quote Link to comment https://forums.phpfreaks.com/topic/184385-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-973351 Share on other sites More sharing options...
emilysnothere Posted December 8, 2009 Author Share Posted December 8, 2009 Hey, my function is function query($query){ $this->result = @$this->mysqli->query($query) or die ($query.'<br />'. $mysqli->error.'<br />'. __FILE__.__LINE__.__FUNCTION__); } I tried your suggestion, but I'm still getting the error (I guess because that isnt really the same function) Oh well hopefully I can get some help in class today as well Quote Link to comment https://forums.phpfreaks.com/topic/184385-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-973697 Share on other sites More sharing options...
premiso Posted December 8, 2009 Share Posted December 8, 2009 Shouldn't it be $this->mysqli->error? Or should the query part be $mysqli->query Also remove the @, there is no reason to suppress errors, later on when it is on production you can turn error display off. $this->result = $this->mysqli->query($query) or trigger_error($query.'<br />'. $this->mysqli->error.'<br />'. __FILE__.__LINE__.__FUNCTION__); Quote Link to comment https://forums.phpfreaks.com/topic/184385-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-973703 Share on other sites More sharing options...
emilysnothere Posted December 8, 2009 Author Share Posted December 8, 2009 oh okay, I've changed that too thanks Still getting the mysql_num_rows error though Quote Link to comment https://forums.phpfreaks.com/topic/184385-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-973739 Share on other sites More sharing options...
cags Posted December 9, 2009 Share Posted December 9, 2009 Since you appear to be storing the resultset in $this->result, it is $form->result that needs to be passed to mysql_num_rows. $number = mysql_num_rows($form->result); Quote Link to comment https://forums.phpfreaks.com/topic/184385-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-974068 Share on other sites More sharing options...
PFMaBiSmAd Posted December 9, 2009 Share Posted December 9, 2009 You are using mysqli and you are using OOP. You are mixing in mysql_ functions. You have got to use the mysqili OOP methods. You cannot use mysqli procedural functions with an mysqli OOP result either. Quote Link to comment https://forums.phpfreaks.com/topic/184385-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-974071 Share on other sites More sharing options...
cags Posted December 9, 2009 Share Posted December 9, 2009 I completely missed they were using mysqli, I think I should go back to bed. Quote Link to comment https://forums.phpfreaks.com/topic/184385-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-974072 Share on other sites More sharing options...
emilysnothere Posted December 9, 2009 Author Share Posted December 9, 2009 Thanks for the help I fixed it by putting the query etc in my formClass function checkUsername($fieldName){ if (strlen($this->valuesArray[$fieldName])==0){ $this->totalErrors ++; $this->messageArray[$fieldName]= 'This field cannot be empty'; }else{ $userquery = "SELECT username FROM tbl_user WHERE username = '".$this->valuesArray[$fieldName]."'"; //exit($userquery); $this->query($userquery); if($this->result->num_rows > 0){ $this->totalErrors ++; $this->messageArray[$fieldName]= 'Sorry, username already exists.'; }else{ $this->messageArray[$fieldName]='Good'; $this->escapeData ($fieldName); } } } so its all in one place just called by $form->checkUsername('username'); (for people trying this in the future I guess...) Quote Link to comment https://forums.phpfreaks.com/topic/184385-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-974284 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.