0

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.";


}
?>

6
Contributors
24
Replies
26
Views
6 Years
Discussion Span
Last Post by tomato.pgn
0
if (mysql_num_rows($sql>0));{

should read

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

Edited by smantscheff: n/a

0

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.

Edited by TechySafi: n/a

0

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

0

Try

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

Edited by smantscheff: n/a

0

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());
0

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;
}
}
?>

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

mysql_select_db("mydb", $usethis);

Edited by TechySafi: n/a

0

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?

0

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.

0

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?

0

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

Edited by TechySafi: n/a

0
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."); 
}

Edited by TechySafi: n/a

0

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.

0

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?

0

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.

0

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

0

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

Edited by tomato.pgn: n/a

0

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.