Member Avatar for OldDeveloper01

I have recieved this, Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\...register.php on line 16

I dont understand to be honest what this mean as i am new to php and mysql.

What do i need to do to fix this problem?

Below is my php.

<?php

include("db.php");
if (isset($_POST) && isset($_POST) && isset($_POST))
{

//Prevent SQL injections
$username = mysql_real_escape_string($_POST);
$email = mysql_real_escape_string($_POST);

//Get MD5 hash of password
$password = md5($_POST);

//Check to see if username exists
$sql = mysql_query("SELECT username FROM users WHERE username = '".$username."'");
if (mysql_num_rows($sql>0));{
die ("Username taken.");
}

mysql_query("INSERT INTO users (username, password, email) VALUES ( '$username', '$password', '$email')") or die (mysql_error()); echo "Account created.";


}
?>

Recommended Answers

All 24 Replies

if (mysql_num_rows($sql>0));{

should read

if (mysql_num_rows($sql)>0);{
Member Avatar for OldDeveloper01

i have tried that and still i get the same message!

Member Avatar for TechySafi

Try this

$sql=mysql_query("SELECT username FROM users WHERE username='$username'"); //recheck two things, your table name is [B]users [/B] and you have a column named [B]username [/B].

That kinda error occurs when php can't make perfect mysql query. So there must be a wrong with query.

Member Avatar for OldDeveloper01

I have checked this and users is the table name and username is the column name.

Try

$sql = mysql_query("SELECT username FROM users WHERE username = '".$username."'") or die( mysql_error();
Member Avatar for TechySafi

Try

$sql = mysql_query("SELECT username FROM users WHERE username = '".$username."'") or die( mysql_error();

typo:

$sql = mysql_query("SELECT username FROM users WHERE username = '".$username."'") or die(mysql_error());
Member Avatar for OldDeveloper01

THat has got rid of that problem for now thanks.

but the message i get in reply is "no database selected" i assume that is something to with the include("db.php");

which i will show you beacause im certain there is a mistake somewhere, as i am new to this.


<?php

session_start();

mysql_connect("localhost", "", "");

mysql_select_db("mydb");

function user_login ($username, $password)
{
//take the username and prevent SQL injections
$username = mysql_real_escape_string($username);

//begin the query
$sql = mysql_query("SELECT * FROM users WHERE username = '".$username."' AND password = '".$password."' LIMIT 1");

//check to see how many rows were returned
$rows = mysql_num_rows($sql);
if ($rows<=0 )
{
echo "Incorrect username/password";
}
else
{
//have them logged in
$_SESSION = $username;
}
}
?>

Member Avatar for TechySafi
$usethis=mysql_connect("localhost", "root", ""); //root is default username, you should change it if you have different username

mysql_select_db("mydb", $usethis);
Member Avatar for OldDeveloper01

thansk that seems to have doen that, i have just tried to register and it says the username is already taken which is impossible becuase there are 0 records.

any ideas?

Member Avatar for TechySafi

Hahaha....ofcourse its already taken :)

I guess you running these on your local computer right? If so then you don't have to register someone named "root" :) It's pre-registered when you installed your MySQL.

Just try that code.

Member Avatar for OldDeveloper01

sorry now i am confused.

I did change that code as you said.

Yes i am using my local computer (localhost), using phpmyadmin.

i never entered root into the username field.

i typed random letters in, why are they not being registered to the database?

I think mistack is here
you typed like this
if (mysql_num_rows($sql>0));{

But it should like
this

if (mysql_num_rows($sql)>0) {
}


Remove Semicolon and Add one Brace

Member Avatar for TechySafi

Sorry for making you confuse. Trying to make you clear....here is your processing page

<?php 

include("db.php"); 
if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['email']))
{

//Prevent SQL injections 
$username = mysql_real_escape_string($_POST['username']); 
$email = mysql_real_escape_string($_POST['email']);

//Get MD5 hash of password 
$password = md5($_POST['password']);

//Check to see if username exists 
$sql = mysql_query("SELECT username FROM users WHERE username = '".$username."'") or die( mysql_error();
if (mysql_num_rows($sql)>0){ 
die ("Username taken."); 
} 

mysql_query("INSERT INTO users (username, password, email) VALUES ( '$username', '$password', '$email')") or die (mysql_error()); echo "Account created."; 


}
?>

And put this code in your db.php

<?php
$db = mysql_connect("localhost","root",""); 
   if (!$db)
  {
  die('Could not connect to Server: ' . mysql_error());
  }
 $data_base=mysql_select_db("mydb",$db);  
  if (!$data_base)
  {
  die('Could not connect to DataBase : ' . mysql_error());
  } 
?>

just run this, ans show me what does it says.

EDIT: A good catch by the commentor of above :)

Member Avatar for OldDeveloper01

i get username taken again!

Member Avatar for TechySafi
echo $count=mysql_num_rows($sql); //no need to echo but im just wondering what it is outputting, the output should be either 0 or 1
if ($count==1)
{ 
die ("Username taken."); 
}
Member Avatar for OldDeveloper01

it echoed 0! and again username taken.

Member Avatar for TechySafi

it echoed 0! and again username taken.

Most weird thing ever I heard :P I've no idea whats going on. And that piece of work working fine on my engine.

Member Avatar for OldDeveloper01

it is really frustrating i have tried several registration form and i always get the same problem. im using xampp and all the turoials i have folled have used this and they get it working fine. Is there a setting i need to change or something?

Member Avatar for TechySafi

nah I really have no idea :P

By the way make a cup of coffee and stay away from your pc for a while and then come back, relax and check everything again. And if you have some time then download wampserver or easyphp. Not recommending but try if you wish.

Insert a record using mysql console or phpmyadmin. I bumped into this before.

Member Avatar for OldDeveloper01

Thanks for your help and the advice. I could do with some coffee!

are you sending blank username...because it might treat null value as already taken...
other wise do this...n see.....
i would suggest u to debug ur code using this...n check what is the output....

<?php 

include("db.php"); 
if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['email']))
{

//Prevent SQL injections 
$username = mysql_real_escape_string($_POST['username']); 
$email = mysql_real_escape_string($_POST['email']);
echo $username."<br/>".$email."<br/>";
//Get MD5 hash of password 
$password = md5($_POST['password']);

//Check to see if username exists 
$sql = mysql_query("SELECT username FROM users WHERE username = '".$username."'");
echo mysql_num_rows($sql);
if (mysql_num_rows($sql)>0));{ 
die ("Username taken."); 
} 

mysql_query("INSERT INTO users (username, password, email) VALUES ( '".$username."', '".$password."', '".$email."')") or die (mysql_error()); echo "Account created."; 


}
?>

just show me whats the output...then i might go further for ur problem....
waiting for ur response whether it was useful...

Have you used mysql_select_db. If you haven't already, choose a database and see if that helps.

Have you used mysql_select_db. If you haven't already, choose a database and see if that helps.

I think so all database connection string and database selection is done in db.php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.