从SQL查询的变量读取字符串

SQL newb here...

$db_result = mysql_query("SELECT first_name FROM gamers WHERE comp_id = 'myid'"); works the way I want.

$compid1 = 'myid';
$db_result = mysql_query("SELECT first_name FROM gamers WHERE comp_id = @compid1");

does not yield the same results.

I have also tried $compid1 and various other things, but without success.

Sorry for the simple question, but the answer is still eluding me. Thanks!

UPDATE: Oh yea...the question. How can I use a prestored variable for my WHERE check?

You need to use $ before a variable, not @. And you need to put quotes around it since it's a string:

$db_result = mysql_query("SELECT first_name FROM gamers WHERE comp_id = '$compid1'");

However, it would be best if you stopped using the mysql extension. Use PDO or mysqli, and use prepared statements with parameters. E.g. in PDO it would be:

$stmt = $conn->prepare("SELECT first_name FROM gamers WHERE comp_id = :compid");
$stmt->bindParam(':compid', $compid1);
$stmt->execute();

Enclose the string variable inside a pair of quotes.

$compid1 = 'myid';
$db_result = mysql_query("SELECT first_name FROM gamers WHERE comp_id = '$compid1'");