Joined: 07 Mar 2004 Posts: 1048
Location: Dhaka, Bangladesh
Post subject: Send multiple sql query in a while loop
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.
Joined: 21 Jun 2004 Posts: 85
Location: Dhaka,Bangladesh
Post subject:
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
quantum Site Admin
Joined: 07 Mar 2004 Posts: 1048
Location: Dhaka, Bangladesh
Post subject: Making Multiple sql query
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)
);
Last edited by quantum on Sun Aug 22, 04 8:17 am; edited 1 time in total
Sun Aug 22, 04 4:46 am
tanveer User
Joined: 21 Jun 2004 Posts: 85
Location: Dhaka,Bangladesh
Post subject:
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.