Bluehost.com Web Hosting $6.95

Multiple SQL commands with one mysql_query?

This is a discussion on Multiple SQL commands with one mysql_query? within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi, Is it possible to issue a bunch of SQL commands with one mysql_query? TIA...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2006
 
Posts: n/a
Default Multiple SQL commands with one mysql_query?

Hi,
Is it possible to issue a bunch of SQL commands with one mysql_query?

TIA


Reply With Quote
  #2 (permalink)  
Old 02-28-2006
sapo
 
Posts: n/a
Default Re: Multiple SQL commands with one mysql_query?

You can try setting up a transaction:
http://www.devarticles.com/c/a/MySQL...QL-4.0-and-PHP

But its not with just one mysql_query, you could loop doing as much
queries as you like, and in the end of the loop commit them.

something like:
mysql_query("BEGIN");
for($i=0;$i<10;$i++) {
mysql_query("INSERT INTO TABLE (id, value) VALUES ($i, '$i')");
}
mysql_query("COMMIT");

Reply With Quote
  #3 (permalink)  
Old 02-28-2006
Oli Filth
 
Posts: n/a
Default Re: Multiple SQL commands with one mysql_query?

no@email.com said the following on 27/02/2006 22:02:
> Hi,
> Is it possible to issue a bunch of SQL commands with one mysql_query?
>


No (unless you execute a stored procedure).

The solution is to use the mysqli functions instead. See the manual.


--
Oli
Reply With Quote
  #4 (permalink)  
Old 02-28-2006
BearItAll
 
Posts: n/a
Default Re: Multiple SQL commands with one mysql_query?

no@email.com wrote:

> Hi,
> Is it possible to issue a bunch of SQL commands with one mysql_query?
>
> TIA


Yes.

You could for example put as many sql commands as you like into a text file
and pass the file as a commandline argument to mysql. Or, you can use the
'source' command in mysql to read in the file and execute the queries found
there.

Ex
If you include the sql command

USE databasename;

In your query then you can use something like this,

mysql -u username -p < text_file

Otherwise you give the name of the database on the commandline.

You can also combine the two by giving the mysql internal command on the
commandline,

mysql -e 'source text_file;'

In either case the commands are in serial fashion, i.e. each is fully
complete before the next begins.

What about the output from a command? Well you can catch that too, you might
want it for a script or to display, it is in the standard sql output format
(everything is a table) except that the graphic lines are not included in
the output.

mysql < text_file > mysql.out

Might give,

names
Fred
Bob

Instead of the normal,
-------
|names|
-------
|Fred |
|Bob |

So for example a single file could create a table if it doesn't already
exist, then fill it with some data and run a query on it. Once you are used
to it it can be a quick way to pick out data from a comma seperated file,
one of those things you want just once in a while rather than a permanent
table.

It is important to remember though that processing of a file will normally
stop on the first error. It can be forced, but presumably the commands in a
single file would be in some way related, so forcing mysql to continue is
much more a debugging aid than a general purpose command.

Some examples of sql scripting are given in the MySQL manual (well worth
downloading the pdf version so that you have it handy). But in the mean
time here is an example,

#sql_example_script.sql
#use,
#mysql -u username -p < sql_example_script.sql

CREATE DATABASE IF NOT EXISTS addressbook;

CREATE TABLE addressbook.names (
id int,
name varchar( 15)
);
CREATE TABLE addressbook.birthdays (
id int,
birthday date
);

INSERT into addressbook.names ( fields ) VALUE ( data);


Reply With Quote
  #5 (permalink)  
Old 02-28-2006
Oli Filth
 
Posts: n/a
Default Re: Multiple SQL commands with one mysql_query?

BearItAll said the following on 28/02/2006 09:15:
> no@email.com wrote:
>
>> Hi,
>> Is it possible to issue a bunch of SQL commands with one mysql_query?
>>


>
> Yes.
>

<...SNIP STUFF ABOUT USE OF COMMAND LINE...>


Umm, no... none of this is using mysql_query().


--
Oli
Reply With Quote
  #6 (permalink)  
Old 02-28-2006
BearItAll
 
Posts: n/a
Default Re: Multiple SQL commands with one mysql_query?

Oli Filth wrote:

> BearItAll said the following on 28/02/2006 09:15:
>> no@email.com wrote:
>>
>>> Hi,
>>> Is it possible to issue a bunch of SQL commands with one mysql_query?
>>>

>
>>
>> Yes.
>>

> <...SNIP STUFF ABOUT USE OF COMMAND LINE...>
>
>
> Umm, no... none of this is using mysql_query().
>
>


oops, he only wrote one line too, I must have read as far as 'Is it possible
to issue a bunch of SQL commands'.

They is only one solution to this, posters must write questions of ten words
or less, would someone add that to the news group faq please.

Reply With Quote
  #7 (permalink)  
Old 02-28-2006
Drakazz
 
Posts: n/a
Default Re: Multiple SQL commands with one mysql_query?

Maybe:
<?php
$sql="SELECT * FROM a;
Select * FROM b;
select * from c;";
mysql_query($sql,$connection);
?>
??

Reply With Quote
  #8 (permalink)  
Old 02-28-2006
David Haynes
 
Posts: n/a
Default Re: Multiple SQL commands with one mysql_query?

Drakazz wrote:
> Maybe:
> <?php
> $sql="SELECT * FROM a;
> Select * FROM b;
> select * from c;";
> mysql_query($sql,$connection);
> ?>
> ??
>

mysqli has the mysqli_multi_query() for times like this.

-david-

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 03:36 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0