roopurt18 Posted May 5, 2010 Share Posted May 5, 2010 Does anyone know how run a script.sql via PDO for PostgreSql? A while back, I initially tried this: <?php $sql = file_get_contents( 'script.sql' ); $pdo->query( $sql ); ?> But it failed. I eventually found that this was the way: <?php $sql = file_get_contents( 'script.sql' ); $st = $pdo->prepare( $sql ); $st->execute(); ?> It worked for a long time on Windows. Then when I migrated my code to Linux it stopped working and I had to update my code to do this: <?php $sql = file_get_contents( 'script.sql' ); $st = $pdo->prepare( $sql, array( PDO::ATTR_EMULATE_PREPARES => true ) ); $st->execute(); ?> Then I had a script.sql with regular expressions in queries. The regular expressions had question marks in them. So then PDO's prepare() method thought my query had parameters and wouldn't run them. So I had to change my code to this: <?php $sql = file_get_contents( 'script.sql' ); $sql = str_replace( '?', "\\077", $sql ); $st = $pdo->prepare( $sql, array( PDO::ATTR_EMULATE_PREPARES => true ) ); $st->execute(); ?> Now I've moved my code to a new Linux box that has, what I believe, are the latest versions of libpq, PDO, and pdo_pgsql and it doesn't work. I'm getting errors about bound parameters again, even though I've used str_repalce() to change question marks to their octal equivalent. This leads me to believe that PDO might be translating the octal codes back to question marks. Is there any way to accomplish this task with PDO? Quote Link to comment https://forums.phpfreaks.com/topic/200839-running-a-sql-script-via-pdo/ Share on other sites More sharing options...
roopurt18 Posted May 7, 2010 Author Share Posted May 7, 2010 I've since re-written this code to not use PDO and just use the sequence of: pg_connect(); pg_query( $script ); pg_close(); And it works fine. But I can't seriously be the only programmer on these boards with this issue and PDO? Has nobody else tried this? Quote Link to comment https://forums.phpfreaks.com/topic/200839-running-a-sql-script-via-pdo/#findComment-1054711 Share on other sites More sharing options...
Mchl Posted May 7, 2010 Share Posted May 7, 2010 Not only you're using Postgres, but also PDO. That moves you to category of 'PHP coders that do not exist'. Quote Link to comment https://forums.phpfreaks.com/topic/200839-running-a-sql-script-via-pdo/#findComment-1054814 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.