Error backup sql database but succeed creating database dump file - php

I tried this following code to backup mysql database into file and it works perfectly create the .sql file I need.
function backup_tables()
{
$host='localhost';
$user='root';
$pass='';
$name='evote';
$tables = '*';
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = str_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
//save file
$sql_name='db-backup.sql';
$handle = fopen($sql_name,'w+');
fwrite($handle,$return);
fclose($handle);
return $sql_name;
}
but it shows the error code :
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: return
Filename: models/vote_m.php
can somebody tell me how to fix this?

The first time you reach this line;
$return.= 'DROP TABLE '.$table.';';
...$return has no value to append to. To get rid of the warning, you'll need to initialize $return (to an empty string probably) before starting the loop.

Related

How to take backup of database in php?

I am doing some projects related to finance and for that i am using mysql database.Can any one suggest me how to take backup of database after each operation?
In this code, enter host, username, password, database or table name in first line : backup_tables('localhost','useranme','password','database_or_table_name');
You will get a file named db-backup....... in htdocs.
This works for me. Try This code:
<?php
// Enter database name here
backup_tables('localhost','useranme','password','database_or_table_name');
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
//cycle through
$return = "";
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE IF EXISTS '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j < $num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j < ($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
//save file
$handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
fwrite($handle,$return);
fclose($handle);
}
?>

Export SQL File not written in specified file path in php?

I'm new to codeigniter and version is 2.1.4, i have problem in export database in sql method. I have given code below, when i run the code it will not show any errors. When i check the given path no files created. Can any one help me. Thanks in advance.
function backup_tables($host='localhost',$user='arts',$pass='arts123',$name='arts',$tables = 'sales')
{
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
$return = "";
//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
//$return.= 'CREATE TABLE '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
// $row[$j] = ereg_replace("\n","\\n",$row[$j]);
$row[$j] = preg_replace("#\n#", "\\n", $row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
$backup_file = '/var/www/html/art/assets/uploads/csv/backup-'.date('M').'-'.date('Y').'.sql';
//save file
$handle = fopen($backup_file,'w+');
fwrite($handle,$return);
fclose($handle);
exit;
}

PHP Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in [duplicate]

This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 2 years ago.
I don't know why I recieve this error.
Here is what my code looks like:
<?php ini_set('memory_limit', '1000M'); set_time_limit(0);
backup_tables('host','dbuser','password','dbname','dbtable');
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
if($tables == '*') {
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result)) {
$tables[] = $row[0];
}
} else {
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
foreach($tables as $table) {
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++) {
while($row = mysql_fetch_row($result)) {
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++) {
$row[$j] = addslashes($row[$j]);
$row[$j] = preg_replace("/\n/i", "\\n",$row[$j]);;
if (isset($row[$j])) {
$return.= '"'.$row[$j].'"' ;
} else {
$return.= '""';
}
if ($j<($num_fields-1)) {
$return.= ',';
}
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
$handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
fwrite($handle,$return); fclose($handle);
} ?>
firstly, all your code on one line makes it awfully hard to help you debug.
secondly check $result before passing it to mysql_num_fields you'll find its false because the query failed.
check here for more info on a similar problem thats explained in a better way
Change to this it might work :
$result = mysql_query('SELECT * FROM `$table`');

gibberish data in utf8 columns while exporting sql database

I have used the db_backup function from PongoCMS to make the backup of the MySQL database. Almost all the columns in all the database tables are exported properly but on table columns with 'utf8' encoding (i've used utf8_unicode_ci collation), the exported data of that columns are filled with question marks.
Following is the function that I used:
public static function db_backup()
{
$type_db = Config::get('database.default');
$connections = Config::get('database.connections');
switch($type_db) {
case 'sqlite':
$file_name = $connections[$type_db]['database'] . '.' . $connections[$type_db]['driver'];
break;
case 'mysql':
$link = mysql_connect($connections[$type_db]['host'],$connections[$type_db]['username'],$connections[$type_db]['password']);
mysql_select_db($connections[$type_db]['database'],$link);
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
//Set time now
$now = date('Y-m-d-H-i-s');
//File header
$return ="### DB BACKUP: " . $connections[$type_db]['database'] . " at " . $now . " ###\n\n\n";
//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE IF EXISTS '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = preg_replace("#\n#i","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
//save file
$file_name = 'db-backup-'.$now.'.sql';
$handle = fopen(path('storage') . 'database/' . $file_name, 'w+');
fwrite($handle, utf8_encode($return));
fclose($handle);
}
return $file_name;
}
However, if I export the database from phpmyadmin, the database is exported properly. What should be changed/added in the above function so that the function also exports the database properly?
Try execute DB query: "SET NAMES 'utf8'".
But it needs to be executed first! So, put next line in you script:
mysql_query("SET NAMES 'utf8'");
And keep in mind, this query must be very first query after "mysql_select_db"
Hope it will solve your issue, good luck!

Backup an entire database with PHP/MYSQL

I am trying to perform php/mysql backups
I receive values from a form page and then with the command "select tables", i save those values in a array.
After that i do a "for" loop to backup each table:
<?php
$dbname = $_POST['txt_db_name'];
$tbname = $_POST['txt_tb_name'];
$ligacao=mysql_connect('localhost','root','')
or die ('Problemas na ligação ao Servidor de MySQL');
$res = mysql_query("SHOW TABLES FROM pessoal");
$tables = array();
mysql_select_db($dbname,$ligacao);
while($row = mysql_fetch_array($res, MYSQL_NUM)) {
$tables[] = "$row[0]";
}
$length = count($tables);
for ($i = 0; $i < $length; $i++) {
$query=
"SELECT * INTO OUTFILE 'pessoa_Out.txt'".
"FIELDS TERMINATED BY ',' ".
"ENCLOSED BY '\"'".
"LINES TERMINATED BY '#'".
"FROM $tables[$i]";
$resultado = mysql_query($query,$ligacao);
}
mysql_close();
if ($resultado)
$msg ='Sucesso na Exportaçao da Database '.$dbname.' ';
else
$msg ='Erro Impossivel Exportar a Database '.$tbname.' ';
?>
Don't do this — you are re-inventing existing tools!
Invoke mysqldump instead, which is expressly designed for the purpose.
With the proper permissions, you can use PHP's system or exec to invoke this.
First, I agree with the ones saying that mysqldump should be used for this. But basing on your comment that you just need it to be done with php/mysql for educational (or whatever) purposes, here is the script (yep, it re-invents the wheel). Note that you should create a backup directory in the folder where you upload this and allow web server to write to it :
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
ini_set('memory_limit','1500M');
set_time_limit(0);
backup_tables('localhost','user','xxxxxxx','xxxxxxxxxx');
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
//save file
$handle = gzopen(getcwd() . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'db-backup-'.time().'.sql.gz','w9');
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return = 'DROP TABLE IF EXISTS '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return .= "\n\n".$row2[1].";\n\n";
gzwrite($handle,$return);
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return = 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = str_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
gzwrite($handle,$return);
}
}
$return ="\n\n\n";
gzwrite($handle,$return);
}
gzclose($handle);
}

Categories