script to support mysql databases in php, but I require migrating to mysqli

    <?php

backup_tables('XXXXXXX','XXXXXXX','XXXXXXX','XXXXXXX');


/* backup the db OR just a table */
//En la variable $talbes puedes agregar las tablas especificas separadas por comas:
//profesor,estudiante,clase
//O déjalo con el asterisco '*' para que se respalde toda la base de datos

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
   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);
}

echo "<script>";
  echo "alert ('Respaldo realizado exitosamente.');";
  echo "window.location.replace('index.php');";
  echo "</script>";


?>

Works but it marks me these errors:

Deprecated: mysql_connect (): the mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead.

Deprecated: function ereg_repace () is deprecated

I have tried to switch to mysqli but it stops working, they could advise me how I can migrate to mysqli, also how I could restore the most recent backed up copy.

Regarding that it stops working, it actually creates the file but without information and marks me the following errors:

Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in / backup / backup.php on line 15 Warning: mysqli_query () expects at least 2 parameters, 1 given in /backup/backup.php on line 21 Warning: mysqli_fetch_row () expects parameter 1 to be mysqli_result, null given in / backup / backup.php on line 22

Thank you very much.

 1
Author: Comunidad, 2016-08-26

2 answers

A serious option:

Check the php configuration if the 'mysqli' option is enabled in the Extensions section. If it is not activated, it cannot be used. Just remove the semicolon next to it (;) and that's it.

I think it's like this

...
;extensión = mysqli
...

Modified:

In such a case, I would say that you have misstated the order of the parameters.

According to the documentation you can find here ,

The connector object what do you get by making the call

"mysqli_connect", should come as the first parameter in the"mysqli_select_db" function.

For example, in the case of "mysqli_select_db(Mysqli $link, String nombre name)",

$link (of type Mysqli) is the connector and nombre name (type String) is the name

From the database you are trying to connect to.

So, you should check others that have given you similar errors and

Coregir under.

I hope this helps you solve the errors.

 1
Author: P.J Palmer, 2016-08-27 19:41:35

I have already solved the errors that existed, change obsolete commands, no longer mark any errors and back up all tables with their content, thank you very much for your comments, greetings.

 0
Author: Stravos77, 2016-08-29 16:50:14