tags:

views:

31

answers:

3

Hi, Being quite new with PHP, I cannot find any solution why this does not work. The query is OK and the resource is returned. But I dunno why fetch_assoc does not print values. Thanks

$query=sprintf("SELECT ID,NAME FROM USERS WHERE PASS='%s' AND NAME='%s'",mysql_real_escape_string($p),mysql_real_escape_string($n));

$result=mysql_query($query);

if ($result)
 {

while ($row = mysql_fetch_assoc($result)) {
    echo $row['ID'];
    echo $row['NAME'];
}
}

}
+1  A: 

Some simple questions to start with:

  • Have you done a var_dump($row) to see what it returns?
  • Are you sure that the name and the password you specify are actually in the database?
  • Have you encrypted the password in the database (and not in the query)?
  • Have you a valid database connection ? (I know the answer is yes but a double check won't harm anyone and maybe save some headache)

Edit:

  • Added a link to the man page for var_dump.
  • As already suggested use mysql_error() to find what goes wrong. (A simple echo mysql_error(); after $result=mysql_query($query); will suffice)
  • write down out the query to see if something goes wrong with the escaping.
Eineki
Connection is OK. No encryption done and I can see the values in the database. I am pretty new to PHP so I dont know about var_dump.
Petr
@Petr, http://php.net/functionName will get you the documentation for any function, for example http://php.net/var_dump will get you docs for var_dump()
Unkwntech
A: 

Hello, 1.Echo out Your $query to see if it is what You like to be for debugging purposes; 2. Check $row['ID'] AND ['NAME'] if they are really UPPER letters; 3. Use mysql error reporting after if ($result){....} else { echo mysql_errno($link) . ": " . mysql_error($link) . "\n"; } where $link is Your DB handle.

arunas_t
2. Yes, they are. In the query they are reported uppercase and php maintains the case used in the query, not the case used in the table definition
Eineki
Thanks, Eineki.
arunas_t
A: 

Are you sure that rows were returned? You can use mysql_num_rows($result) to get the count. The only thing I can think of looking at your code is that you're passing in the password in plain text and the version in the DB is MD5 or something.

Greg
This is where I would start if you aren't getting any errors and it's simply not returning any values (or something like "Resource #100"). The $result variable will be defined even if a row isn't replaced.Try inserting echo(mysql_num_rows($result)); after $result=mysql_query($query);. If it prints "0", something's wrong with your query. If it returns "1", echo a dummy sting inside your while control to see if it prints anything.
Marc Ripley