Re: [PHP] novice: how to run .sql script from php?
at the depth of execQuery(), an in-house function, it has already selected a database, (default for the project). my apologise for saying problem with using mysql_query( ...), it was my misunderstanding of this in-house function. Now I can run the script with Create database ... in the script. yes file_get_contents('installation.sql') was much better :) the problem is that not all the client site will have phpMyAdmin, now the problem has been reduced to just get rid of all comment/non-sql fluff and do execQuery(file_get_contents('installation.sql')) thanks everyone. M. Sokolewicz [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] tony yau wrote: I realised that there is a fundamental problem with using mysql_query( ...) to run a .sql script to setup a database, and that was the database needs to be there in the first place for php to connect to! also there was a lot of comment lines in the script that was causing problem. ... so I gave up trying to parse the file. instead I did this 1) create the database with phpmyadmin etc 2) remove (by hand) all comment lines from the .sql file and then include the file ob_start(); include 'installation.sql'; $contents = ob_get_contents(); ob_end_clean(); this is *very* overTheTop; why not just do: $contents = file_get_contents('installation.sql'); ??? execQuery($contents); not very good but does the job. thanks for all the help tony yau Rory Browne [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] I'm assuming that the .sql file consists of a list of MySQL queries, that when performed sequentially will set up your system. That being the case, the perfered way ot install the thing is to do a 'mysql [host/username/password parameters] file.sql'. I believe you can also run file.sql scripts using phpMyAdmin. If you were to define a mysql_run_script() function, it would look a bit like the following: ?pseudo_code function mysql_run_script($file){ $sql = file_get_contents($file); $queries = split_sql_into_individual_sql_queries($sql); foreach($queries as $query){ mysql_query($query); } } ? Come to think of it, you could turn the above pseudo code into valid php code, by defining the split_sql_into_individual_sql_queries() function. This would involve splitting by ';', taking into account the possibility that ';' may occur in the middle of a string. Check out the code for phpmyadmin, or phpbb(db backup/recover feature), for a better idea. On 5/27/05, tony yau [EMAIL PROTECTED] wrote: Hi All, I got this .sql script that setup the whole db schema, I can run mysql.exe to run on my pc but when it goes to a hosting company's server I don't have that command! So I tried to include(setup.sql) into a string and send that as one long sql query. BUT I need to get rid of all the comment lines first in the script!! can someone give me a better idea of doing this. (there must be an equivalent php function like mysql_run_script(setup.sql)) thanks -- Tony Yau -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] novice: how to run .sql script from php?
I realised that there is a fundamental problem with using mysql_query( ...) to run a .sql script to setup a database, and that was the database needs to be there in the first place for php to connect to! also there was a lot of comment lines in the script that was causing problem. ... so I gave up trying to parse the file. instead I did this 1) create the database with phpmyadmin etc 2) remove (by hand) all comment lines from the .sql file and then include the file ob_start(); include 'installation.sql'; $contents = ob_get_contents(); ob_end_clean(); execQuery($contents); not very good but does the job. thanks for all the help tony yau Rory Browne [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] I'm assuming that the .sql file consists of a list of MySQL queries, that when performed sequentially will set up your system. That being the case, the perfered way ot install the thing is to do a 'mysql [host/username/password parameters] file.sql'. I believe you can also run file.sql scripts using phpMyAdmin. If you were to define a mysql_run_script() function, it would look a bit like the following: ?pseudo_code function mysql_run_script($file){ $sql = file_get_contents($file); $queries = split_sql_into_individual_sql_queries($sql); foreach($queries as $query){ mysql_query($query); } } ? Come to think of it, you could turn the above pseudo code into valid php code, by defining the split_sql_into_individual_sql_queries() function. This would involve splitting by ';', taking into account the possibility that ';' may occur in the middle of a string. Check out the code for phpmyadmin, or phpbb(db backup/recover feature), for a better idea. On 5/27/05, tony yau [EMAIL PROTECTED] wrote: Hi All, I got this .sql script that setup the whole db schema, I can run mysql.exe to run on my pc but when it goes to a hosting company's server I don't have that command! So I tried to include(setup.sql) into a string and send that as one long sql query. BUT I need to get rid of all the comment lines first in the script!! can someone give me a better idea of doing this. (there must be an equivalent php function like mysql_run_script(setup.sql)) thanks -- Tony Yau -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] novice: how to run .sql script from php?
tony yau wrote: I realised that there is a fundamental problem with using mysql_query( ...) to run a .sql script to setup a database, and that was the database needs to be there in the first place for php to connect to! also there was a lot of comment lines in the script that was causing problem. ... so I gave up trying to parse the file. instead I did this 1) create the database with phpmyadmin etc 2) remove (by hand) all comment lines from the .sql file and then include the file ob_start(); include 'installation.sql'; $contents = ob_get_contents(); ob_end_clean(); this is *very* overTheTop; why not just do: $contents = file_get_contents('installation.sql'); ??? execQuery($contents); not very good but does the job. thanks for all the help tony yau Rory Browne [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] I'm assuming that the .sql file consists of a list of MySQL queries, that when performed sequentially will set up your system. That being the case, the perfered way ot install the thing is to do a 'mysql [host/username/password parameters] file.sql'. I believe you can also run file.sql scripts using phpMyAdmin. If you were to define a mysql_run_script() function, it would look a bit like the following: ?pseudo_code function mysql_run_script($file){ $sql = file_get_contents($file); $queries = split_sql_into_individual_sql_queries($sql); foreach($queries as $query){ mysql_query($query); } } ? Come to think of it, you could turn the above pseudo code into valid php code, by defining the split_sql_into_individual_sql_queries() function. This would involve splitting by ';', taking into account the possibility that ';' may occur in the middle of a string. Check out the code for phpmyadmin, or phpbb(db backup/recover feature), for a better idea. On 5/27/05, tony yau [EMAIL PROTECTED] wrote: Hi All, I got this .sql script that setup the whole db schema, I can run mysql.exe to run on my pc but when it goes to a hosting company's server I don't have that command! So I tried to include(setup.sql) into a string and send that as one long sql query. BUT I need to get rid of all the comment lines first in the script!! can someone give me a better idea of doing this. (there must be an equivalent php function like mysql_run_script(setup.sql)) thanks -- Tony Yau -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] novice: how to run .sql script from php?
On 5/29/05, tony yau [EMAIL PROTECTED] wrote: I realised that there is a fundamental problem with using mysql_query( ...) to run a .sql script to setup a database, and that was the database needs to be there in the first place for php to connect to! also there was a lot of comment lines in the script that was causing problem. ... so I gave up trying to parse the file. Umm, I suggest you read an awful lot more on mysql. I could be wrong on this, but I can't at face value see anything wrong with mysql_query(create database database_name); mysql_query(use database_name). Your problem seems to be your lack of knowledge regarding mysql, and using mysql from PHP. instead I did this 1) create the database with phpmyadmin etc 2) remove (by hand) all comment lines from the .sql file and then include the file ob_start(); include 'installation.sql'; $contents = ob_get_contents(); ob_end_clean(); execQuery($contents); ob_start(); include 'installation.sql'; $contents = ob_get_contents(); ob_end_clean(); is spagetti code. use $contents = file_get_contents(installation.sql) instead. is a bad way of writing that: $contents = file_get_contents(installation.sql). Where did you get the execQuery() from? If you have an execQuery() function that allows you to do multiple queries seperated by semicolons, then you're sorted. 1 not very good but does the job. thanks for all the help tony yau Rory Browne [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] I'm assuming that the .sql file consists of a list of MySQL queries, that when performed sequentially will set up your system. That being the case, the perfered way ot install the thing is to do a 'mysql [host/username/password parameters] file.sql'. I believe you can also run file.sql scripts using phpMyAdmin. If you were to define a mysql_run_script() function, it would look a bit like the following: ?pseudo_code function mysql_run_script($file){ $sql = file_get_contents($file); $queries = split_sql_into_individual_sql_queries($sql); foreach($queries as $query){ mysql_query($query); } } ? Come to think of it, you could turn the above pseudo code into valid php code, by defining the split_sql_into_individual_sql_queries() function. This would involve splitting by ';', taking into account the possibility that ';' may occur in the middle of a string. Check out the code for phpmyadmin, or phpbb(db backup/recover feature), for a better idea. On 5/27/05, tony yau [EMAIL PROTECTED] wrote: Hi All, I got this .sql script that setup the whole db schema, I can run mysql.exe to run on my pc but when it goes to a hosting company's server I don't have that command! So I tried to include(setup.sql) into a string and send that as one long sql query. BUT I need to get rid of all the comment lines first in the script!! can someone give me a better idea of doing this. (there must be an equivalent php function like mysql_run_script(setup.sql)) thanks -- Tony Yau -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] novice: how to run .sql script from php?
tony yau wrote: I realised that there is a fundamental problem with using mysql_query( ...) to run a .sql script to setup a database, and that was the database needs to be there in the first place for php to connect to! also there was a lot of comment lines in the script that was causing problem. ... so I gave up trying to parse the file. instead I did this 1) create the database with phpmyadmin etc so you have phpmyadmin installed. good. now while you have the database selected, 2) click the SQL (it's in the toolbar in the right frame) 3) click Browse... button, find the .sql file, click OK 4) click the bottom Go button, wait and relax -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] novice: how to run .sql script from php?
tony yau wrote: Hi All, I got this .sql script that setup the whole db schema, I can run mysql.exe to run on my pc but when it goes to a hosting company's server I don't have that command! So I tried to include(setup.sql) into a string and send that as one long sql query. BUT I need to get rid of all the comment lines first in the script!! can someone give me a better idea of doing this. (there must be an equivalent php function like mysql_run_script(setup.sql)) Either do it with a query mysql_query ( LOAD DATA INFILE 'file.sql' ); Or use a system command in php... $command = mysql db_name file.sql; exec ( $command ); -- John C. Nichel ÜberGeek KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] novice: how to run .sql script from php?
I'm assuming that the .sql file consists of a list of MySQL queries, that when performed sequentially will set up your system. That being the case, the perfered way ot install the thing is to do a 'mysql [host/username/password parameters] file.sql'. I believe you can also run file.sql scripts using phpMyAdmin. If you were to define a mysql_run_script() function, it would look a bit like the following: ?pseudo_code function mysql_run_script($file){ $sql = file_get_contents($file); $queries = split_sql_into_individual_sql_queries($sql); foreach($queries as $query){ mysql_query($query); } } ? Come to think of it, you could turn the above pseudo code into valid php code, by defining the split_sql_into_individual_sql_queries() function. This would involve splitting by ';', taking into account the possibility that ';' may occur in the middle of a string. Check out the code for phpmyadmin, or phpbb(db backup/recover feature), for a better idea. On 5/27/05, tony yau [EMAIL PROTECTED] wrote: Hi All, I got this .sql script that setup the whole db schema, I can run mysql.exe to run on my pc but when it goes to a hosting company's server I don't have that command! So I tried to include(setup.sql) into a string and send that as one long sql query. BUT I need to get rid of all the comment lines first in the script!! can someone give me a better idea of doing this. (there must be an equivalent php function like mysql_run_script(setup.sql)) thanks -- Tony Yau -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php