Petr
Petr

Reputation: 7957

PHP mysql_fetch does not return values

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'];
}
}

}

Upvotes: 2

Views: 2460

Answers (3)

Eineki
Eineki

Reputation: 14959

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.

Upvotes: 2

Greg
Greg

Reputation: 2563

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.

Upvotes: 0

arunas_t
arunas_t

Reputation: 1586

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.

Upvotes: 0

Related Questions