Jump to content

multi-line mysql query from php


trilbyfish

Recommended Posts

 

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.

 

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?

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 />';
}

 

 

 

 

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.