嘿,伙计们,我在找一个小问题的解决方案,PHP似乎运行正常,我看不到SQL查询的任何问题,有人能看到我的代码有什么明显的错误吗?我真的被困在这里了。
“项”行的内容类似于以下内容:
30->0,31->0,32->0,36->0,33->10,29->0,35->0,6->0,5->0,8->0,9->0,7->0,14->0,15->0,10->0,17->0
我只需要它们爆炸成一个数组,并在数组中预测值,用它们的“键”打印在页面上。我看过这里:explode() into $key=>$value pair,但没有得出结论--这是完整的代码,如果有人能帮助我,告诉我我在做什么,或者甚至指出正确的方向,我会很高兴的。
$id = $_GET['order'];
if(!is_number($id)){
exit();
}
$sql = "SELECT * FROM ORDERS WHERE id='$id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$dataString = $row["items"];
foreach (explode(",", $dataString) as $cLine) {
list ($cKey, $cValue) = explode('->', $cLine, 2);
$itemarray[$cKey] = $cValue;
}
foreach($itemarray as $key => $value) {
echo "<br/>".$value." x ".$key;
}
}
} else {
echo "0 results";
}
谢谢。
发布于 2015-09-02 15:52:26
原来我的代码和查询都很完美。问题就在这里:
if(!is_number($id)){
exit();
}
不是我的?order=55被识别为数字,就是误用了is_number函数。
谢谢你们的帮助。
发布于 2015-09-02 15:17:01
我还不能对此发表评论,所以我正在发帖答复。以下代码似乎运行良好:
$dataString = "30->0,31->0,32->0,36->0,33->10,29->0,35->0,6->0,5->0,8->0,9->0,7->0,14->0,15->0,10->0,17->0";
foreach (explode(",", $dataString) as $cLine) {
list ($cKey, $cValue) = explode('->', $cLine, 2);
$itemarray[$cKey] = $cValue;
foreach($itemarray as $key => $value) {
echo "<br/>".$value." x ".$key;
}
}
因此,我最好的选择是查询返回的数据不像预期的那样。
发布于 2015-09-02 15:27:30
因为当你得到数据的时候,这应该更容易:
parse_str(str_replace(array('->',','), array('=','&'), $row['items'), $itemarray);
print_r($itemarray);
https://stackoverflow.com/questions/32356793
复制相似问题