如何通过 PHP 分隔 SQL 联合上的值
我使用 mysql_fetch_array() 来执行此操作,并通过 phpMyAdmin 测试我的查询。而且,正如我认为它们都在同一行上一样,是否可以使用 mysql fetch array() 来做到这一点?
这是我的代码:
$sql="SELECT xml FROM g2s_api_logs WHERE functioncode='MQX' AND cardnumber= '$cardnumber' AND
direction='out' AND date(datetime)='$date' AND time(datetime)='$time' UNION SELECT xml FROM
g2s_api_logs WHERE functioncode='MQX' AND cardnumber= '$cardnumber' AND direction='in' AND
date(datetime)='$date' AND time(datetime)='$time' ";
$query= mysql_query($sql)or die(mysql_error().$sql);
$array=mysql_fetch_array($query,MYSQL_ASSOC);
$result= htmlspecialchars($array['?']);
$response=htmlspecialchars($array['?']);
mysql_close();
请忽略问号;我还不知道要放什么进去。我使用 phpMyAdmin 运行这个,它起作用了。不过我想在我的显示中分开他们的结果。即使他们在同一排我也可以这样做吗?有没有 fetch 列之类的东西?
I'm using mysql_fetch_array()
to do this, and testing my queries through phpMyAdmin. And, as I thought they are both on the same row, is it possible to do this using mysql fetch array()
?
Here is my code:
$sql="SELECT xml FROM g2s_api_logs WHERE functioncode='MQX' AND cardnumber= '$cardnumber' AND
direction='out' AND date(datetime)='$date' AND time(datetime)='$time' UNION SELECT xml FROM
g2s_api_logs WHERE functioncode='MQX' AND cardnumber= '$cardnumber' AND direction='in' AND
date(datetime)='$date' AND time(datetime)='$time' ";
$query= mysql_query($sql)or die(mysql_error().$sql);
$array=mysql_fetch_array($query,MYSQL_ASSOC);
$result= htmlspecialchars($array['?']);
$response=htmlspecialchars($array['?']);
mysql_close();
Please ignore the question mark; I dunno what to put into it yet. I run this one using phpMyAdmin and it worked. However I would like to separate their results in my display. Can I do that even if they are in the same row? Is there such a thing as fetch column?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有这样的查询:
并且您想知道特定行是否来自 X 或 Y,那么您可以向 SELECT 子句添加文字值来区分它们:
只要列名/别名和类型与UNION 会对这种事情感到高兴。
If you have a query like this:
and you want to know if a particular row came from X or Y, then you can add a literal value to the SELECT clause to differentiate them:
as long as the column names/aliases and types match the UNION will be happy with this sort of thing.