. * * Execute sql, one statement or more per line. * * Karl O. Pinc * * Remarks: * SQL statements must appear on a single line. * Upon error, remining statements on the line are ignored and * subsqeuent lines are executed. * * Shows errors, but no results. */ // Includes require('constants.php.inc'); require('lib.php.inc'); require('lib_upload.php.inc'); // // Constants // // Delimiter separating sql statements. define('DELIM', "\r\n"); // // Functions. // function execute_sql(&$upload_form, &$dbconn, $form_sql) { /* * Execute the sql and show the result. * * Input: * $upload_form An instance of the Upload_form class. * $dbconn An instance of the Upload_connection class. * $form_sql Sql code to execute if no file content is uploaded. * * Returns: n/a * * Side Effects: * Outputs html. * * Remarks: * One of the main pages of the program. */ // Get the uploaded data. $upload_object = new Uploaded_data($upload_form); if ($upload_object->filename == '') { $sql = explode(DELIM, $form_sql); } else { if (!$upload_object->upload()) { return; } $sql = $upload_object->data; } // // Connect to the db. // if (!$dbconn->connect()) { return; } // // Go through all the data and load it. // $faulting = FALSE; $lines = count($sql); for ($line = 1; $line <= $lines; $line++) { $this_sql = $sql[$line - 1]; if ($this_sql !== '' && !$dbconn->sql($this_sql)) { // Record the error. // Let the user try again. $upload_form->error($line , 'PostgreSQL will not execute your statement, it reports the error: ' . htmlentities($dbconn->last_error()) , $faulting , 'The sql statement is: ' . htmlentities($sql[$line - 1])); $faulting = TRUE; } } $upload_object->success(); // Remember uploaded file. if ($faulting) { $upload_form->show('

' . 'Errors occurred during the execution of ' . $lines . ' lines of sql.

'); } else { // // Everything seems to have worked. // Allow the user to do it again. // $upload_form->show('

' . 'Successfully executed ' . $lines . ' lines of sql!

'); } return; } // // Body of code // maintain_login_session(); show_html_start('sqlbyline -- Execute multiple lines of sql'); ?>

Execute multiple lines of SQL

extra_input = array('4' => array('label' => '' , 'data' => '')); // // Process input values // // // Dispatch on action // switch ($action) { case ACTION_NONE: $upload_form->show(); break; case ACTION_UPLOAD: // Make a database connection. $dbconn =& new Upload_connection($db , $user , $password , $upload_form); // Do the deed. execute_sql($upload_form, $dbconn, $sql); break; } // Finish the stock page. show_html_finish(); return; // End of program.