-1

I am trying to take the whole mysql database backup. I have searched the code, but when I am trying to use it, it gives me error saying Deprecated: Function ereg_replace() is deprecated function

I don't know much about where I am wrong. Help and suggestion will be highly appreciated. Following is the php code. Please put your own database credentials

static function db_backup()
{


    $host=self::host;
    $pass=  self::pass;
    $name=  self::db;
    $user=  self::user;
    $tables = '*';


     $return='';
    $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] = 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);
}        
}

When I am trying to replace ereg_replace() function with preg_replace () i am struggling with this this error preg_replace(): Empty regular expression

  • http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html – Digital Chris Dec 20 '13 at 13:12
  • The `ereg_replace()` function is deprecated. The error message couldn’t be any clearer. – Martin Bean Dec 20 '13 at 13:12
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – h2ooooooo Dec 20 '13 at 13:12
  • @h2ooooooo i am using PDO but the code which i found on internet uses mysql – user3122845 Dec 21 '13 at 19:40

1 Answers1

0

Please use the search function, or google for that matter. A simple search would have resulted in: http://php.net/preg_replace

Dirk de Man
  • 657
  • 3
  • 9