简体   繁体   English

PHP和mysql查询带有动态变量

[英]PHP and mysql query with a dynamic variable

I am trying to get a value from database using the code below. 我正在尝试使用下面的代码从数据库中获取值。 I want to save the value in the variable category so I can give this as parameter to a function. 我想将值保存在变量类别中,以便可以将此作为参数提供给函数。 The id is dynamically given. id是动态给出的。 Is the code below correct? 下面的代码正确吗? because when trying this nothing works... 因为尝试此操作无济于事...

$thecategory = mysql_query("SELECT TYPE FROM lists WHERE id =" . this.id);

The use of '$id' provides a little security against sql injection 使用'$ id'可以防止SQL注入

$thecategory = mysql_query("SELECT `TYPE` FROM `lists` WHERE `id` ='$id'");
$associate = mysql_fetch_assoc($thecategory);
$TYPE = $associate['TYPE'];

Use mysql_result : 使用mysql_result

$thecategory = mysql_query("SELECT TYPE FROM lists WHERE id =" . $id); // changed $id
$type = mysql_result($thecategory, 0);

I suggest that you use PDO instead of mysql_* functions, they are deprecated. 我建议您使用PDO代替mysql_*函数,因为它们已被弃用。

Try: 尝试:

$thecategory = mysql_query("SELECT TYPE FROM lists WHERE id =" . this->id); $ thecategory = mysql_query(“从列表中选择类型,其中id = =” .this-> id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM