从现在折旧的mysql代码转换到PDO。此代码应该输出表中的所有值。这是代码:
$stmt = $pdo->prepare('SELECT * FROM admin WHERE user_id = :user_id');
$stmt->bindParam(':user_id', $userid);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
并且调用字段的代码是:
$data_t .= '<td>' . $row['date'] . '</td>';
$data_t .= '<td>' . $row['length'] . '' . $selected . '</td>';
$data_t .= '<td>' . $row['ground'] . '' . $selected . '</td>';
出于某种原因,不是输出所有匹配的值,而是只吐出一个。现在我还使用count函数来显示一个人创建的条目数,并显示一个小于实际数据库中的条目。 (意思是如果存在5则显示数字计数为4)这是代码:
$rResult = $stmt->fetchAll();
$gorResult = count($rResult);
我已尝试使用此代码使用fetchAll()并且根本不返回任何内容。我知道我必须在这里遗漏一些东西,这对于有新脑的人来说很简单。同样问题是这个调用只输出一行,而不是所有匹配的行。
$stmt = $pdo->prepare('SELECT * FROM admin WHERE user_id = :user_id');
$stmt->bindParam(':user_id', $userid);
$stmt->execute();
$rResult = $stmt->fetchAll();
$gorResult = count($rResult);
foreach($rResult as $row) {
$data_t = '<b>Length: '. $strt_length .' </b> | <b>Ground: '. $strt_ground .' </b>';
$data_t .= '<span class="label label-success">'. $gorResult .' Entries Total</span>';
$data_t .= '<table class="table table-striped">';
$data_t .= '<thead>';
$data_t .= '<tr>';
$data_t .= '<th>' . $table_fields['jo_col_1_name'] . '</th>';
$data_t .= '<th>' . $table_fields['jo_col_2_name'] . '</th>';
$data_t .= '<th>' . $table_fields['jo_col_3_name'] . '</th>';
$data_t .= '</tr>';
$data_t .= '</thead>';
$data_t .= '<tbody>';
$data_t .= '<tr>';
$data_t .= '<td>' . $row['date'] . '</td>';
$data_t .= '<td>' . $row['length'] . '' . $selected . '</td>';
$data_t .= '<td>' . $row['ground'] . '' . $selected . '</td>';
$data_t .= '<td>';
if (!$del_hide) {
$data_t .= "<form method='post' action='');' />";
$data_t .= "<input type='hidden' name='primary_key' value='".$row["primary_key"]."' />";
$data_t .= '<button type="submit" name="deleteItem" value="delete" class="btn btn-link">';
$data_t .= '<span class="glyphicon glyphicon-remove"></span></button>';
} else { };
$data_t .= '<button type="image" name="image" value="image" class="btn btn-link">';
$data_t .= '<span class="glyphicon glyphicon-picture"></span></button>';
$data_t .= '</form>';
}
$data_t .= '</td>';
$data_t .= '<td>';
$data_t .= '</td>';
$data_t .= '</tr>';
$data_t .= '</tbody>';
$data_t .= '</table>';
echo $data_t;
?>
答案 0 :(得分:1)
据我了解你漫长而多风的故事,你正在做这样的事情
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { // fetching one row
$data_t .= '<td>' . $row['date'] . '</td>'; // using row
$rResult = $stmt->fetchAll(); // FETCHING REST OF ROWS
...
} // no more rows for the next iteration
虽然必须
$data = $stmt->fetchAll(); // fetching rows
$count = count($data); // getting count
foreach($data as $row) { // iterating over rows
$data_t .= '<td>' . $row['date'] . '</td>'; // using row
}
答案 1 :(得分:0)
因为您指定了user_id
(我认为是唯一键),因此只有一行。只需删除它。