fetch(PDO :: FETCH_ASSOC仅返回一行 [英] fetch(PDO::FETCH_ASSOC only returning one row

查看:102
本文介绍了fetch(PDO :: FETCH_ASSOC仅返回一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从现在已贬值的mysql代码切换到PDO.该代码应该输出表中的所有值.这是代码:

Changing over from what is now depreciated mysql code, over to PDO. This code is supposed to output all the values within the table. Here is the code:

$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>';

由于某些原因,只输出一个,而不是输出所有选择的匹配值.现在,我还使用了一个计数功能来显示一个人进行了多少项输入,而该项显示的条目数比数据库中的实际条目少. (这意味着如果存在5,它将显示4的数字计数).这是代码:

For some reason, rather than output all of the matching values selected, only one is spit out. Now I also make use of a count function to display how many entries a person has made, and that shows one entry less than is actually in the database. (meaning it displays a numerical count of 4 if 5 exist) Here is that code:

$rResult = $stmt->fetchAll();
$gorResult = count($rResult);

我尝试将此代码与fetchAll()一起使用,并且什么也不返回.我知道我在这里一定会想念某些东西,对于一个头脑新鲜的人来说,这可能很简单.同样,问题在于此调用仅输出一行,而不输出所有匹配的行.

I have tried using fetchAll() with this code and that returns nothing at all. I know I must be missing something here, and it's likely simple for someone with a fresh brain. Again the issue is this call only outputs one row, rather than all matching rows.

$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;
?>

推荐答案

据我了解您漫长而多风的故事,您正在做这样的事情

As far as I understand your long and windy story, you are doing something like this

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
}

这篇关于fetch(PDO :: FETCH_ASSOC仅返回一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆