-2

Below code IS NOT working

$ID=2;
mysql_connect($localhost,$username,$password);
mysql_select_db(productlist) or die( "Unable to select database");
$query='SELECT * FROM HolisticSerums where ProductID =$ID' ;
$result=mysql_query($query);
$array = mysql_fetch_assoc($result);
echo $array['Size'];
mysql_close();

but when i use 2 instead of $ID it is working

   //$ID=2;
    mysql_connect($localhost,$username,$password);
    mysql_select_db(productlist) or die( "Unable to select database");
    $query='SELECT * FROM HolisticSerums where ProductID =2' ;
    $result=mysql_query($query);
    $array = mysql_fetch_assoc($result);
    echo $array['Size'];
    mysql_close();

first please tell me how to typecast integer of PHP to int of database? can any one help me what is the problem in 1st case.. ?? ProductID column is INT in database

Pupil
  • 23,834
  • 6
  • 44
  • 66
  • Before you do `$query =` what happens when you do `echo $ID`? – Explosion Pills Oct 03 '13 at 17:02
  • You could try `$query="SELECT * FROM HolisticSerums where ProductID ='$ID'";` – Funk Forty Niner Oct 03 '13 at 17:15
  • Note the the `mysql_` lib is outdated and dangerous, use PDO instead. Bobby tables will the grateful. http://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work and see also: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php?rq=1 – Johan Oct 04 '13 at 02:31

3 Answers3

1

Because PHP variables are not interpolated inside single quotes

$query='SELECT * FROM HolisticSerums where ProductID =$ID' ;  // $ID Should be inside double quotes or outside all


$query="SELECT * FROM HolisticSerums where ProductID =".$ID ;
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
1

you have to use SQL query in 2 options

$query="SELECT * FROM HolisticSerums where ProductID =$ID";
       ^                                                 ^

or

$query="SELECT * FROM HolisticSerums where ProductID =". $ID;
qwertmax
  • 3,120
  • 2
  • 29
  • 42
0
$query='SELECT * FROM HolisticSerums where ProductID =$ID' ;

Should be:

$query="SELECT * FROM HolisticSerums where ProductID =$ID" ;

Since the '-string don't parse variables.