mysql_numrows(): supplied argument is not a valid MySQL res

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
latoyale
Forum Commoner
Posts: 25
Joined: Fri Apr 04, 2008 1:16 am

mysql_numrows(): supplied argument is not a valid MySQL res

Post by latoyale »

Hello,

I am trying to figure out what the problem is with my code. I receive the following message:

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/content/j/a/b/jaberkjaber/html/askslicky/register.php on line 16

I read through some tutorials, checked my variables but I still don't see the problem. Please respond with any suggestions.

Thanks:

<?
session_start();
include("database.php");


function usernameTaken($username){
global $conn;
if(!get_magic_quotes_gpc()){
$username = addslashes($username);
}
$q = "select username from users where username = '$username'";
$result = mysql_query($q,$conn);
return (mysql_numrows($result) > 0);
}

/**
* Inserts the given (username, password) pair
* into the database. Returns true on success,
* false otherwise.
*/

function addNewUser($username, $password){
global $conn;
$q = "INSERT INTO users VALUES ('$username', '$password')";
return mysql_query($q,$conn);
}

/**
* Displays the appropriate message to the user
* after the registration attempt. It displays a
* success or failure status depending on a
* session variable set during registration.
*/

function displayStatus(){
$uname = $_SESSION['reguname'];
if($_SESSION['regresult']){
?>

<h1>Registered!</h1>
<p>Thank you <b><? echo $uname; ?></b>, your information has been added to the database, you may now <a href="login_success.php" title="Login">log in</a>.</p>

<?
}
else{
?>

<h1>Registration Failed</h1>
<p>We're sorry, but an error has occurred and your registration for the username <b><? echo $uname; ?></b>, could not be completed.<br>
Please try again at a later time.</p>

<?
}
unset($_SESSION['reguname']);
unset($_SESSION['registered']);
unset($_SESSION['regresult']);
}

if(isset($_SESSION['registered'])){
/**
* This is the page that will be displayed after the
* registration has been attempted.
*/
?>

<html>
<title>Registration Page</title>
<body>

<? displayStatus(); ?>

</body>
</html>

<?
return;
}

/**
* Determines whether or not to show to sign-up form
* based on whether the form has been submitted, if it
* has, check the database for consistency and create
* the new account.
*/

if(isset($_POST['subjoin'])){
/* Make sure all fields were entered */
if(!$_POST['username'] || !$_POST['password']){
die('You didn\'t fill in a required field.');
}

/* Spruce up username, check length */
$_POST['username'] = trim($_POST['username']);
if(strlen($_POST['username']) > 30){
die("Sorry, the username is longer than 30 characters, please shorten it.");
}

/* Check if username is already in use */
if(usernameTaken($_POST['username'])){
$use = $_POST['username'];
die("Sorry, the username: <strong>$use</strong> is already taken, please pick another one.");
}

/* Add the new account to the database */
$md5pass = md5($_POST['password']);
$_SESSION['reguname'] = $_POST['username'];
$_SESSION['regresult'] = addNewUser($_POST['username'], $md5pass);
$_SESSION['registered'] = true;
echo "<meta http-equiv=\"Refresh\" content=\"10;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
return;
}
else{

?>

<html>
<title>Registration Page</title>
<body>
<h1>Register</h1>
<form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="username" maxlength="30"></td></tr>
<tr><td>Password:</td><td><input type="password" name="password" maxlength="30"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="subjoin" value="Join!"></td></tr>
</table>
</form>
</body>
</html>


<?
}
?>
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: mysql_numrows(): supplied argument is not a valid MySQL res

Post by Kieran Huggins »

try

Code: Select all

$result = mysql_query($q,$conn) or die(MySQL_query());
I always use that when developing so I can see when my queries fail (and why).
latoyale
Forum Commoner
Posts: 25
Joined: Fri Apr 04, 2008 1:16 am

Re: mysql_numrows(): supplied argument is not a valid MySQL res

Post by latoyale »

When i put that code in I now get the following error:

Warning: Wrong parameter count for mysql_query() in /home/content/j/a/b/jaberkjaber/html/askslicky/register.php on line 17

I don't see how my count is wrong, the code is supposed to have 0 in it to test whether the user is already logged in.
latoyale
Forum Commoner
Posts: 25
Joined: Fri Apr 04, 2008 1:16 am

Re: mysql_numrows(): supplied argument is not a valid MySQL res

Post by latoyale »

Its probably giving me this error since:

$result = mysql_query($q,$conn) or die(MySQL_query());;
return (mysql_numrows($result) > 0);

the variable $result probably can't contain the die function. So now I am kind of back at square one.... :(
latoyale
Forum Commoner
Posts: 25
Joined: Fri Apr 04, 2008 1:16 am

Re: mysql_numrows(): supplied argument is not a valid MySQL res

Post by latoyale »

After inserting the mysql_error function, the following message occurred:

Unknown column 'username' in 'field list'

What can I possibly be missing in the code?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: mysql_numrows(): supplied argument is not a valid MySQL res

Post by onion2k »

That error is telling you that your query isn't working because the "username" field is missing from the database table you're trying to search. Have a look at the `users` table and see if there's a column called username.
Post Reply