Jump to content

Importing a database via PHP


jj20051

Recommended Posts

I'm attempting to import a mysql database backup via php unfortunately I keep getting this error:

 

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 'SOURCE file.sql' at line 1

 

Code:

mysql_query("SOURCE file.sql") or die (mysql_error());

 

I've researched this online and this seems to be the way everyone suggests to use, unfortunately it isn't working. If it helps here is the first 4 lines of file.sql as that may be the problem for all I know:

 

-- phpMyAdmin SQL Dump
-- version 3.2.4
-- http://www.phpmyadmin.net
--

Link to comment
https://forums.phpfreaks.com/topic/213268-importing-a-database-via-php/
Share on other sites

Hey,

 

I checked the file but it haven't any fault in file.

 

I think you should change your logic to import by source command.

 

$con = mysql_connect('localhost','user','pass');
$db = mysql_select_db('dbname',$con);

$val = file_get_contents('file.txt');
$query = explode('-- --------------------------------------------------------',$val);
foreach( $query as $k=>$v)
{
    mysql_query($v);
}

 

This worked. let me know still you are facing problems.

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.