如何在MySQL中包含PHP数组变量

时间:2016-06-08 15:24:41

标签: php mysql arrays variables

我正在尝试以下方法:

mysql_query("SELECT `lastUpdate` FROM `summoner` WHERE `name` = '$summoner['name']';");

但它不起作用。我认为这是因为'中的$summoner['name']

没有以下任何解决方法:

$summonerName = $summoner['name'];

3 个答案:

答案 0 :(得分:3)

您可以使用(出现时没有单引号)

"SELECT `lastUpdate` FROM `summoner` WHERE `name` = '$summoner[name]'";

或者用大括号中的引号包装整数数组

"SELECT `lastUpdate` FROM `summoner` WHERE `name` = '{$summoner['name']}'";

答案 1 :(得分:2)

你真的想用预备语句和mysqi_ * PHP Manual

//assume you have $conn made and working
if ($stmt = mysqli_prepare($conn, "SELECT `lastUpdate` FROM `summoner` WHERE `name`=?"){
    mysqli_stmt_bind_param($stmt, "s", $summoner['name']);
    $stmt->execute();
    $result = $stmt->get_result();
}

//use $result to loop or whatever...

答案 2 :(得分:0)

试试这个

"SELECT `lastUpdate` FROM `summoner` WHERE `name` = '$summoner[name]'";