-2

I am supposed to design an exam server system and I keep getting errors while I am trying to prepare the statement using mysqli's prepare function. I am using php to connect to sql represented by the $conn variable(it does connect to the DB), and I have tried the same exact statements in mysql workbench and it works fine. Here is the code I have written:

$stmtTempTable = $conn->prepare("CREATE TEMPORARY TABLE front_action_temp
                        SELECT * FROM front_action
                        WHERE time_offset > 0");
$stmt = $conn->prepare("SELECT event_name, status, day_of_week, week_of_year, event_year, start_time, time(start_time + 
                        time_offset) as end_time, machine_group
                        FROM front_weekly
                        LEFT JOIN front_event ON front_weekly.event_id=front_event.event_id
                        LEFT JOIN front_daily ON front_event.event_id=front_daily.event_id
                        LEFT JOIN front_group ON front_daily.group_id=front_group.group_id
                        LEFT JOIN front_action_temp ON front_action_temp.event_id=front_event.event_id
                        WHERE day_of_week=? and week_of_year=? and event_year=?");

if ($stmt === false){
    die('Unable to execute');
} else {
    $stmt->bind_param('sss', $dayOfWeek, $weekNumber, $year);
    $stmt->execute();
}

I keep getting false for $stmt. Any ideas why?

Kai
  • 1
  • 1
  • 1
  • `$stmtTempTable` never gets executed. – ADyson Sep 23 '20 at 14:51
  • 1
    You need to stop manually checking for errors. Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) and [Should I manually check for errors when calling “mysqli_stmt_prepare”?](https://stackoverflow.com/q/62216426/1839439) – Dharman Sep 23 '20 at 14:54
  • `$stmtTempTable` does not need to be prepared, you aren't passing any placeholders/values. – GrumpyCrouton Sep 23 '20 at 15:36

1 Answers1

1

You are only prepare()ing the CREATE TEMPORARY TABLE... but not execute()ing it. The second statement fails because the table does not exist.

Vesa Karjalainen
  • 1,087
  • 8
  • 15