简体   繁体   English

PHP mssql_query删除,创建并从#temp表中选择

[英]PHP mssql_query delete, create and select from #temp table

I am trying to: 我在尝试着:

  1. Delete a temp table if it exists. 删除临时表(如果存在)。
  2. Create a temp table with a selected data. 用选定的数据创建一个临时表。
  3. Select from the temp table. 从临时表中选择。

I am getting "Parse error: parse error in C:\\xampp\\htdocs\\test.php on line 57. 我在第57行收到“解析错误: C:\\xampp\\htdocs\\test.php解析错误。

Any help will be greatly appreciated! 任何帮助将不胜感激!

Short Info: 简短信息:

  • PHP Version 5.2.9 PHP版本5.2.9
  • MS SQL 2008 R2 MS SQL 2008 R2
  • The queries executed in MS SQL work as expected 在MS SQL中执行的查询按预期工作

The PHP code below: 下面的PHP代码:

$query = "IF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #temp"
$result = mssql_query($query);

$query = "
SELECT *
INTO #temp
FROM (SELECT ROW_NUMBER() OVER (PARTITION BY key
      ORDER BY moment desc) AS Seq, *
FROM  reportstable) t
WHERE Seq = 1
and moment between '$_GET[od] $time' and '$_GET[do] $time1'"
$result = mssql_query($query);

$query = "select * from #temp"
$result = mssql_query($query);

Thanks in advance! 提前致谢!

This will not work as written, for a couple reasons. 由于多种原因,这将无法正常工作。

  1. Temp tables as you're using them here are local only to the current session. 在此处使用它们的临时表仅在当前会话本地。 In other words, #temp is disappearing after each of your calls to mssql_query() . 换句话说,在每次调用mssql_query()之后, #temp mssql_query()都会消失。 If you need a temp table to persist across multiple contexts, it has to be a global temp table - ##temp . 如果您需要一个临时表在多个上下文中持久保存,则它必须是全局临时表- ##temp
  2. Your code is wide open to SQL injection - you're using unsanitized input from the users. 您的代码是SQL注入敞开的 -你正在使用从用户unsanitized输入。 This is a massive security vulnerability that has existed for 2 decades and you're persisting it. 这是一个巨大的安全漏洞,已经存在了20年,并且您一直坚持下去。 Read up on PDO and parameterized queries/prepared statements. 阅读PDO和参数化查询/准备好的语句。
  3. Your query is probably malformed. 您的查询可能格式错误。 Output the actual $query after constructing it and make sure that your datetime is properly formatted. 构造后输出实际的 $query ,并确保您的日期时间格式正确。

All that said, and assuming you fix your SQL injection vulnerability, the use of a temp table here is pointless and wasting resources. 综上所述,假设您已修复SQL注入漏洞,则在此处使用临时表毫无意义且浪费资源。 The following would be sufficient: 以下内容就足够了:

$query = "
SELECT *
FROM (SELECT ROW_NUMBER() OVER (PARTITION BY key
      ORDER BY moment desc) AS Seq, *
FROM  reportstable) t
WHERE Seq = 1
and moment between '$_GET[od] $time' and '$_GET[do] $time1'"
$result = mssql_query($query);

A working solution based on alroc suggestions: 基于alroc建议的可行解决方案:

The parse error was thrown due to missing semi-colon. 由于缺少分号,引发了分析错误。 And ## must be present at all times in order to be able to query the table. 并且##必须始终存在,以便能够查询该表。

// to delete the temp table
$query = "IF OBJECT_ID('tempdb..##temp') IS NOT NULL DROP TABLE ##temp";
$result = mssql_query($query);

// to initialize the temp table
$query = "
SELECT *
INTO ##temp
FROM (SELECT ROW_NUMBER() OVER (PARTITION BY test_key
      ORDER BY test_moment desc) AS Seq, *
FROM  reports_table) t
WHERE Seq = 1
and test_moment between '$_GET[od] $time' and '$_GET[do] $time1'";
$result = mssql_query($query);

// to query the temp table
select test_key
from ##temp
where x = 0
and y = 1
and z = 3

ps The above works in the same session only. ps以上内容仅在同一会话中起作用。 If u call the same page in the same time from different sources, one will throw an error because of trying to drop the temp table. 如果您从不同的来源在同一时间调用同一页面,则由于尝试删除临时表而将引发错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM