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

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.