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
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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.