在mysql查询中添加变量

时间:2014-05-17 08:40:12

标签: php

我有一个变量$text = "sometexthere"

我想在以下mysql查询中添加此变量$text而不是sometexthere

$query = "SELECT sum(nbr) AS sum FROM table2 WHERE name LIKE '%sometexthere%'";

我尝试过很多方式,比如

$query = "SELECT sum(stats) AS sum FROM download WHERE filename LIKE '%' . $text . '%'";

$name = "'%' . $text . '%'";
$query = 'SELECT sum(stats) AS sum FROM download WHERE filename LIKE '.$name.'';

但不工作:/

2 个答案:

答案 0 :(得分:1)

尝试

$query = "SELECT sum(stats) AS sum FROM download WHERE filename LIKE '%$text%'";

并避免这一行

$name = "'%' . $text . '%'";

或尝试这样,

$name = "%".$text."%";
$query = "SELECT sum(stats) AS sum FROM download WHERE filename LIKE '$name'";

答案 1 :(得分:0)

尝试

$query = "SELECT sum(stats) AS sum FROM download WHERE filename LIKE '%" . $text . "%'";