JamesH106 Posted November 18, 2006 Share Posted November 18, 2006 HeyI 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]<?phpclass 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 More sharing options...
Psycho Posted November 18, 2006 Share Posted November 18, 2006 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. Link to comment https://forums.phpfreaks.com/topic/27675-trouble-with-mysql-export-script/#findComment-126652 Share on other sites More sharing options...
JamesH106 Posted November 18, 2006 Author Share Posted November 18, 2006 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. Link to comment https://forums.phpfreaks.com/topic/27675-trouble-with-mysql-export-script/#findComment-126707 Share on other sites More sharing options...
Psycho Posted November 18, 2006 Share Posted November 18, 2006 Try adding this just before the foreachecho "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>";} Link to comment https://forums.phpfreaks.com/topic/27675-trouble-with-mysql-export-script/#findComment-126720 Share on other sites More sharing options...
Psycho Posted November 18, 2006 Share Posted November 18, 2006 Also, you can't get the results from the query in the manner that you have it. Needs to be like this:while ($row = mysql_fetch_assoc($query)) { Link to comment https://forums.phpfreaks.com/topic/27675-trouble-with-mysql-export-script/#findComment-126736 Share on other sites More sharing options...
JamesH106 Posted November 18, 2006 Author Share Posted November 18, 2006 [quote author=mjdamato link=topic=115436.msg470157#msg470157 date=1163874107]Try adding this just before the foreachecho "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: trueTable is an array: trueItems in array: 1 Link to comment https://forums.phpfreaks.com/topic/27675-trouble-with-mysql-export-script/#findComment-126747 Share on other sites More sharing options...
Psycho Posted November 18, 2006 Share Posted November 18, 2006 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 databaseclass 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]<?phpinclude('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] Link to comment https://forums.phpfreaks.com/topic/27675-trouble-with-mysql-export-script/#findComment-126771 Share on other sites More sharing options...
JamesH106 Posted November 18, 2006 Author Share Posted November 18, 2006 Thank you very much - it's working like a charm 8) Link to comment https://forums.phpfreaks.com/topic/27675-trouble-with-mysql-export-script/#findComment-126785 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.