Title: Reading SQL script and executing it
Last modified: January 10, 2018

---

# Reading SQL script and executing it

 *  Resolved [jeffreylebowski](https://wordpress.org/support/users/jeffreylebowski/)
 * (@jeffreylebowski)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/reading-sql-script-and-executing-it/)
 * Is it possible to do the following:
 *  $sql_script = file_get_contents(‘path to uploads/script.sql’);
    $wpdb->query(
   $wpdb->prepare($sql_script));

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [RossMitchell](https://wordpress.org/support/users/rossmitchell/)
 * (@rossmitchell)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/reading-sql-script-and-executing-it/#post-9853429)
 * Yes.
    Questions remain as to why. Also how safe is it ?
 *  [Dion](https://wordpress.org/support/users/diondesigns/)
 * (@diondesigns)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/reading-sql-script-and-executing-it/#post-9856529)
 * You should never use `$wpdb->prepare` with data from a .sql file that was created
   by an external script (mysqldump, phpMyAdmin, etc). The queries in those files
   are already properly escaped.
 * The WordPress DBAL does not support the execution of multiple queries from a 
   single function call. That will require custom code that grabs the DB handle 
   and uses `mysqli_multi_query()` to execute all the queries at once. Before you
   use this function, hwoever, please make sure to read the following:
 * [https://secure.php.net/manual/en/mysqli.multi-query.php](https://secure.php.net/manual/en/mysqli.multi-query.php)
 *  Thread Starter [jeffreylebowski](https://wordpress.org/support/users/jeffreylebowski/)
 * (@jeffreylebowski)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/reading-sql-script-and-executing-it/#post-9856644)
 * I did figure out that prepare wasn’t necessary for this, but thanks for reinforcing
   that.
 * I have reworked everything to use mysqli_multi_query() and I’m getting a syntax
   error:
 * First query failed…You have an error in your SQL syntax; check the manual that
   corresponds to your MySQL server version for the right syntax to use near ‘SET
   [@newid](https://wordpress.org/support/users/newid/) = last_insert_id()
 * SET [@symbol](https://wordpress.org/support/users/symbol/) = ‘COST’
 * SET @tagID = (SELECT tagID ‘ at line 4
 *  [Dion](https://wordpress.org/support/users/diondesigns/)
 * (@diondesigns)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/reading-sql-script-and-executing-it/#post-9857239)
 * Each query must be terminated with a semicolon.
 *  Thread Starter [jeffreylebowski](https://wordpress.org/support/users/jeffreylebowski/)
 * (@jeffreylebowski)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/reading-sql-script-and-executing-it/#post-9858969)
 * Yeah they all had semicolons already. Missed the single quotes around the table
   name on the previous line. That was the issue.
 * thanks again!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Reading SQL script and executing it’ is closed to new replies.

## Tags

 * [MySQL](https://wordpress.org/support/topic-tag/mysql/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 5 replies
 * 3 participants
 * Last reply from: [jeffreylebowski](https://wordpress.org/support/users/jeffreylebowski/)
 * Last activity: [8 years, 4 months ago](https://wordpress.org/support/topic/reading-sql-script-and-executing-it/#post-9858969)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
