Find out number rows in php

Cree
Cree used Ask the Experts™
on
I am learning php by book and came across an issue.  I want to add a if statement that checks to see if a username has been used before. if it has..return an error, if not continue on.

my test page can be found on : http://sql.platinumslvdb.com/

it just passes over the portion that does the query as if it doesn't find a row in the users table where the username doesn't match the  '$username'

Thanks!

<?php
require ('database.php');

$firstname = $_POST['first_name'];
$lastname = $_POST['last_name'];
$username = $_POST['username'];
$password = sha1($_POST['password']);
$email = $_Post['email'];
$homephone= $_Post['homephone'];
$cellphone = $_POST['cellphone'];
$password2 =sha1($_POST['password2']);

 if ($password != $password2) { 
      $error_message = "the passwords do not match!";
    include('index.php');
 } else {

 if (empty($username)) {
    $error_message = "Username cannot be blank or null.  Please enter a username";
    include('index.php');
}else {
  $query2  ="select * from users where username = '$username'";
  $res = mysql_query($query2);
  $db->exec($query2);
  ECHO mysql_num_rows($res) ;
  if (mysql_num_rows($res) != 0){
       $error_message = "Sorry but the Username '$username' already exists";
       include('index.php');
   }else {
$query= "Insert into users (username,password,firstname,lastname,email,homephone,cellphone) 
    Values('$username','$password','$firstname','$lastname','$email','$homephone','$cellphone')";

$db->exec($query);
echo '<br> Congratulations ';
echo $firstname;
echo "  ";
echo $lastname;
echo "you have been added to the database!";

}
}
 }
?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
I would probably add a die() underneath your ECHO mysql_num_rows($res); line to see what is being outputted.

Also, you are wide open to SQL injection. Don't forget to sanitize your data before using it in a query, or else you might wake up and have your database deleted or your site hacked.
Dave BaldwinFixer of Problems
Most Valuable Expert 2014
Commented:
You are mixing MySQL methods and that does not work.  There are 2 different MySQL drivers plus the PDO methods in PHP and when you open a connection using one method, you have to use that throughout your code because the other methods are other drivers that don't know anything about each other.  This page tells you about them: http://us2.php.net/manual/en/mysql.php

Author

Commented:
i added the die();
and now it's blank.

haven't gotten to sanitize portion of the book.

honestly, if someone want's to delete it.. i could care less.  i'm just trying to learn the language.

any other ideas?
Commented:
Dave is correct - it is probably just that you need to use the correct MySQL query calls. The mysql_query() function is one way, while $db->exec($query2) is another way. The database file you include at the top probably determines what kind of connection / method you're using - you just need to stick with the right one.

Commented:
$query2  ="select * from users where username = '$username'";
should be
$query2  ="select * from users where username = ' ". $username. " ' ";
the query is failing on wrong syntax and not executing.
Most Valuable Expert 2011
Top Expert 2016
Commented:
Not sure what book you're using, but maybe these articles will offer some help.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391.html
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_3939-Registration-and-Email-Confirmation-in-PHP.html

The syntax in this query appears to be correct.  A good strategy is to always check for the success or failure or a query.  MySQL is not a black box; it can and will fail for reasons that are not in your control.  This shows how to test for success or failure and get a message if things go awry.
$query2  ="select * from users where username = '$username'";
$res = mysql_query($query2);
if (!$res) die("FAIL: $query2 <br/>" . mysql_error() );

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial