views:

185

answers:

1

Hi everyone...

While looking for a way to temporarily save the search results when a user searches for a hotel free between particular dates i came across temporary tables.

But certain questions are not answered even in mysql manual.... like...

  1. Will the temporary table be unique for each user that executes the script...? Or will it be overwritten when two different users run the script at the same time...?

  2. When will the table be destroyed..? When the user closes the browser window or just navigates away from the page in which the script runs...?

Thanks for your clarifications...

This is how i do it....

$table = "CREATE OR REPLACE TEMPORARY TABLE $free_room(
              room_id INT(5),
              room_name VARCHAR(150),
              max_persons INT(1),
              rooms_free INT(1),
              room_price INT(6),
              hotel_id INT(4),
              hotel_name VARCHAR(100),
              hotel_stars INT(1),
              hotel_type INT(1)) ENGINE=MEMORY";

    $query_getFreeRooms = "INSERT INTO  $free_room
                           SELECT $rooms.room_id,
                                  $rooms.room_name,
                                  $rooms.max_persons,
                                  $rooms.total_rooms - $rooms.booked_rooms AS rooms_free,
                                  $rooms.room_price,
                                  $hotels.hotel_id,
                                  $hotels.hotel_name,
                                  $hotels.hotel_stars,
                                  $hotels.hotel_type
                           FROM   $hotels,$rooms
                           WHERE  $rooms.room_id NOT IN (SELECT room_id
                                                         FROM   $reservations
                                                         WHERE  $dateCheck)
                           AND    $hotels.hotel_city = '$city_search1'
                           AND    $hotels.hotel_id = $rooms.hotel_id
                           AND    $hotels.hotel_deleted = '0'
                           AND    $rooms.room_deleted = '0'";
+2  A: 

Quoting the MySQL manual page of CREATE TABLE :

You can use the TEMPORARY keyword when creating a table.
A TEMPORARY table is visible only to the current connection, and is dropped automatically when the connection is closed.
This means that two different connections can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name.

Considering that two users will have two distinct connections, the table will be unique for each user.

The temporary table will be dropped when the connection that created it is closed -- in PHP, at least, it means when the script that generates the page ends (So, generally, even before the users actually reads the page)

Pascal MARTIN
But when i test it online the table created by the first user is still available to all the users accessing it after him...!!! A new table is not created for each user...
SpikETidE
So maybe you're using connection pooling in some way ?
nos
Connection pooling... being a new comer i am not aware of this concept.. i'll look it up first..
SpikETidE
Is the extract from the Manual a copyright infringement ?
Martin
It works.. had to tweak the code a li'l bit... Got it Running.. Thanks for the response, Martin... +1 and accepted answer..
SpikETidE
@Martin : I would be surprised if quoting *(I explicitly said it was a quote, and provided a link to the source)* a short extract would cause much trouble ;;; @SpikE : ok ; glad you found out a solution :-)
Pascal MARTIN