如何使用PHP变量为MYSQL准备查询字符串?

时间:2015-11-09 08:34:05

标签: php sql connect

我有这个查询在phpmyadmin中工作,但在php中不起作用。除此之外还有其他选择吗?

$servername = "localhost";
$username = "user";
$password = "password";
$dbname = "dbname";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {die("Connection failed: " . $conn->connect_error);}

$champname = 'name';

$sql = "SELECT column1, column2, column3, column4, column5
FROM champions 
WHERE champion = '$champname' ORDER BY ID DESC LIMIT 1";

$result = $conn->query($sql);
$row = $result->fetch_assoc();

任何帮助都将受到高度赞赏

我想'$champname'是问题所在。

2 个答案:

答案 0 :(得分:1)

试试这个

$sql = "SELECT column1, column2, column3, column4, column5
FROM champions 
WHERE champion = $champname ORDER BY ID DESC LIMIT 1";

$sql = "SELECT column1, column2, column3, column4, column5
FROM champions 
WHERE champion = `$champname` ORDER BY ID DESC LIMIT 1";

答案 1 :(得分:0)

$ sql =“SELECT column1,column2,column3,column4,column5 来自冠军 WHERE冠军='“。$ champname。”'ORDER BY ID DESC LIMIT 1“;