1

Here I is what I have so far:

And I am NOT SURE WHY Hakre closed this because it has to do with dropping MySQL events from the MySQL Event Scheduler NOT with using table names with PDO.

My code is as follows:

public function dMySQLEvent($my_env, $my_event_name) {

        $my_success = 0;
        $my_message = '';

        try {

            # declare & set variables using $my_env
            list ($db_dbdriver, $db_hostname, $db_database, $db_username, $db_password) = Connection::dbConnect($my_env);

            # set default timezone

            # create & set connection

            # WORKS
                # $conn->exec("DROP EVENT IF EXISTS " . $my_event_name . ";");

            # DOES NOT WORK
                # Prepare an SQL statement
                $my_sql = "DROP EVENT IF EXISTS :My_Event_Name";
                $stmt = $conn->prepare($my_sql);

            # bind parameters to prevent SQL Injection
            $stmt->bindValue(':My_Event_Name', $my_event_name);

            # execute the SQL statement
            $stmt->execute();

            # closes the cursor, enabling the statement to be executed again
            $stmt->closeCursor();

            # close & unset the connection
            $stmt = null;
            unset($stmt);
            $conn = null;
            unset($conn);

            # Set success value to TRUE
            $my_success = 1;
            $my_message = 'success';

        } catch(PDOException $e) {

            # Append error message to error log

        }

        return array($my_success, $my_message);

    }
John Edward Law
  • 121
  • 1
  • 9
  • 3
    You can't use a placeholder for the event name `:My_Event_Name`. PDO can only bind scalar values with placeholders. Does your event name come from user input? – Michael Berkowski Dec 23 '14 at 21:12
  • @Michael: No I am wanting to set that value dynamically. Hakre: HOW is this a duplicate of the other post you referenced? – John Edward Law Dec 23 '14 at 22:22
  • If you are in control of the value at all times and it doesn't come from user input as a string, you can safely just use a variable. If it has to be chosen from a list, check the input value against an Array of acceptable values – Michael Berkowski Dec 23 '14 at 22:36
  • Thank you Michael! That is the direction I am going – John Edward Law Dec 23 '14 at 22:46

0 Answers0