PHP&;MySQL查询结果令人困惑
正在编写一个快速脚本,以在我的数据库中显示客户最近的活动-我有脚本输出结果,但它输出的内容让我感到困惑PHP&;MySQL查询结果令人困惑,php,mysql,sql,Php,Mysql,Sql,正在编写一个快速脚本,以在我的数据库中显示客户最近的活动-我有脚本输出结果,但它输出的内容让我感到困惑 <?php //Search for customer recent history $q = "SELECT * FROM txn_log WHERE customer_no = $customer_no ORDER BY datetime DESC LIMIT 3";
<?php
//Search for customer recent history
$q = "SELECT * FROM txn_log WHERE customer_no = $customer_no ORDER BY datetime DESC LIMIT 3";
$r = mysql_query($q) or die(mysql_error());
while($row = mySQL_fetch_array($r)) {
$recent_history = '';
$str .= '<a href="#" class="list-group-item">';
$str .= ' <span class="badge">' . gmdate("Y-m-d\TH:i:s\Z", $row['datetime']) . '</span>';
$str .= ' <i class="fa fa-check"></i> ' . $row['txn_id'] . ': ' . $row['txn_type'] . ' ' . $row['amount_dif'];
$str .= '</a>';
echo $str;
}
?>
在我的数据库中,我有一个测试客户,从ID 2开始,有三条记录与之关联
我上面的查询应该只按照所用UNIX时间戳的顺序输出三条记录,
它应该按照以下顺序生成三条记录=>ID2、ID3、ID4
它所做的是输出以下=>ID2,ID2,ID3,ID2,ID3,ID4
我不明白我做错了什么,让它除了产生正确的结果外,还产生了前三个(ID2,ID2,ID3)。每次运行查询时,结果都是相同的。您使用的是$str=
那么
在循环的第一次迭代中,要回显的$str是ID2
第二次迭代,$str被回显为ID2+ID3
第三次迭代,$str被回显为ID2+ID3+ID4
最后,它将看起来像ID2、ID2、ID3、ID2、ID3、ID4
希望有帮助
要解决此问题,请尝试在循环开始时将变量声明为$str=“”,然后您的echo将只回显每个ID一次。您使用的是$str=
那么
在循环的第一次迭代中,要回显的$str是ID2
第二次迭代,$str被回显为ID2+ID3
第三次迭代,$str被回显为ID2+ID3+ID4
最后,它将看起来像ID2、ID2、ID3、ID2、ID3、ID4
希望有帮助
要解决此问题,请尝试在循环开始时将变量声明为$str=“”,然后您的echo将只回显每个ID一次。您使用的是$str=
那么
在循环的第一次迭代中,要回显的$str是ID2
第二次迭代,$str被回显为ID2+ID3
第三次迭代,$str被回显为ID2+ID3+ID4
最后,它将看起来像ID2、ID2、ID3、ID2、ID3、ID4
希望有帮助
要解决此问题,请尝试在循环开始时将变量声明为$str=“”,然后您的echo将只回显每个ID一次。您使用的是$str=
那么
在循环的第一次迭代中,要回显的$str是ID2
第二次迭代,$str被回显为ID2+ID3
第三次迭代,$str被回显为ID2+ID3+ID4
最后,它将看起来像ID2、ID2、ID3、ID2、ID3、ID4
希望有帮助
要解决此问题,请尝试在循环开始时将变量声明为$str=“”,然后您的echo将只回显每个ID一次。为每个循环迭代重置$str。 加:
在<代码>$最近的_历史=''之后代码>为每个循环迭代重置$str。 加:
在<代码>$最近的_历史=''之后代码>为每个循环迭代重置$str。 加:
在<代码>$最近的_历史=''之后代码>为每个循环迭代重置$str。 加:
在<代码>$最近的_历史=''之后代码>您需要使用mysql\u fetch\u assoc,并在循环外进行回显 没什么要记住的 安全性必须是首要原则,检查SQL注入字符,并始终在查询中用单引号括住变量。见下文
<?php
//Search for customer recent history
$customer_no = str_replace("'", '', $customer_no);
$q = "SELECT * FROM txn_log WHERE customer_no = '$customer_no' ORDER BY datetime DESC LIMIT 3";
$r = mysql_query($q) or die(mysql_error());
while($row = mySQL_fetch_assoc($r)) {
$recent_history = '';
$str .= '<a href="#" class="list-group-item">';
$str .= ' <span class="badge">' . gmdate("Y-m-d\TH:i:s\Z", $row['datetime']) . '</span>';
$str .= ' <i class="fa fa-check"></i> ' . $row['txn_id'] . ': ' . $row['txn_type'] . ' ' . $row['amount_dif'];
$str .= '</a>';
}
echo $str;
?>
您需要使用mysql\u fetch\u assoc,并在循环外回送 没什么要记住的 安全性必须是首要原则,检查SQL注入字符,并始终在查询中用单引号括住变量。见下文
<?php
//Search for customer recent history
$customer_no = str_replace("'", '', $customer_no);
$q = "SELECT * FROM txn_log WHERE customer_no = '$customer_no' ORDER BY datetime DESC LIMIT 3";
$r = mysql_query($q) or die(mysql_error());
while($row = mySQL_fetch_assoc($r)) {
$recent_history = '';
$str .= '<a href="#" class="list-group-item">';
$str .= ' <span class="badge">' . gmdate("Y-m-d\TH:i:s\Z", $row['datetime']) . '</span>';
$str .= ' <i class="fa fa-check"></i> ' . $row['txn_id'] . ': ' . $row['txn_type'] . ' ' . $row['amount_dif'];
$str .= '</a>';
}
echo $str;
?>
您需要使用mysql\u fetch\u assoc,并在循环外回送 没什么要记住的 安全性必须是首要原则,检查SQL注入字符,并始终在查询中用单引号括住变量。见下文
<?php
//Search for customer recent history
$customer_no = str_replace("'", '', $customer_no);
$q = "SELECT * FROM txn_log WHERE customer_no = '$customer_no' ORDER BY datetime DESC LIMIT 3";
$r = mysql_query($q) or die(mysql_error());
while($row = mySQL_fetch_assoc($r)) {
$recent_history = '';
$str .= '<a href="#" class="list-group-item">';
$str .= ' <span class="badge">' . gmdate("Y-m-d\TH:i:s\Z", $row['datetime']) . '</span>';
$str .= ' <i class="fa fa-check"></i> ' . $row['txn_id'] . ': ' . $row['txn_type'] . ' ' . $row['amount_dif'];
$str .= '</a>';
}
echo $str;
?>
您需要使用mysql\u fetch\u assoc,并在循环外回送 没什么要记住的 安全性必须是首要原则,检查SQL注入字符,并始终在查询中用单引号括住变量。见下文
<?php
//Search for customer recent history
$customer_no = str_replace("'", '', $customer_no);
$q = "SELECT * FROM txn_log WHERE customer_no = '$customer_no' ORDER BY datetime DESC LIMIT 3";
$r = mysql_query($q) or die(mysql_error());
while($row = mySQL_fetch_assoc($r)) {
$recent_history = '';
$str .= '<a href="#" class="list-group-item">';
$str .= ' <span class="badge">' . gmdate("Y-m-d\TH:i:s\Z", $row['datetime']) . '</span>';
$str .= ' <i class="fa fa-check"></i> ' . $row['txn_id'] . ': ' . $row['txn_type'] . ' ' . $row['amount_dif'];
$str .= '</a>';
}
echo $str;
?>
Woops。。。找到了
在我的时间里
我拨错了弦!
$recent_history=''
应该是$str=''代码>
编辑:
谢谢大家,在我提交更正之前,我没有看到你们发帖。
我肯定会保护脚本,我只是确保我能做我想让它做的事:)Woops。。。找到了
在我的时间里
我拨错了弦!
$recent_history=''
应该是$str=''代码>
编辑:
谢谢大家,在我提交更正之前,我没有看到你们发帖。
我肯定会保护脚本,我只是确保我能做我想让它做的事:)Woops。。。找到了
在我的时间里
我拨错了弦!
$recent_history=''
应该是$str=''代码>
编辑:
谢谢大家,在我提交更正之前,我没有看到你们发帖。
我肯定会保护脚本,我只是确保我能做我想让它做的事:)Woops。。。找到了
在我的时间里
我拨错了弦!
$recent_history=''
应该是$str=''代码>
编辑:
谢谢大家,在我提交更正之前,我没有看到你们发帖。
我肯定会保护脚本,我只是确保我可以做我想让它做的事情:)