Having trouble getting my PHP - MySQL SELECT FROM working with a variable?

  • Thread starter Thread starter Rockstar
  • Start date Start date
R

Rockstar

Guest
I'm first setting the variable (in practice):
$str='josh19';

I'm then trying to select data from the database, and this works fine:

$query="SELECT `lastname`, `firstname` FROM `customers` WHERE `username`='josh19'";

Works great. But of course I need to use a variable instead of writing the name directly in there. So,

$query="SELECT `lastname`, `firstname` FROM `customers` WHERE `username`='$str'";

That doesn't work. I've tried searching on several sites and it seems a lot of people have the same problem, but their fix never seems to work, no matter how I type the quotes. I've tried concatenating with a period and double quotes, I've tried adding slashes... nothing works.

WHAT DO I DO????

MySQL 4.1, PHP 5, Linux (shared hosting)
When I say it doesn't work, it has nothing to do with the MySQL. When I tried echoing $query, it returns as if the variable was null:

The result of echo $query; looks like this:
$query="SELECT `lastname`, `firstname` FROM `customers` WHERE `username`=''

So the PHP completely ignores the variable.
 
$query="SELECT `lastname`, `firstname` FROM `customers` WHERE `username`=' ". $str. " ' ";
 
$query="SELECT `lastname`, `firstname` FROM `customers` WHERE `username`='josh19'";

$query="Select 'lastname','firstname' FROM 'costumers' WHERE username='$str'";
 
Back
Top