script to support mysql databases in php, but I need to migrate to mysqli

1
    <?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>";


?>

It 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 change to mysqli but it stops working, you could advise me how I can migrate to mysqli, as well as how I could restore the latest backed copy.

Regarding the fact that it stops working, it actually creates the file but without information and it 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.

    
asked by Stravos77 27.08.2016 в 00:04
source

2 answers

1

One serious option:

pay attention to the configuration of the php in case the 'mysqli' option is enabled in the extension part. If it is not activated, it can not be used. You just have to remove the semicolon next (;) and that's it.

I think it's like that

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

Modified:

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

According to the documentation that can be found here ,

the connector object that is what you get when you make 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 $ name)",

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

of the database you are trying to connect to.

So, you should review the others who gave you similar errors and

fix according to.

I hope this helps you to solve the errors.

    
answered by 27.08.2016 / 01:02
source
0

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

    
answered by 29.08.2016 в 18:50