简体   繁体   中英

Getting error messages from Zend_Db_Adapter::exec()

I'm working on a script to make versioning our database easier. In order to do this we have a number of files (each contains at least one statement and a couple have over a hundred) containing the SQL queries that we need to update the schema that are delimited using semicolons. It works nicely 90% of the time except occasionally one of the statements will fail and we're having problems getting these fixed. When this happens we have to delete the database and manually copy and paste each SQL statement in the failing file to test it. Some queries cause an exception but for some queries the function just returns 1.

I'm using the following code but I can't figure out how to find the statement that's having the problem:

$db = Zend_Db_Table::getDefaultAdapter();

try{
    // loop through all the files
    foreach($files as $file){
        echo 'Running ', $file, PHP_EOL;

        // use the connection directly to load sql in batches
        if($db->exec(file_get_contents($dir . $file)) == 1){
            // *how do I get the problem line here?*
            return false;
        }

        $db->exec('INSERT INTO database_history SET file = "' . $file .'";');
    }
} catch (Exception $e) {
    echo 'AN ERROR HAS OCCURED:' . PHP_EOL;
    echo $e->getMessage() . PHP_EOL;
    return false;
}

Initialise a counter var like in the following example. Increment it each iteration by one. Output the counter var, when there's an error. I've called the var $line in this example. The exit terminates the script, after the var has been output. If you don't want that, you can just leave it out.

$line = 0;
foreach($files AS $file){
    $line++;
    // ...
     if($db->exec(file_get_contents($dir . $file)) == 1){
        echo '<pre>'; print_r($line); echo '</pre>'; exit;
        return false;
    }
    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM