Jump to content

Running a SQL script via PDO


roopurt18

Recommended Posts

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?

 

Link to comment
https://forums.phpfreaks.com/topic/200839-running-a-sql-script-via-pdo/
Share on other sites

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.