Jump to content



Photo

Database Restore Using Pdo

pdo insert

  • Please log in to reply
3 replies to this topic

#1 Necuima

Necuima

    Advanced Member

  • Members
  • PipPipPip
  • 254 posts
  • LocationSunshine Coast, Queensland, Australia

Posted 18 June 2017 - 7:00 AM

Hi Larry,

As mentioned elsewhere, I am upgrading all my database actions to PDO.

I use the PHP scripts for database backup and restore which you have provided - they are very useful and have served me well.

I have upgraded the database backup to PDO no worries but now I'm coming to the restore.

The PDO "inserts" will need to handle a variable number of fields of varying/unknown data types. Are there any 'best practices' for this type of PDO INSERT / field binding?

Looking forward to your advice and thank you in anticipation,

Cheers
  • 0

#2 Necuima

Necuima

    Advanced Member

  • Members
  • PipPipPip
  • 254 posts
  • LocationSunshine Coast, Queensland, Australia

Posted 20 June 2017 - 2:11 AM

Hi Larry,

 

I experimented a bit and the following process worked for me:

 

Loop through the row returned from the .doc file for the table in question and create the query inserting an un-named placeholder (?) for every data item - thus it will create an un-named placeholder for each data item - you don't need to know how many up front.

Then loop through the row again binding each data item to a numerical count of the placeholder - I found that all the data items are strings so I used the PDO::PARAM_STR constant in every bind.

The second foreach loop needs to declare the data_item with a preceding & pass by reference otherwise the binds don't work properly.

 

There may be better ways but this worked for me (after numerous trials and errors)!

 

Cheers 


  • 0

#3 Necuima

Necuima

    Advanced Member

  • Members
  • PipPipPip
  • 254 posts
  • LocationSunshine Coast, Queensland, Australia

Posted 22 June 2017 - 3:07 AM

After further experimenting, the bind can be with bindValue in which case you don't need the pass by reference.

 

Cheers


  • 0

#4 Larry

Larry

    Administrator/Writer

  • Administrators
  • 4758 posts
  • LocationState College, PA (USA)

Posted 22 June 2017 - 9:15 PM

Kudos for figuring it out and thanks for sharing your solution. Let us know if you have more questions!


  • 0