monkeypaw201 Posted June 1, 2008 Share Posted June 1, 2008 I have a SQL file that i need to run through PHP, the problem is..well, im not sure how I have heard about using the INLINE or SOURCE in mysql_query, but im not sure... If it helps, it only updates 1 table... Quote Link to comment https://forums.phpfreaks.com/topic/108261-solved-run-sql-file-in-php/ Share on other sites More sharing options...
monkeypaw201 Posted June 1, 2008 Author Share Posted June 1, 2008 i get the following error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SOURCE install.sql' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/108261-solved-run-sql-file-in-php/#findComment-555023 Share on other sites More sharing options...
.josh Posted June 1, 2008 Share Posted June 1, 2008 <?php class chalkboard { function writedown ($msg, $num) { for ($x = 1;$x <= $num; $x++) { echo "$x : $msg <br />"; } } } $chalk = new chalkboard(); $chalk->writedown("I will post my code.", 100); ?> Quote Link to comment https://forums.phpfreaks.com/topic/108261-solved-run-sql-file-in-php/#findComment-555052 Share on other sites More sharing options...
monkeypaw201 Posted June 1, 2008 Author Share Posted June 1, 2008 ... All you had to do was ask here is the first chunk, on upload.php <?php require_once("Includes/parse.class.php"); mysql_select_db($database_vamsys, $vamsys); $parseObj = new parse("Templates/$zip_dir/install.sql"); $res = $parseObj->startParsing(); ?> on the parse.class.php: <?php class parse { var $file; function parse($file) { $this->setFile($file); $this->startParsing(); } /** * @purpose : Sets filename to be parsed * @params $file * @return none */ function setFile($file) { $this->file = $file; } /** * @purpose : Gets filename to be parsed * @params none * @return filename */ function getFile() { return $this->file; } /** * @purpose : Parses SQL file * @params none * @return none */ function startParsing() { $file = $this->getFile(); // Getting the SQL file content $content = file_get_contents($file); // Processing the SQL file content $file_content = explode("\n",$content); $query = ""; // Parsing the SQL file content foreach($file_content as $sql_line) { if(trim($sql_line) != "" && strpos($sql_line, "--") === false) { $query .= $sql_line; // Checking whether the line is a valid statement if(preg_match("/(.*);/", $sql_line)) { $query = substr($query, 0, strlen($query)-1); //Executing the parsed string, returns the error code in failure $result = mysql_query($query)or die(mysql_error()); $query = ""; } } } //End of foreach return true; } //End of function } //End of class ?> it works fine, however it seems to be repeating everything in the sql file twice?! Quote Link to comment https://forums.phpfreaks.com/topic/108261-solved-run-sql-file-in-php/#findComment-555068 Share on other sites More sharing options...
.josh Posted June 1, 2008 Share Posted June 1, 2008 I believe it's because you have a method named the same as your class, making it a constructor. Your constructor automatically gets run when you create your object, which runs your setfile/startparsing methods, but then you turn around and run the method again here: $res = $parseObj->startParsing(); Quote Link to comment https://forums.phpfreaks.com/topic/108261-solved-run-sql-file-in-php/#findComment-555071 Share on other sites More sharing options...
monkeypaw201 Posted June 1, 2008 Author Share Posted June 1, 2008 now, should i rename the class or remove the $res line? Quote Link to comment https://forums.phpfreaks.com/topic/108261-solved-run-sql-file-in-php/#findComment-555074 Share on other sites More sharing options...
.josh Posted June 1, 2008 Share Posted June 1, 2008 or the function. doesn't really matter. just don't make them the same name. Or you can setup your script to keep it as a constructor and just not call it twice. It's up to you. Quote Link to comment https://forums.phpfreaks.com/topic/108261-solved-run-sql-file-in-php/#findComment-555078 Share on other sites More sharing options...
monkeypaw201 Posted June 1, 2008 Author Share Posted June 1, 2008 now i renamed my class to `parsed` so the parse.class.php starts with class parsed and the upload has $parseObj = new parsed(".."); but now it will not execute at all?! Quote Link to comment https://forums.phpfreaks.com/topic/108261-solved-run-sql-file-in-php/#findComment-555079 Share on other sites More sharing options...
.josh Posted June 1, 2008 Share Posted June 1, 2008 okay so now you renamed your class. You make a new object, passing your file name. You then execute startparsing() ...and it's trying to getfile but you never passed it the filename because your parse method that was acting as a constructor did that before, and now it's not doing that. So now you have to pass your argument over to it from inside your startparsing method. Quote Link to comment https://forums.phpfreaks.com/topic/108261-solved-run-sql-file-in-php/#findComment-555081 Share on other sites More sharing options...
.josh Posted June 1, 2008 Share Posted June 1, 2008 p.s.- since you opted for changing the class name, your parse method pretty much becomes obsolete, unless you want to call that first, instead of startparsing. Quote Link to comment https://forums.phpfreaks.com/topic/108261-solved-run-sql-file-in-php/#findComment-555083 Share on other sites More sharing options...
monkeypaw201 Posted June 1, 2008 Author Share Posted June 1, 2008 ok, you lost me after make a new object... what requires the least amount of re-coding?? :-\ Quote Link to comment https://forums.phpfreaks.com/topic/108261-solved-run-sql-file-in-php/#findComment-555084 Share on other sites More sharing options...
.josh Posted June 1, 2008 Share Posted June 1, 2008 Okay, you renamed your class. So if I read your script right, what should work is, instead of doing this: $parseObj = new parse("Templates/$zip_dir/install.sql"); $res = $parseObj->startParsing(); do this: $parseObj = new parsed(); // you did rename it to parsed, right? // $res = $parseObj->startParsing(); old method call // call this instead, passing the filename to it $res = $parseObj->parse("Templates/$zip_dir/install.sql"); Quote Link to comment https://forums.phpfreaks.com/topic/108261-solved-run-sql-file-in-php/#findComment-555091 Share on other sites More sharing options...
monkeypaw201 Posted June 1, 2008 Author Share Posted June 1, 2008 thanks that fixed it Quote Link to comment https://forums.phpfreaks.com/topic/108261-solved-run-sql-file-in-php/#findComment-555094 Share on other sites More sharing options...
.josh Posted June 1, 2008 Share Posted June 1, 2008 Awesomesauce Quote Link to comment https://forums.phpfreaks.com/topic/108261-solved-run-sql-file-in-php/#findComment-555100 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.