0

I have this piece of PHP code:

<?php
$username=$_POST['username'];
$password=$_POST['password'];

if($username&&$password){
$connect=mysql_connect("localhost","root","") or die(" Couldnt connect");
mysql_select_db("phplogin") or die ("Can't find database" .mysql_error());  
$query=mysql_query("SELECT * users WHERE username='$username' ");
$numrows=mysql_num_rows($query);
if (!$query) {
die('Invalid query: ' . mysql_error());
}
}
else
die ("Enter username and password!") .mysql_error();
?>

However, when I try to run this code I get these errors:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\PHP testing\login.php on line 9

and

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'users WHERE username='alex'' at line 1

Can someone explain to me what I'm I doing wrong here?

3
  • -1 for posting SQL-injectable code Commented Jun 4, 2011 at 13:26
  • 5
    ^Johan he(Mentalhead) is here on SO to learn by mistakes so you can comment your view without -1. Commented Jun 4, 2011 at 17:30
  • @Johan if he knew what are you writing about he wouldn't ask about... mysql_num_rows() :D Commented Oct 5, 2012 at 15:36

5 Answers 5

5

You must specify a table from which you're selecting with FROM keyword:

$query=mysql_query("SELECT * FROM users WHERE username='$username' ");
$numrows=mysql_num_rows($query);
Sign up to request clarification or add additional context in comments.

1 Comment

You're right, I completely missed the FROM keyword, thanks a lot!
5

you should really check for errors after your query, then the system will tell you what is wrong

$query = mysql_query("SELECT * users WHERE username='$username' ");

if (mysql_error() {
   die(mysql_error());
}

$numrows = mysql_num_rows($query);

as @mike commented, your select query is missing the from bit

"SELECT * FROM users WHERE username='$username' "

Comments

4

Well Your code is vulnerable to SQL Injection Attack

$username=$_POST['username'];
$password=$_POST['password'];

instead of above use this code 

$username= mysql_real_escape_string($_POST['username']);
$password=mysql_real_escape_string($_POST['password']);

1 Comment

I know it's vulnerable, it's just for testing and learning purposes, but thanks for this security tip.
1
$connect = mysql_connect("localhost","root","") or die("Couldn't connect!");
mysql_select_db("phplogin") or die("Couldn't find db");
$result = mysql_query("SELECT * FROM admin", $connect);
$numrows = mysql_num_rows($result);

and it will evaluate resource

Comments

0
$query = mysql_query("SELECT * users WHERE username='$username' ");
if (mysql_error() {
   die(mysql_error());
}
$numberOfRows = mysql_num_rows($query);
echo $numberOfRows;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.