0

I want to import my local database dump to server phpmyadmin.For that first i export whole database dump as db-backup-2016-09-14 14.25.11.sql file.after tht i tried to import this sql file to my server phpmyadmin.Below is my code

<?php
date_default_timezone_set('Asia/Kolkata');
$date = date("Y-m-d");
$time = date("Y-m-d H:i:s");

import_tables();

function import_tables()
{
$filename = 'db-backup-2016-09-14 14.25.11.sql';
$mysql_host = '192.168.1.1';
$mysql_username = 'databaseuser';
$mysql_password = 'databasepassword';
$mysql_database = 'databasename';
$conn=mysqli_connect($mysql_host, $mysql_username, $mysql_password,$mysql_database) or die('Error connecting to MySQL server: ' . mysqli_error($conn));
mysqli_select_db($conn,$mysql_database) or die('Error selecting MySQL database: ' . mysqli_error($conn));
$templine = '';
$lines = file($filename);
foreach ($lines as $line)
{
if (substr($line, 0, 2) == '--' || $line == '')
    continue;
$templine .= $line;
if (substr(trim($line), -1, 1) == ';')
{
    mysqli_query($conn,$templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysqli_error($conn) . '<br /><br />');
    $templine = '';
}
}
 echo "Tables imported successfully";
}
?>

Here the hostname declared is my server IP address and username password which i given in mysql database username password created in server.For this im getting error like

enter image description here

Problem with the connection to server.If anybody knows solution please help me to get out of this problem.Thanks in advance

Shiv Singh
  • 6,939
  • 3
  • 40
  • 50
Kavya Shree
  • 1,014
  • 2
  • 17
  • 52
  • Those aren't your real connection details, right? – Jonnix Sep 15 '16 at 08:16
  • 1
    I'm not even sure why you're doing this if you want to use phpmyadmin. Just import the file that you apparently already have. Or use the mysql CLI tool to import the file. There is no point in having this PHP code afaics. – Jonnix Sep 15 '16 at 08:17
  • In my application if the user working in local server after inserted all details simply on click of backup it should export from local server data and imported in server database for tht im using PHP – Kavya Shree Sep 15 '16 at 08:23
  • Look at the demo provided here http://stackoverflow.com/questions/19751354/how-to-import-sql-file-in-mysql-database-using-php – Sasikumar Sep 15 '16 at 08:25
  • Problem is I can import in local..I cant import to server PHPmyadmin. In above link also refered there thy tried to import in local only – Kavya Shree Sep 15 '16 at 08:29
  • This is not a problem with your code. Something is preventing you from connecting to your MySQL server. Contact your web host for details. –  Sep 17 '16 at 05:00
  • Image shows that you are running script on your local network (Windows). So, i think, that your local IP it's not in firewall or not authorized on cpanel's mysql server. https://dev.mysql.com/doc/refman/5.5/en/error-messages-client.html#error_cr_connection_error – abkrim Sep 17 '16 at 05:11
  • first of all never use real details on any public place like your question – Shiv Singh Sep 20 '16 at 05:03

1 Answers1

0

first of all you should enable remote connection on Mysql database and its look like you are using cPanel than you should do it by cPanel => Remote MySQL and set % for all otherwise put accessing server IP

Once Remote access has been enabled than you can access,

Note: Some time it could be not work due to firewall port block so contact hosting provider to allow on firewall

Shiv Singh
  • 6,939
  • 3
  • 40
  • 50