Home

Forums

Web development

 

 

 

 
     
 
dna88 Web development and Technology Forum
 
Profile   Register   Memberlist   Usergroups   FAQ   Search  Log in
Send multiple sql query in a while loop

 
Post new topic   Reply to topic    dna88 Forum Index -> Databases Discussion Forum
Author Message
quantum
Site Admin
Site Admin


Joined: 07 Mar 2004
Posts: 1048
Location: Dhaka, Bangladesh

Post Post subject: Send multiple sql query in a while loop Reply with quote

Hi everyone,

I was wondering if anyone could show me with example how to send multiple sql query to a mysql database? I want to use a while loop for that. And I also would like to have an example showing the sql query in conjunction with, if possible, php syntax.

Thanks.
_________________

Dust fills my eyes / Clouds roll by / and I roll with them / Centuries cry / Orders fly / and I fall again
Afford best design, implement best solution. Outsource your web design.
Fri Aug 20, 04 9:58 am
Back to top
quantum View user's profile Send private message Visit poster's website AIM Address
tanveer
User
User


Joined: 21 Jun 2004
Posts: 85
Location: Dhaka,Bangladesh

Post Post subject: Reply with quote

Hi Qunatum,
Correct me If I am wrong.
Are u looking for something like this:-

Code:

while (condition)
{
    $user_query="delete from main where call_number='$call'";
    $user_result=mysql_query($user_query);

    $sql_avail="delete from book_lending where call_number='$call'";
    $sql_query=mysql_query($sql_avail);

    $sql_order="delete from book_order where call_number='$call'";
    $sql_order=mysql_query($sql_order);
}
Fri Aug 20, 04 12:50 pm
Back to top
tanveer View user's profile Send private message
quantum
Site Admin
Site Admin


Joined: 07 Mar 2004
Posts: 1048
Location: Dhaka, Bangladesh

Post Post subject: Making Multiple sql query Reply with quote

Thanks Tanvir. Yes, it was something like this I was looking for. As I see that the crucial point here is how to make connection to the mysql and fetch the resultset to call. In such cases we CAN make multiple sql queries with a while loop IF everytime the resultset is with a different variable name. Something like this,

Code:
$sql = "SELECT * FROM TABLE";
$result = mysql_query($sql,$db_link);
while ($row = mysql_fetch_array($result){
          $sql = "SELECT * FROM TABLE2 WHERE TABLE2_ID = '" . $row[0] . "'";
           // code here
          $result2 = mysql_query($sql,$db_link);
          while ($row2 = mysql_fetch_array($result2){
                   //code here
          }
}


However, I have found a more convenient way to do what the while loop is doing here. Joining tables. Cool stuff. Like this,

Allow SELECT a FROM table_name1 LEFT JOIN table_name2 USING (a); in this case a is assumed to come from the table_name1 table.

This example I picked up from the mysql manual:

Code:
CREATE TABLE person (
    id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
    name CHAR(60) NOT NULL,
    PRIMARY KEY (id)
);

CREATE TABLE shirt (
    id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
    style ENUM('t-shirt', 'polo', 'dress') NOT NULL,
    color ENUM('red', 'blue', 'orange', 'white', 'black') NOT NULL,
    owner SMALLINT UNSIGNED NOT NULL REFERENCES person(id),
    PRIMARY KEY (id)
);

INSERT INTO person VALUES (NULL, 'Antonio Paz');

INSERT INTO shirt VALUES
(NULL, 'polo', 'blue', LAST_INSERT_ID()),
(NULL, 'dress', 'white', LAST_INSERT_ID()),
(NULL, 't-shirt', 'blue', LAST_INSERT_ID());

INSERT INTO person VALUES (NULL, 'Lilliana Angelovska');

INSERT INTO shirt VALUES
(NULL, 'dress', 'orange', LAST_INSERT_ID()),
(NULL, 'polo', 'red', LAST_INSERT_ID()),
(NULL, 'dress', 'blue', LAST_INSERT_ID()),
(NULL, 't-shirt', 'white', LAST_INSERT_ID());

SELECT * FROM person;
+----+---------------------+
| id | name                |
+----+---------------------+
|  1 | Antonio Paz         |
|  2 | Lilliana Angelovska |
+----+---------------------+

SELECT * FROM shirt;
+----+---------+--------+-------+
| id | style   | color  | owner |
+----+---------+--------+-------+
|  1 | polo    | blue   |     1 |
|  2 | dress   | white  |     1 |
|  3 | t-shirt | blue   |     1 |
|  4 | dress   | orange |     2 |
|  5 | polo    | red    |     2 |
|  6 | dress   | blue   |     2 |
|  7 | t-shirt | white  |     2 |
+----+---------+--------+-------+

SELECT s.* FROM person p, shirt s
 WHERE p.name LIKE 'Lilliana%'
   AND s.owner = p.id
   AND s.color <> 'white';

+----+-------+--------+-------+
| id | style | color  | owner |
+----+-------+--------+-------+
|  4 | dress | orange |     2 |
|  5 | polo  | red    |     2 |
|  6 | dress | blue   |     2 |
+----+-------+--------+-------+

_________________

Dust fills my eyes / Clouds roll by / and I roll with them / Centuries cry / Orders fly / and I fall again
Afford best design, implement best solution. Outsource your web design.


Last edited by quantum on Sun Aug 22, 04 8:17 am; edited 1 time in total
Sun Aug 22, 04 4:46 am
Back to top
quantum View user's profile Send private message Visit poster's website AIM Address
tanveer
User
User


Joined: 21 Jun 2004
Posts: 85
Location: Dhaka,Bangladesh

Post Post subject: Reply with quote

hey, thanks goes to u bcz thatz a very useful piece of code indeed. I always overlooked these join portion and the code I post is one of my projects which I will now try to implement with this join. Gentle smile
Sun Aug 22, 04 8:04 am
Back to top
tanveer View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    dna88 Forum Index -> Databases Discussion Forum All times are GMT - 7 Hours
Page 1 of 1

 

Partners and Resources

Bangladesh hosting company

Bangladesh web design

Driven by phpBB © phpBB Group