执行查询时PHP Parse错误

时间:2017-03-09 11:40:30

标签: php php-parse-error

这应该是一个非常基本的错误,但根据错误描述,我似乎无法弄明白。要么我误解了概念的某些部分,要么只是遗漏了一些标志。

当我尝试执行查询时出现问题。

这是一些代码(我认为应该足够了):

//Create database connection to my server
$pdo = new PDO($dsn, $user, $password);


$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

//All single variables
$lan = $_POST["lan"];
$botyp = $_POST["botyp"];

//All variables with min and max value
$pris = $_POST["pris"];
$prisArray = explode(",", $pris); //Splits string "minvalue, maxvalue" by delimiter "," to become array with [minvalue, maxvalue]
$prisMin = $prisArray[0];
$prisMax = $prisArray[1];

$storlek = $_POST["storlek"];
$storlekArray = explode(",", $storlek);
$storlekMin = $storlekArray[0];
$storlekMax = $storlekArray[1];

$rum = $_POST["rum"];
$rumArray = explode(",", $rum);
$rumMin = $rumArray[0];
$rumMax = $rumArray[1];

$avgift = $_POST["avgift"];
$avgiftArray = explode(",", $avgift);
$avgiftMin = $avgiftArray[0];
$avgiftMax = $avgiftArray[1];

$query = "SELECT * FROM bostader 
WHERE lan = ? AND
objekttyp = ? AND
(pris >= ? AND pris <= ?) AND
(area >= ? AND area <= ?) AND
(rum >= ? AND rum <= ?) AND
(avgift >= ? AND avgift <= ?)";

$stmt = $pdo->prepare($query);
$stmt->execute([$lan, $botyp, $prisMin, $prisMax, $storlekMin, $storlekMax, $rumMin, $rumMax, $avgiftMin, $avgiftMax]); //Execute query using relevant variables

当我运行此操作时,我收到错误消息:

  

解析错误:解析错误,期待'&#39;]&#39;&#39;在/Library/WebServer/Documents/resultat.php第58行

指向这一行:

$stmt->execute([$lan, $botyp, $prisMin, $prisMax, $storlekMin, $storlekMax, $rumMin, $rumMax, $avgiftMin, $avgiftMax]);

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

而不是这段代码

$stmt->execute([$lan, $botyp, $prisMin, $prisMax, $storlekMin, $storlekMax, $rumMin, $rumMax, $avgiftMin, $avgiftMax]); 

你请试试这个

$stmt->execute(array(
    $lan, 
    $botyp, 
    $prisMin, 
    $prisMax, 
    $storlekMin, 
    $storlekMax, 
    $rumMin, 
    $rumMax, 
    $avgiftMin, 
    $avgiftMax
));