Jump to content

Trouble with mySQL export script


JamesH106

Recommended Posts

Hey

I am making a mySQL administrator in php, and I am having trouble getting it to export a script.

This is a snippet of my mySQL class:
[code]<?php
class mysql {
    function export($db, $tables) {
        mysql_select_db($db) or die(mysql_error());
        $result = "## Dump created ".date("d M Y")." ##";
        foreach($tables as $t) {
                $query = mysql_query("select * from $t") or die(mysql_error());
                $num_fields = mysql_num_fields($query);
                $numrow = mysql_num_rows($query);
                for ($i =0; $i<$numrow; $i++) {
                    $result .= "INSERT INTO ".$t." 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])) {
                            $result .= "\"$row[$j]\"";
                        } else {
                            $result .= "\"\"";
                        }

                        if ($j<($num_fields-1)) $result .= ",";

                    }
                $result .= ");\n";
                }
        }
        $file_name = "MySQL_Database_Backup.sql";
        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=$file_name");
        print $result;
    }
}
?>[/code]

..and this is how I am calling the function up:

[code]<?php
$mysql = new mysql;
$mysql->connect(***,***,***);
$tables = array();
$tables[] = 'banned';
$mysql->export(smf,$tables);
?>[/code]

..but it doesn't work :(

If I do not add anything into the array (i.e. take line 4 out of my second code snippet), I will get my file back, with information about the dump. If I do put some tables in my array, though, I do not get anything back. The script does not exit, I just don't get my file back.

Any help is [b]greatly[/b] apprecitated  :)
Link to comment
https://forums.phpfreaks.com/topic/27675-trouble-with-mysql-export-script/
Share on other sites

When you are calling the calss you are passing [b]smf[/b] - shouldn't that be [b]'smf'[/b] in quotes?

Regardless, I suggest you debug the code outside of a class first. Run all the code inline. Once it works then put it into a class. Also, put some echo's at certain steps to see what is happening as the code progresses.
Thank you very much for your reply. I have inserted echo's into many places to see where the error is occuring, and the error seems to be in the foreach. This is what I used:

[code]<?php
    // to export (backup) a database
    function export($db, $tables) {
echo "Working 1";
        mysql_select_db($db) or die(mysql_error());
echo "Working 2";
        $result = "## Dump created ".date("d M Y")." ##";
        foreach($tables as $t) {
"Working @ foreach";
                $query = mysql_query("select * from $t") or die(mysql_error());
                $num_fields = mysql_num_fields($query);
                $numrow = mysql_num_rows($query);
                for ($i =0; $i<$numrow; $i++) {
                    $result .= "INSERT INTO ".$t." 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])) {
                            $result .= "\"$row[$j]\"";
                        } else {
                            $result .= "\"\"";
                        }

                        if ($j<($num_fields-1)) $result .= ",";

                    }
                $result .= ");\n";
                }
        }
print $result;
        #$file_name = "MySQL_Database_Backup.sql";
        #header("Content-type: application/octet-stream");
        #header("Content-Disposition: attachment; filename=$file_name");
        #print $result;
    }
?>[/code]

..and..

[code]<?php
$mysql = new mysql;
$mysql->connect(****,****,****);
$tables = array();
$tables[] = 'banned';
$mysql->export('smf',$tables);
?>[/code]

when I ran it I got this:

Working 1Working 2## Dump created 18 Nov 2006 ##

So the foreach is obviously not working.

Thanks again for your help :)

James.
Try adding this just before the foreach

echo "Table is set: ";
echo (isset($tables)) ? "true" : "false";
echo "<br>Table is an array: ";
echo (is_array($tables)) ? "true" : "false";
if (is_array($tables)) {
  echo "<br>Items in array: " . count($tables) . "<br>";
}
[quote author=mjdamato link=topic=115436.msg470157#msg470157 date=1163874107]
Try adding this just before the foreach

echo "Table is set: ";
echo (isset($tables)) ? "true" : "false";
echo "<br>Table is an array: ";
echo (is_array($tables)) ? "true" : "false";
if (is_array($tables)) {
  echo "<br>Items in array: " . count($tables) . "<br>";
}
[/quote]

Table is set: true
Table is an array: true
Items in array: 1
OK, I got it to work. First thing though was I had to do the mysql connection "locally" - i.e. not trying to open the connection in the class.

[code]<?php
// to export (backup) a database

class mysql {
  function export($db, $tables) {

    mysql_select_db($db) or die(mysql_error());

    $result = "## Dump created ".date("d M Y")." ##\n";

    foreach($tables as $t) {

        $query = mysql_query("select * from $t") or die(mysql_error());

        if (mysql_num_rows($query)>0) {
            while ($row = mysql_fetch_assoc($query)) {
                $result .= "INSERT INTO ".$t." VALUES(";
                $sep = "";
                foreach ($row as $value) {
                    $value = addslashes($value);
                    $value = ereg_replace("\n","\\n",$value);
                    $result .= $sep . "'" . $value . "'";
                    $sep = ",";
                }
                $result .= ");\n";
            }
        }
    }
    print "<pre>$result</pre>";
    #$file_name = "MySQL_Database_Backup.sql";
    #header("Content-type: application/octet-stream");
    #header("Content-Disposition: attachment; filename=$file_name");
    #print $result;
  }
}
?>[/code]

[code]<?php

include('sqlclass.php');

// Connect to DB
$dbhost = 'localhost';
$dbuser = 'XXXX';
$dbpasswd = 'XXXX';
mysql_connect($dbhost,$dbuser,$dbpasswd);

$mysql = new mysql;
$tables = array();
$tables[] = 'movies';
$mysql->export('test',$tables);

?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.