[英]PHP mssql_query delete, create and select from #temp table
I am trying to: 我在尝试着:
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: 简短信息:
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. 由于多种原因,这将无法正常工作。
#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
。 $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.