-1

I'm self taught in PHP, but i'm afraid my syntax is off for the following MySQL query.

$getPassword="SELECT password FROM $tbl_name WHERE username=$myusername";
$password=mysql_query($getPassword);

Is the above registering $myusername as a string or as a variable? I'm using PHP 5.4. Thanks in advance for any help!

BlitZ
  • 12,038
  • 3
  • 49
  • 68

5 Answers5

5

Add single quotes to $myusername so that it will be read in the query.

$getPassword="SELECT `password` FROM `$tbl_name` WHERE `username`='$myusername'";

Instead use mysqli_* functions or PDO because mysql_* functions are depracated and will no longer be used in the future.

$password=mysqli_query($your_connection, $getPassword);
John Robertson
  • 1,486
  • 13
  • 16
  • 2
    Well, if you're going to go down this road (and you should), then it's probably wise to look at prepared statements. – Strawberry Aug 26 '14 at 08:09
1

You must put prime symbols around the $myusername variable:

$getPassword="SELECT password FROM $tbl_name WHERE username='$myusername'";
$password=mysql_query($getPassword);
jondinham
  • 8,271
  • 17
  • 80
  • 137
1

Try this

 $getPassword="SELECT password FROM ".$tbl_name." WHERE username='".$myusername."'";
Eduard Luca
  • 6,514
  • 16
  • 85
  • 137
Kack
  • 86
  • 2
  • single codes are required for the values in mysql queries , if you want to concat variables with a string. Please look at the below code $getPassword="SELECT password FROM ".$tbl_name." WHERE username='".$myusername."'"; – rahul Aug 26 '14 at 09:29
  • i have updated my answer please see and if wrong then send me error – Kack Aug 27 '14 at 08:43
0

If you are using php variables which contains strings, you have to enclose it in quotes like,

$getPassword="SELECT password FROM $tbl_name WHERE username='$myusername'";
                                                            ^           ^

Otherwise, your query will fail to execute.

Jenz
  • 8,280
  • 7
  • 44
  • 77
0

For the perfect syntax that can make your code much faster...

$getPassword="SELECT `password` FROM `$tbl_name` WHERE `username`='$myusername'";
rahul
  • 776
  • 5
  • 30