0

This should be a very basic error, but based on the error-description I can't seem to figure it out. Either I misunderstood some part of the concept or it's just some sign missing.

The problem arises when I try to execute a query.

This is some of the code (I think it should be enough):

//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

When I run this I get an error saying:

Parse error: parse error, expecting `']'' in /Library/WebServer/Documents/resultat.php on line 58

Which points to this line:

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

Thank you in advance for your help.

5
  • 1
    You missing something I guess, read here: php.net/manual/en/pdostatement.execute.php Commented Mar 9, 2017 at 11:43
  • 2
    Your php is not 5.4? Commented Mar 9, 2017 at 11:44
  • 1
    Try with array syntax instead of [] syntax Commented Mar 9, 2017 at 11:47
  • @u_mulder - My PHP-version is "5.5.27". Commented Mar 9, 2017 at 11:50
  • @smarber - If you mean "array($var1,$var2,...,$varN) I just tried it as per the link provided by the first commenter. But it does not seem to make any difference. Commented Mar 9, 2017 at 11:51

1 Answer 1

1

Instead of this code

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

you shuld try this one

$stmt->execute(array(
    $lan, 
    $botyp, 
    $prisMin, 
    $prisMax, 
    $storlekMin, 
    $storlekMax, 
    $rumMin, 
    $rumMax, 
    $avgiftMin, 
    $avgiftMax
)); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a bunch! It seems to work :-)
Happy to help :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.