Jump to content

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resourc


emilysnothere

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :D

Link to comment
Share on other sites

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__);

Link to comment
Share on other sites

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...)

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.