Unexpected T_ELSE in small PHP function
I have an unexpected T_ELSE on the last else in this function.
function QueryPeople($stringQuery, $table, $max, $cmd) {
$con = mysqli_connect("localhost","user","password", "host");
if ($cmd == "Option1") {
$SearchSQL = "SELECT signature, firstname, birthdate FROM $table WHERE lower(signature) LIKE ?" . $max;
if ($fetchData = $con->prepare($SearchSQL)) {
$fetchData->bind_param("s", "%".$stringQuery."%");
$fetchData->execute();
$fetchData->bind_result($signature, $firstname, $birthdate);
$rows = array();
}
} else if ($cmd == "Option2") {
$searchSQL = "SELECT signature, firstname, birthdate FROM $table WHERE birthdate = ?" . $max;
if ($fetchData = $con->prepare($searchSQL)) {
$fetchData->bind_param(":birthdate", $stringQuery);
$fetchData->execute();
$fetchData->bind_result($signature, $firstname, $birthdate);
$rows = array();
}
}
while ($fetchData->fetch()) {
$row = array(
'signature' => $signature,
'firstname' => $firstname,
'birthdate' => $birthdate,
);
$rows[] = $row;
}
return $rows;
} else { // <-- This else doesn't have an if
print_r($con->error); // <-- This else doesn't have an if
} // <-- This else doesn't have an if
}
I seriously cannot understand why this is happening. Both the if blocks should be self contained,开发者_开发技巧 and both are closed, and then it should go to the while, and only the if if something looks fishy?
There's an extra bracket somewhere... If you indent the code you'll see that you have not closed every section properly...
You'd need to add an if($con)
before the first if
:
function QueryPeople($stringQuery, $table, $max, $cmd) {
$con = mysqli_connect("localhost","user","password", "host");
if($con){
if ($cmd == "Option1") {
$SearchSQL = "SELECT signature, firstname, birthdate FROM $table WHERE lower(signature) LIKE ?" . $max;
if ($fetchData = $con->prepare($SearchSQL)) {
$fetchData->bind_param("s", "%".$stringQuery."%");
$fetchData->execute();
$fetchData->bind_result($signature, $firstname, $birthdate);
$rows = array();
}
} else if ($cmd == "Option2") {
$searchSQL = "SELECT signature, firstname, birthdate FROM $table WHERE birthdate = ?" . $max;
if ($fetchData = $con->prepare($searchSQL)) {
$fetchData->bind_param(":birthdate", $stringQuery);
$fetchData->execute();
$fetchData->bind_result($signature, $firstname, $birthdate);
$rows = array();
}
}
while ($fetchData->fetch()) {
$row = array(
'signature' => $signature,
'firstname' => $firstname,
'birthdate' => $birthdate,
);
$rows[] = $row;
}
return $rows;
} else {
print_r($con->error);
}
}
Anyway, I don't think $con->error will show anything... you'll need mysql_error for that.
Your code is wrong. Organise your code tidily. You missed an if statement.
<?php
function QueryPeople($stringQuery, $table, $max, $cmd) {
//
if ($cmd == "Option1") {
//
if ($fetchData = $con->prepare($SearchSQL)) {
/**/
}
} else if ($cmd == "Option2") {
//
if ($fetchData = $con->prepare($searchSQL)) {
/**/
}
}
while ($fetchData->fetch()) {
/**/
}
return $rows;
} else { //<- WHAT else?
print_r($con->error);
}
}
You have got double of } .. see below
$rows = array();
}
} else if ($cmd == "Option2")
change it to
$rows = array();
} else if ($cmd == "Option2")
UPDATE ..
oops mistaken
actually you forgot to add
if($con) {
Your last else is outside the function braces like:
function funcName() {
// function body...
} else {
// this is where your else is
}
You should use an IDE that highlights source, put the cursor on each brace in turn and you will see the matching brace.
精彩评论