-3

I have a problem with my script: i believe mysql_num_rows won't find anything from my database even though i know there is something in there (two records actually).... Anyone help?

<?php
$con = mysql_connect("localhost","root","root") or die(mysql_error());
$db =  mysql_select_db("usersData", $con) or die(mysql_error());

$username = mysql_real_escape_string($username, $con) or die(mysql_error());
$query = "SELECT * FROM `usersInfo` WHERE `Username`='$username'";
$result = mysql_query($con, $query) or die(mysql_error());
$num_rows = mysql_num_rows($result) or die(mysql_error());

if($num_rows == 0)
{
    //header('Location: login.php');    
    echo "meow";
}
?>

i hope this is a better piece of code now. However, when i run it it now gives me a white page?

12
  • $username = mysql_real_escape_string($username); Where is $username initially defined? Commented Oct 26, 2014 at 15:06
  • 1
    it's always your fault. don't blame PHP. Commented Oct 26, 2014 at 15:07
  • 3
    Obligatory "don't use mysql_ functions" comment :P stackoverflow.com/questions/12859942/… Commented Oct 26, 2014 at 15:09
  • 1
    $result = mysql_query($query, $con) or die(mysql_error()); Commented Oct 26, 2014 at 15:15
  • 1
    @Pearson95: You can greatly benefit by turning error reporting to the highest level while writing your code: How to get useful error messages in PHP? Commented Oct 26, 2014 at 15:22

1 Answer 1

3

Check these two lines:

$username = mysql_real_escape_string($username);
$query = "SELECT * FROM `usersInfo` WHERE `Username`='$Username'";

variable $username is different than $Username

Variables in php are case sensitive so it is like you are using two different variables here.

Fix your query so it uses the same lower case $username variable you are setting above:

$query = "SELECT * FROM `usersInfo` WHERE `Username`='$username'";
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your response. I edited it but i still get a 0 result from it all. ` $con=mysql_connect("localhost","root","root"); $db = mysql_select_db("usersInfo", $con); $username = mysql_real_escape_string($_POST['username']); $query = "SELECT * FROM usersInfo WHERE Username='$username'"; $result = mysql_query($query, $con); $num_rows = mysql_num_rows($result); if(mysql_num_rows($result) == 0) { //header('Location: login.php'); echo "meow"; }else{ echo "now use session"; } `
@Pearson95 don't do that. If you have changed your code, change your question. Don't post entire code blocks in the comments where noone can read them..
Okay, i apologise. New to coding and all this stack overflow business.
This answer does not acknowledge the fact that Pearson95 should stop using the mysql_* extensions, as per @rjdown's comment.
i have realised this and changed my mysql_functions to mysql_functions as an error stated using mysqli or PDO. Is this now better as i do not use mysql functions?

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.