【发布时间】:2013-01-24 15:24:23
【问题描述】:
我在编辑代码以使用 PHP foreach 函数时遇到一些问题。我下面的“transactiondata” div 用于从我的表中提取交易信息。我想使用 foreach() 为表中的每一行创建一个新的 div 部分。但是,在编辑我的 div 以包含 foreach() 时,我抛出了大量错误,所以我回到了第 1 格。完成此任务的最佳方法是什么?
<?php
include('cn.php');
session_start();
$userUsername = $_SESSION['loggedInUser'];
$sql = "SELECT * FROM transactions WHERE
UserID = '" . $userUsername . "'";
$result = mysql_query($sql, $cn) or
die(mysql_error($cn));
$row = mysql_fetch_assoc($result);
$RequestBody = $row['RequestBody'];
$ResponseBody = $row['ResponseBody'];
parse_str($RequestBody);
parse_str($ResponseBody);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo $userUsername; ?>'s Profile - Test 2013</title>
<style>
.userstatustext
{
position: absolute;
top: 0px;
right: 5px;
}
.transactiondata
{
position: absolute;
padding: 5px;
top: 170px;
left: 75px;
width: 900px;
height: 400px;
overflow-x: hidden;
background: #B2B2B2;
border: 1px solid black;
word-break: break-all;
word-wrap: break-word;
}
</style>
</head>
<body>
<table border="0" cellpadding="10">
<tr>
<td>
<img src="../images/api_rat.png">
</td>
<td>
<h1>Transactions - Test 2013</h1>
</td>
</tr>
</table>
<div class="userstatustext">
<h5>Welcome, <?php echo $userUsername; ?>!</h5>
<h5><a href="logout.php">[LOGOUT]</a></h5>
</div>
<h3>Your most recent transactions:</h3>
<div class="transactiondata">
<p><h4>Timestamp: </h4><?php echo date("F j, Y g:i a", strtotime($TIMESTAMP)); ?></p>
<p><h4>Payment Status: </h4><?php echo $ACK; ?></p>
<?php
if(isset($CORRELATIONID))
{
echo "<p><h4>Correlation ID: </h4></p>";
echo $CORRELATIONID;
}
?>
<?php
if(isset($TRANSACTIONID))
{
echo "<p><h4>Transaction ID: </h4></p>";
echo $TRANSACTIONID;
}
?>
<p><h4>Errors Returned (if any): </h4>
<a href="https://www.testsite.com/test/search.php?query=<?php echo $ERRORCODE; ?>&search=Search">
<?php echo $ERRORCODE; ?>
</a>
</p>
<p><h4>Request Body: </h4><?php echo $RequestBody; ?></p>
<p><h4>Response Body: </h4><?php echo $ResponseBody; ?></p>
</div>
</body>
</html>
【问题讨论】:
-
什么foreach?您的代码中没有...
-
你遇到了什么错误?
-
@MarcB 可能不是单个调用
$row = mysql_fetch_assoc($result);他想在foreach 中调用它,或者在... -
首先,停止使用mysql扩展,开始使用
mysqli或PDO。 -
在经历了所有错误之后,我将 foreach() 拉出。主要是缺少的错误,或;尽管一切似乎都井然有序。我能问一下为什么 mysqli 或 PDO 是更好的选择吗?我将不得不深入研究 mysqli。