trilbyfish Posted April 29, 2008 Share Posted April 29, 2008 Is there a way to run a multi-line mysql query (such as creating a database with about 20 fields and entering some data) from a php script? Link to comment https://forums.phpfreaks.com/topic/103440-multi-line-mysql-query-from-php/ Share on other sites More sharing options...
rhodesa Posted April 29, 2008 Share Posted April 29, 2008 I don't think it let's you do that. But just put them in separate mysql_query commands: mysql_query("CREATE TABLE ..."); mysql_query("INSERT INTO ..."); mysql_query("INSERT INTO ..."); mysql_query("INSERT INTO ..."); Link to comment https://forums.phpfreaks.com/topic/103440-multi-line-mysql-query-from-php/#findComment-529703 Share on other sites More sharing options...
SharkBait Posted April 29, 2008 Share Posted April 29, 2008 Not sure about mulli-line query, but you can have the script execute multiple single queries. IE: $str = "CREATE TABLE myTable( id INT, data VARCHAR(100) ) "; $qry = mysql_query($str); $str = "INSERT INTO myTable (id, data) VALUES ( 1, "This is fun")"; $qry = mysql_query($str); granted that's a quick n dirty version. Link to comment https://forums.phpfreaks.com/topic/103440-multi-line-mysql-query-from-php/#findComment-529705 Share on other sites More sharing options...
trilbyfish Posted April 29, 2008 Author Share Posted April 29, 2008 Not sure about mulli-line query, but you can have the script execute multiple single queries. IE: $str = "CREATE TABLE myTable( id INT, data VARCHAR(100) ) "; $qry = mysql_query($str); $str = "INSERT INTO myTable (id, data) VALUES ( 1, "This is fun")"; $qry = mysql_query($str); granted that's a quick n dirty version. Ok thanks, would that let me do 2 or coloumns? WOuld i have to have a different variable each time? Link to comment https://forums.phpfreaks.com/topic/103440-multi-line-mysql-query-from-php/#findComment-529706 Share on other sites More sharing options...
SharkBait Posted April 29, 2008 Share Posted April 29, 2008 That is for 2 columns, you can modify the create table to add as many columns as you need. So the columns of course would be `id` and `data` This might help: http://sql-info.de/mysql/examples/CREATE-TABLE-examples.html Link to comment https://forums.phpfreaks.com/topic/103440-multi-line-mysql-query-from-php/#findComment-529708 Share on other sites More sharing options...
wildteen88 Posted April 29, 2008 Share Posted April 29, 2008 You can not run multiple mysql queries in one mysql_query call. What you're better of doing is defining your queries in an array, call this $sql_queries then use a loop to loop through the array for running the queries, eg. // query for creating table. In this example a simple database for storing names/ages $sql_queries[] = 'CREATE TABLE `names` ( id INT(11) NOT NULL AUTO_INCREMENT, name VARCHAR(100) NOT NULL, age INT(2) NOT NULL )'; // list queries for populating the table $sql_queries[] = 'INSERT INTO `names` (`name`, `age`) VALUES (\'Sam\', 47)'; $sql_queries[] = 'INSERT INTO `names` (`name`, `age`) VALUES (\'Mike\', 13)'; $sql_queries[] = 'INSERT INTO `names` (`name`, `age`) VALUES (\'Sally\', 29)'; $sql_queries[] = 'INSERT INTO `names` (`name`, `age`) VALUES (\'Jo\', 21)'; // now process the queries foreach($sql_queries as $query) { echo 'Running Query: <b><pre>'.htmlentities($query, ENT_QUOTES).'</pre></b>'; mysql_query($query) or die('... FAIL!<br />Error: ' . mysql_error()); echo '... SUCCESS!<br />'; } Link to comment https://forums.phpfreaks.com/topic/103440-multi-line-mysql-query-from-php/#findComment-529710 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.