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

Pearson95
  • 9
  • 3
  • $username = mysql_real_escape_string($username); Where is $username initially defined? – The One and Only ChemistryBlob Oct 26 '14 at 15:06
  • 1
    it's always your fault. don't blame PHP. – Karoly Horvath Oct 26 '14 at 15:07
  • 3
    Obligatory "don't use mysql_ functions" comment :P http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – rjdown Oct 26 '14 at 15:09
  • -1 Your code lacks error checking albeit you ask here for debugging help. Not helpful. – hakre Oct 26 '14 at 15:14
  • 1
    $result = mysql_query($query, $con) or die(mysql_error()); – I wrestled a bear once. Oct 26 '14 at 15:15
  • $username is at the top to define that i am getting the posted text box. Thank you for your obvious and unhelpful comment Karoly lol... Okay, what should i use instead of mysql_functions rjdown? What can i do to make the above work, can someone explain please? – Pearson95 Oct 26 '14 at 15:17
  • Try @Adelphia recommendation to see what the actual error is so we can identify the problem. `$result = mysql_query($query, $con) or die(mysql_error());` – Kypros Oct 26 '14 at 15:22
  • 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?](http://stackoverflow.com/q/845021/367456) – hakre Oct 26 '14 at 15:22
  • Okay, thanks you guys! Sorry for my incompetent mistakes. I have added or die(mysql_error()) at the end of all my relevant fields. Should this suffice for relevant error finding or is this a bad way of figuring it out? – Pearson95 Oct 26 '14 at 15:26
  • Sorry if I'm asking really obvious questions - i am a baby really... – Pearson95 Oct 26 '14 at 15:27
  • If you get a white page (or a different error), please refer to [the PHP Error Reference we have on site](http://stackoverflow.com/q/12769982/367456). – hakre Oct 26 '14 at 15:40
  • I have changed everything and no errors except "Undefined index"... however, even though i have the same error on the registration page and that works completely fine to insert data into a database now i get the same error on my login script and get a white page... any ideas? – Pearson95 Oct 26 '14 at 17:13

1 Answers1

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'";
Kypros
  • 2,997
  • 5
  • 21
  • 27
  • 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 Oct 26 '14 at 15:10
  • @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.. – I wrestled a bear once. Oct 26 '14 at 15:14
  • Okay, i apologise. New to coding and all this stack overflow business. – Pearson95 Oct 26 '14 at 15:21
  • This answer does not acknowledge the fact that Pearson95 should stop using the mysql_* extensions, as per @rjdown's comment. – muttley91 Oct 26 '14 at 15:55
  • 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? – Pearson95 Oct 26 '14 at 15:58