Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make a temporary table and select from it

Tags:

php

mysql

I'm getting an error 'undeclared variable: temp' when I run this...

<?php 

$maketemp = "CREATE TEMPORARY TABLE temp(`itineraryId` int NOT NULL, `live` varchar(1), `shipCode` varchar(10), `description` text, `duration` varchar(10), PRIMARY KEY(itineraryId))"; 

mysql_query( $maketemp, $connection ) or die ( "Sql error : " . mysql_error ( ) );

$inserttemp = "SELECT live, id AS itineraryId, ship AS shipCode, description AS description, duration AS length FROM cruises WHERE live ='Y' INTO temp";

mysql_query( $inserttemp, $connection ) or die ( "Sql error : " . mysql_error ( ) );

$select = "SELECT intineraryId, shipCode, description, duration FROM temp";

$export = mysql_query ( $select, $connection ) or die ( "Sql error : " . mysql_error( ) );

Any ideas ?

like image 411
Rich Avatar asked Apr 27 '12 09:04

Rich


People also ask

How do I SELECT a temporary table in MySQL?

The general syntax would be like this: INSERT INTO temporary_tabel_name SELECT * FROM existing table_name; Following the general syntax, we will copy the data from the existing table, named, Guys into the newly created temporary table, named, “temporary_data”.

How do I make a temporary table?

To create a Global Temporary Table, add the “##” symbol before the table name. Global Temporary Tables are visible to all connections and Dropped when the last connection referencing the table is closed. Global Table Name must have an Unique Table Name.

Does SELECT into create a temporary table?

After creating the table, we can insert data into it as the persisted tables. At the same time, we can create a temporary table using the SQL SELECT INTO statement command.


1 Answers

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.


This code should work:

<?php

  $maketemp = "
    CREATE TEMPORARY TABLE temp_table_1 (
      `itineraryId` int NOT NULL,
      `live` varchar(1),
      `shipCode` varchar(10),
      `description` text,
      `duration` varchar(10),
      PRIMARY KEY(itineraryId)
    )
  "; 

  mysql_query($maketemp, $connection) or die ("Sql error : ".mysql_error());

  $inserttemp = "
    INSERT INTO temp_table_1
      (`itineraryId`, `live`, `shipCode`, `description`, `duration`)
    SELECT `id`, `live`, `ship`, `description`, `duration`
    FROM `cruises`
    WHERE `live` = 'Y'
  ";

  mysql_query($inserttemp, $connection) or die ("Sql error : ".mysql_error());

  $select = "
    SELECT `itineraryId`, `shipCode`, `description`, `duration`
    FROM temp_table_1
  ";
  $export = mysql_query($select, $connection) or die ("Sql error : ".mysql_error());

I guess you are going to do more stuff with the temporary table, or are just playing with it, but if not be aware that the whole code could be summed up with:

<?php

  $query = "
    SELECT `id` AS 'itineraryId', `ship`, `description`, `duration`
    FROM `cruises`
    WHERE `live` = 'Y'
  ";
  $export = mysql_query($query, $connection) or die ("Sql error : ".mysql_error());
like image 172
DaveRandom Avatar answered Oct 24 '22 16:10

DaveRandom