0

I'm trying to get information from mysql database using GET id. I use the following code to check what the id is.

            $qry    = "SELECT name,country FROM databasetable WHERE uid=$id";

I get error which means that it couldn't find any entry with the specified uid. If I change the uid to only to numbers, then it works can look it up. Example: ?id=1000000000 works fine, ?id=1kKV0LEfMi . Can't be found Please help me

andrew
  • 31
  • 1
  • 9
  • It may not help answer your question, but you should stop using `mysql_*` functions. They're being deprecated. Instead use [PDO](http://php.net/manual/en/book.pdo.php) (supported as of PHP 5.1) or [mysqli](http://php.net/manual/en/book.mysqli.php) (supported as of PHP 4.1). If you're not sure which one to use, [read this article](http://net.tutsplus.com/tutorials/php/pdo-vs-mysqli-which-should-you-use/). – Matt Aug 20 '12 at 18:16
  • What *exactly* is the query you send to the database? – Matt Aug 20 '12 at 18:17
  • Also, `htmlspecialchars` doesn't stop SQL Injections. You have to use `mysql_real_esacape_string`, though it's outdated (!) as Matt said. – ComFreek Aug 20 '12 at 18:19

3 Answers3

1

you need to add quotes around the $id to allow strings in the query

$qry= "SELECT name,country FROM databasetable WHERE uid='$id'";

btw. why are you doing $id = htmlspecialchars($_GET['id']); ?

this should rather be $id = mysql_escape_string($_GET['id']);!

Andreas Linden
  • 12,489
  • 7
  • 51
  • 67
0
 '$id'  <-- first put the variable in single quotes. good practice. 

Secondly, if you're not using special chars take that out because that could be your problem. Try stripping html special chars and try again

Matt
  • 6,993
  • 4
  • 29
  • 50
Dnaso
  • 1,335
  • 4
  • 22
  • 48
0

You need to change your query to have single quotes around the uid value like this

$qry = "SELECT name,country FROM databasetable WHERE uid='$id'";

That being said you also really need to look into using the newer MySQL connection support (not the mysql_* functions) and you need to learn how to escape your data so you protect against SQL injection.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103