0

I need your help. For my website, I was using a simple PHP script that was creating an email with the description of my database and all the data in it, and it was sending it to my email once I clicked on a special link on the admin page.

Suddenly it does not work anymore, probably because the web server on which I uploaded the site has changed some parameters, which I cannot customize (I'm working on Altervista).

Do you have any idea of how to create a script that allows me to download the same backup by clicking on the same link, instead of receiving an email?

The former script was something like this

$table_top = "<table class=\"tab\">";
$table_bottom = "</table>";
$mail="<html><body>";
$add="whatever@example.com";
$tit="Backup MySQL (".date("d-m-Y, H:i",time()).")";
$headers="Content-type: text/htmlrn";

$query = "SHOW TABLES FROM my_database"; 
$result = mysql_query($query);
while($row = mysql_fetch_row($result)){
    $table = $row[0];
    $mail.="<h2>table: ".$table."</h2>";
    $query2 = "DESCRIBE $table"; 
    $result2 = mysql_query($query2);
    $mail.=$table_top;
    $mail.="<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th><th>Extra</th><tr>";
    while($row2 = mysql_fetch_row($result2)){
        $k2=count($row2);
        $mail.="<tr>";
        for($i2=0; $i2<$k2; $i2++){
            $mail.="<td>".$row2[$i2]."</td>";
        }
        $mail.="</tr>";
    }
    $mail.=$table_bottom."<br><br>";
    $query2 = "SELECT * FROM $table"; 
    $result2 = mysql_query($query2);
    while($row2 = mysql_fetch_row($result2)){
        $mail.="\"".$row2[0]."\"";
        $i=1;
        while(isset($row2[$i])){
            $mail.=","."\"".$row2[$i]."\"";
            $i=$i+1;
        }
        $mail.="<br>";
    }
}
$mail.="</body></html>";
mail($add,$tit,$mail,$headers);

Thanks guys! :)

Sfrow
  • 381
  • 1
  • 2
  • 15
  • 1
    If anything changed on the server, it's the PHP version. Add error reporting to the top of your file(s) right after your opening ` – Funk Forty Niner Dec 09 '14 at 14:48
  • 2
    You may have gotten caught - [Here is an example of what happens when you continue to use `mysql_*` functions.](http://stackoverflow.com/questions/26299564/php-version-upgraded-cannot-use-few-functions) Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. – Jay Blanchard Dec 09 '14 at 14:49
  • @JayBlanchard Good day Jay ;) long time no see. – Funk Forty Niner Dec 09 '14 at 14:50
  • This library may helpful to you.I feel it's more reliable. https://github.com/ifsnop/mysqldump-php – Dhruv Kapatel Dec 09 '14 at 14:50
  • Good day Fred ;) I was out of town at a wedding. Just settling back in this morning. – Jay Blanchard Dec 09 '14 at 14:53
  • @JayBlanchard Cool! Did you catch the garter? – Funk Forty Niner Dec 09 '14 at 14:53
  • I'll never tell @Fred-ii- ;) – Jay Blanchard Dec 09 '14 at 14:55
  • Yeah, you were right: I forgot to update this script to MySQLi. Now it is updated, the mail has been sent by the website but I have never received it. But I know that this issue is not solvable by PHP. To get back to the original question, is there a way to create and to download a backup file instead (maybe .csv or .sql)? – Sfrow Dec 09 '14 at 15:49
  • 1
    See this Q&A's on Stack http://stackoverflow.com/q/8166277/ - You can further your research on Google using what I used for keywords to find that "backup database and download .sql file php". Use a header. – Funk Forty Niner Dec 09 '14 at 16:38

0 Answers0