简体   繁体   English

fetch(PDO :: FETCH_ASSOC仅返回一行

[英]fetch(PDO::FETCH_ASSOC only returning one row

Changing over from what is now depreciated mysql code, over to PDO. 从现在折旧的mysql代码转换为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)) {

and the code that calls the fields are: 调用字段的代码是:

$data_t .= '<td>' . $row['date'] . '</td>';
$data_t .= '<td>' . $row['length'] . '' . $selected . '</td>';
$data_t .= '<td>' . $row['ground'] . '' . $selected . '</td>';

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: (这意味着如果存在5,它将显示4的数字计数)这是该代码:

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

I have tried using fetchAll() with this code and that returns nothing at all. 我尝试将fetchAll()与此代码一起使用,并且什么也不返回。 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

while it have to be 虽然必须

$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
}

There is only one row because you specified user_id (which, I assume, is unique key). 只有一行是因为您指定了user_id (我认为这是唯一键)。 Just remove it. 只需将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM