views:

92

answers:

4

Hello,

I have a very aggrivated unexped 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 wile, and only the if if something loos fishz?

A: 

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) {
Wbdvlpr
I have several other functions, none of which requre if($con) {, so I don't think that is the issue.
Jill Doherty
+1  A: 

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.

Seb
A: 

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);
   }
}
erenon
YOu have tiedied up my code, but the logic seems the same, with the same number of brackets in the same logical sequence.
Jill Doherty
Check the comment. You have an unnecessary else.
erenon
A: 

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.

Jim Ford