This question might seem a duplicate, However i tried all the examples to restore my database but none seems to work for me.
I am trying to run a script that will restore my backup sql file to new database. I tried these lines to restore my database, but none seem to work.
$mysql_host = 'localhost';
$mysql_username = 'my_username';
$mysql_password = 'somepassword';
$db_name = 'test_db';
$source = 'C:/wamp/www/my_folder/test_db.sql';
$conn = mysql_connect( $mysql_host, $mysql_username, $mysql_password ) or die('Error connecting to MySQL server: ' . mysql_error());
mysql_query("CREATE DATABASE $db_name", $conn ) or die('Error connecting to MySQL server: ' . mysql_error());
restore_my_database( $mysql_host, $mysql_username, $mysql_password, $db_name, $source );
function restore_my_database( $mysql_host, $mysql_username, $mysql_password, $db_name, $source ) {
exec("mysql --opt -h $mysql_host -u $mysql_username -p $mysql_password $db_name < $source");
}
I also tried in function restore_my_database following lines
$command = "mysqldump --opt -h $mysql_host -u $mysql_username -p $mysql_password $db_name > $source";
system($command);
Upto database create, the code is working fine, but restore is not working.
Can any one help me with the php restore code to restore my database. Thanks in advance
you can achieve it by below code, first get content from .sql file and then execute the query
$source = 'C:/wamp/www/my_folder/test_db.sql';
$conn = mysql_connect( $mysql_host, $mysql_username, $mysql_password ) or die('Error connecting to MySQL server: ' . mysql_error());
mysql_query("CREATE DATABASE $db_name", $conn ) or die('Error connecting to MySQL server: ' . mysql_error());
mysql_select_db($db_name);
$qry = file_get_contents($source);
mysql_query($qry, $conn);
and also increase max execution time, memory limit in php.ini file
-- you have to create a schema:
mysql
mysql> create database [your schema name];
mysql> grant all privileges on [your schema name].* to [your username]#localhost identified by '[your password]';
-- create sql dump on your computer:
mysqldump [your schema name] > [your schema name].sql
-- and import it to the new DB:
mysql [your schema name] < [your schema name].sql;
Since you are tagging this with "phpmyadmin", try with phpMyAdmin to create your database then import your .sql file.
//recupero il nome del file .sql
$file = "C:/wamp/www/my_folder/test_db.sql";
//definisco il nome del database
$db = 'name_db';
//effettuo la connessione al databse
$conn = mysqli_connect($mysql_host, $mysql_username, $mysql_password) or die('Errore di connessione al server MySQL: ' . mysqli_error());
//seleziono il database
mysqli_select_db($conn,$db);
//leggo il contenuto del file .sql
$qry = file_get_contents($file);
//divido tutte le query presenti nel file .sql
$query = explode(";",$qry);
//conto quante query esistono
$arrlenght = count($query);
//ciclo tutte le query del file
for($i=0;$i<$arrlenght;$i++){
mysqli_query($conn, $query[$i]);
}
Related
I have been seriously searching for an SQL code to backup my database. I use xampp as my local server and also phpmyadmin. I simply want to do something like:
<?php
$conn = mysqli_connect("localhost", "root", " ", "products");
if(!$conn){
die("Unable to connect ".mysql_error());
}else{
$backup = "BACKUP DATABASE products";
$backup_query = mysqli_query($conn, $backup)
}
?>
How do I backup my database and output it in .sql format on the local computer and possibly upload to recover a damaged database?
Thank you so much!
You can use the shell statement "mysqldump" and call it with php like:
<?php
$dbhost = 'localhost';
$dbuser = 'username';
$dbpassword = 'password';
$dbname = 'datenbankname';
$dumpfile = 'backups/' . $dbname . '_' . date("Y-m-d_H-i-s") . '.sql.gz';
passthru("mysqldump --user=$dbuser --password=$dbpassword --host=$dbhost $dbname | gzip -c > $dumpfile");
echo "-- Dump finished -- ";
echo $dumpfile;
Here is my connection code, I have made mysql database in 000webhost and I have also uploaded file in 000webhost but every time I am getting error like this "
PHP Error Message
Warning: mysql_connect() [function.mysql-connect]: Access denied for
user 'a1346052'#'10.1.1.39' (using password: YES) in
/home/a1346052/public_html/connection.php on line 7
Free Web Hosting
Could not connect database
please help me on this, Thanks
<?php
$mysql_hostname = "mysql13.000webhost.com";
$mysql_user = "a1346052";
$mysql_password = "*****";
$mysql_database = "a1346052_simple";
$prefix = "";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
?
I dont think your username will only be that. There will certainly be a username along with the random number. Ex: a7238414_username. Try running following code and see what result it gives.
$dbhost = 'mysql13.000webhost.com';
$dbuser = 'a7238414_username';
$dbpass = '***';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($conn);
Just a headsup, mysql is deprecated. You should start using mysqli or PDO :)
I have a php file to import .csv file into database. For that i am using connection strings like below :
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'demo-eams';
$db_connect = mysql_connect($db_host, $db_user, $db_pass) or die ("Could not connect to MySQL");
$db_select = mysql_select_db($db_name) or die ("Could not connect to database");
i am using sqlyog for access mysql database.
My problem is whenever i run my coding it showing the connection error :
Could not connect to Mysql.
How to solve this?
Check following:
Check if your mysql is up and running
Validate mysql port (if its not default then add port in host name)
So I wanted to install an acp on my homepage. I uploaded all needed files on my webserver (I use FileZilla Client) and opened phpMyAdmin with xampp.
It connects the database but then it says
"Database does not exist."
I'm fairly new to working with phpMyAdmin and mySQL as well.
this is what my db.php looks like:
<?php
$host = "localhost"; // database server
$user = "akichuchan"; // db username
$pw = "cherryjuice"; // db password
$db = "acp"; // db name
$link = mysql_connect($host, $user, $pw, $db) or die ("No connection possible.");
mysql_select_db($db, $link) or die ("Database does not exist.");
?>
What did I do wrong?
Thanks in advance. :)
Try the below working code:
<?php
$host = "localhost"; // database server
$user = "akichuchan"; // db username
$pw = "cherryjuice"; // db password
$db = "acp"; // db name
$link = mysql_connect($host, $user, $pw) or die ("No connection possible.");
mysql_select_db($db, $link) or die ("Database does not exist.");
?>
NOTE: Don't use " mysql_ ". Better to use " PDO or Mysqli_ " ( safe and secure ) .
I am fresher for Open shift.
Here is my problem in Open shift.
I completed creating application with php, Mysql and PhpMyAdmin
Then I went to phpMyAdmin and created a new database 'Students_list'
Now I am unable to connect to my database with the following code in php pages.
$db_host = $_ENV['127.12.123.2']; //sample host
$db_user = $_ENV['adminnxUpXA6'];
$db_pass = $_ENV['lyPfbtHYpDFcU'];
$db_name = $_ENV['students_list']; //this is the database I created in PhpMyAdmin
$db = new mysqli($db_host, $db_user, $db_pass);
if ($db->connect_errno) {
die('Connect Error (' . $db->connect_errno . ') '
. $db->connect_error);
}
mysqli_select_db($db,$db_name);
May I please know where I am comitting the error.
Try this instead:
$db_host = getenv('OPENSHIFT_MYSQL_DB_HOST'); //sample host
$db_user = getenv('OPENSHIFT_MYSQL_DB_USERNAME');
$db_pass = getenv('OPENSHIFT_MYSQL_DB_PASSWORD');
$db_name = 'students_list'; //this is the database I created in PhpMyAdmin
$db = new mysqli($db_host, $db_user, $db_pass);
if ($db->connect_errno) {
die('Connect Error (' . $db->connect_errno . ') '
. $db->connect_error);
}
mysqli_select_db($db,$db_name);
You are using $_ENV, which contains environment variables from the operating system. Most probably what you wanted to do is this:
$db_host = 'ip';
$db_user = 'username';
$db_pass = 'password';
$db_name = 'students_list';
The structure for the mysqli connection string is this:
mysqli(Hostname,Username,Password,DatabaseName);