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...
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...?
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'";
Temporary tables only exist within the session in which they were created and persist only for the remainder of the session. As such, they are not visible to other users or sessions.
A TEMPORARY table is visible only within the current session, and is dropped automatically when the session is closed. This means that two different sessions can use the same temporary table name without conflicting with each other or with an existing non- TEMPORARY table of the same name.
Advantages of Temporary Tables You can create a temporary table and insert, delete and update its records without worrying about whether you have sufficient rights to change data in permanent tables, or whether you might be accidentally doing so.
Global temp tables can be referenced from the same connection or a different connection so long as the global temp table has not gone out of scope. The database connection is the same when a temp table is created and referenced from a script within the same SSMS tab.
Quoting the MySQL manual page of CREATE TABLE
:
You can use the
TEMPORARY
keyword when creating a table.
ATEMPORARY
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With