我有这个SQL查询,当我在MSSQL Management Studio中执行它时运行正常,但是当我尝试在PHP中执行它时它没有输出。
有什么建议吗?
//获取ID
$atz_id = $_GET['atz_id'];
// SQL查询
$q2 = "
declare @atzID int set @atzID=$atz_id;
if object_id('tempdb..#vesture1') is not null drop table #vesture1
select distinct
atz_id,
datums s_dat,
cast(NULL as datetime) b_dat,
tips
into #vesture1
from guna..erg2_atzistas_o o
where
darbiba=1
and atz_id=@atzID
update #vesture1 set b_dat=(select top 1 datums from guna..erg2_atzistas_o where atz_id=#vesture1.atz_id and darbiba=0 and tips=#vesture1.tips and datums > #vesture1.s_dat order by datums asc)
if object_id('tempdb..#vesture2') is not null drop table #vesture2
select distinct
atz_id,
convert(varchar, s_dat, 104) s_dat,
convert(varchar, b_dat, 104) b_dat,
substring((select ', '+tips from #vesture1 where atz_id=v.atz_id and s_dat=v.s_dat and isnull(b_dat,0)=isnull(v.b_dat,0) order by tips for xml path('')), 3, 3000) 'sugas'
into #vesture2
from #vesture1 v
select * from #vesture2 order by s_dat, b_dat
";
//运行查询
$res2=mssql_query($q2);
//检查是否有一些输出(没有 - 其他查询输出0或行数,这不是)
$num_rows2 = mssql_num_rows($res2);
echo $num_rows2;
//输出数据
echo "<table border='1' class='saraksts_table' width='100%' cellspacing='0'>
<tr class='saraksts_header'>
<th><center><font size='1.2px'>info</font></center></th>
<th><center><font size='1.2px'>xxxx</font></center></th>
<th><center><font size='1.2px'>axxa</font></center></th>
<th><center><font size='1.2px'>SAAAa</font></center></th>
</tr>";
while($row2 = mssql_fetch_array($res2))
{
echo "<tr style=background-color:white;>";
echo "<td>" . $row2['atz_id'] . "</td>";
echo "<td>" . $row2['s_dat'] . "</td>";
echo "<td>" . $row2['b_dat'] . "</td>";
echo "<td>" . $row2['sugas'] . "</td>";
echo "</tr>";
}
echo "</table>";
更新 错误消息:
PHP Warning: mssql_query(): message: Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier. (severity 16) in /home/info/info_pub/sql_vesture.php on line 108
PHP Warning: mssql_query(): General SQL Server error: Check messages from the SQL Server (severity 16) in /home/info/info_pub/sql_vesture.php on line 108
PHP Warning: mssql_num_rows() expects parameter 1 to be resource, boolean given in /home/info/info_pub/sql_vesture.php on line 112
PHP Warning: mssql_fetch_array() expects parameter 1 to be resource, boolean given in /home/info/info_pub/sql_vesture.php on line 167
答案 0 :(得分:1)
我经常会将PHP MySQL查询编写为3个步骤。
定义查询
$ query =“SELECT * FROM tablename WHERE value =''”;
执行查询:
$ result = mysql_query($ query)或die(mysql_error());
验证查询结果:
if(mysql_num_rows($ result)== 0){}
或者您可以将步骤2和3结合起来,如下所示:
if (!result = mysql_query($query)) {
// looks like this didnt return any data
} else {
// woot, we have something to work with
}
大多数情况下,您看到的错误意味着您的查询无效,从而导致使用虚假数据集,而不是返回任何行。
我使用PHP中的错误报告的3个步骤将为您提供MySQL反馈错误代码以及PHP中的行,这通常可以帮助您隔离查询中出错的位置。
答案 1 :(得分:0)
尝试在/etc/freetds/freetds.conf中添加这两行
[global]
tds version = 8.0
client charset = UTF-8
答案 2 :(得分:0)
我必须转换数据类型 - &gt; http://msdn.microsoft.com/en-us/library/aa226054%28v=sql.80%29.aspx
更新选择 - 花了一段时间才弄明白
select atz_id, s_dat, b_dat, **cast(sugas as varchar)as sugas** from #vesture2 order by s_dat, b_dat;
感谢所有人的帮助。