Search code examples
phpsqloracle-databasezend-framework

PHP Zend Executing a raw SQL statement from controller action


I'm looking to see if it's possible to execute a raw SQL statement from a PHP controller action. Currently, I'm trying to execute a block of code that I build within the controller action to insert values into a table within my database...

$sql = "BEGIN INSERT INTO HOSTS VALUES('1', 'hostname1'); INSERT INTO HOSTS VALUES('2', 'hostname2'); END;";
$db->exec($sql);

Is something similar to the above code possible to be handled within a controller/action, or do I need to pursue a different route? Thank you in advance.


Solution

  • I figured out you can reference your DB credentials within the application.ini file and execute the SQL statement using said credentials. This can be done within the controller page of your code.

    $sql = "BEGIN INSERT INTO HOSTS VALUES('1', 'hostname1'); INSERT INTO HOSTS VALUES('2', 'hostname2'); END;";
    $stmt = Zend_Registry::get('DBNAME')->prepare($sql);
    $stmt->execute();