SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problems with case statement

    Hi

    I've got a function that backs up a MySQL DB but am struggling adding error messages. I've managed to redirect on success, but would like to redirect to "index.php?db-backup=error" if the process fails for some reason. Code below and any help would be great.

    Code:
    <?php
    include("config.php");
    include("db_open.php");
    backup_tables('$dbhost','$dbuser','$dbpass','$dbname');
    function backup_tables($dbhost,$dbuser,$dbpass,$dbname,$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);
    }
    // Loop
    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";
    }
    // Write File
    $handle = fopen('../backup/DB-Backup-'.date("dmY").'-'.date("his").'.sql','w+');
    fwrite($handle,$return);
    fclose($handle);
    header("location: ../index.php?db-backup=success");
    }
    ?>

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if the process fails for some reason
    you would need to check for failure, eg
    PHP Code:
    $result mysql_query('SELECT * FROM '.$table);
    if(!
    result) {
        
    // failed, redirect or whatever needs to happen

    for PHP failures you could look at using a custom error handler


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •