制作一个临时表并从中选择 [英] make a temporary table and select from it

查看:91
本文介绍了制作一个临时表并从中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行此程序时出现错误未声明的变量:temp" ...

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( ) );

有什么想法吗?

推荐答案

请不要在新代码 .它们已不再维护并已正式弃用.看到 红色框 ?了解有关 准备好的语句 的信息,并使用 MySQLi -本文将帮助您确定哪一个.如果您选择PDO,这里是一个很好的教程.

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.

此代码应正常工作:

<?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());

这篇关于制作一个临时表并从中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆