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.

Link to comment
Share on other sites

 

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.