Make a temporary table and select it

I get the error "uneclared 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?

+9
source share
3 answers

Please do not use the mysql_* functions in the new code . They are no longer supported and are officially outdated . See the red box ? Instead, learn about ready-made statements and use PDO or MySQLi - this article will help you decide which ones. If you choose PDO, here is a good lesson .


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 assume that you are going to do more things with a temporary table or just play with it, but if you don’t know that all the code can be summed 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()); 
+12
source

I like to use heredoc to help me build an inline sql query (just to help make any subtle mistake); so your first request will look something like this:

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

Then, if you want to correct the second query without specifying the fields of the table into which you want to insert records, you must specify the fields in the same order .

This time just a request:

 INSERT INTO temp SELECT id, live, ship, description, duration FROM cruises WHERE live = 'y'; 

And the last thing about the temporary variable is: Check the part about its visibility. You can use the TEMPORARY keyword when creating a table. The TEMPORARY table is visible only for the current connection and is automatically discarded when the connection is closed. http://dev.mysql.com/doc/refman/5.5/en/create-table.html

What does this mean: when you connect directly to MySQL, for example, through the command line interface, for example:

 mysql> #our query here line-by-line 

Then you essentially use the same connection through all of your multiple requests while your session is active.

But in an external script (for example, PHP), just because it in the same script file does not necessarily mean the same connection, so by the time your request for insertion is executed your temp table does not appear in the connection session.

Try to fulfill all the requests, send all this as part of the execution of a single command / request.

Good luck.

+1
source

The second request is invalid.

From the reference -

MySQL Server does not support SELECT ... INTO TABLE Sybase SQL extension. Instead, MySQL Server supports INSERT INTO ... SELECT . standard SQL syntax, which is basically the same thing.

Try instead -

 INSERT INTO temp SELECT live , id AS itineraryId , ship AS shipCode , description AS description , duration AS length FROM cruises WHERE live = 'Y' 
0
source

Source: https://habr.com/ru/post/914312/


All Articles