在数据库上使用'while FetchObject()'来检索记录并放入表中返回重复项 [英] using 'while FetchObject()' on a database to retrieve records and put into a table is returning duplicates

查看:93
本文介绍了在数据库上使用'while FetchObject()'来检索记录并放入表中返回重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一些代码可以从数据库中检索一行并将其存储在名为$ row的变量中。



使用while函数,对于使用sql检索的数据库中的每条记录,我必须放入一个表。



I我已经创建了一个新表,而不是新表添加到表中。例如,第一个表显示检索到的第一个记录(称为A)。然后第二个表显示检索到的前两个记录(A来自之前,新的一个是B),第三个表显示A,B和新的一个名为C,依此类推。



这是我下面的代码,我不知道这是一个很大的错误,我没看到,或者它只是一个小错误,我没见过:



Hi,

I have a some code that retrieves a row from a database and stores it in a variable called $row.

using a while function, for every record in database that is retrieved using sql, I have to put into a table.

I have got that working but for every new record I get a new table is created instead of the new row being added to the table. For example, the first table displays the first record retrieved (call this A). then the second table displays the first two records retrieved (A from before, and the new one which is B), the third table displays A,B and the new one called C, and so on.

this is the code i have below, I dont know if it is a big error im not seeing or if it is just a small mistake ive not seen:

$sqlQuery = "MY CORRECT QUERY; ";

$stmt = $db->query( $sqlQuery );

$table = <<<EOD
<table border="1" cellpadding="0" width="100%">
    <tr>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
    <tr>

EOD;

while ( $s= $stmt->fetchObject()){

   $row = "<tr><td>" . $s->studentCode .
       "</td>\n<td>" . $s->surname .
       "</td>\n<td>" . $s->forename .
       "</td>\n<td>" . $s->typeDesc .
       "</td>\n<td>" . $s->startYear . "</td>\n</tr><br />";

    $table .= $row;

    echo $table;

}
?>




</table>

推荐答案

行。



使用while函数,对于使用sql检索的数据库中的每条记录,我必须投入一张桌子。



我已经有了工作但是对于每一条新记录,我都会创建一个新表,而不是将新行添加到表中。例如,第一个表显示检索到的第一个记录(称为A)。然后第二个表显示检索到的前两个记录(A来自之前,新的一个是B),第三个表显示A,B和新的一个名为C,依此类推。



这是我下面的代码,我不知道这是一个很大的错误,我没看到,或者它只是一个小错误,我没见过:



row.

using a while function, for every record in database that is retrieved using sql, I have to put into a table.

I have got that working but for every new record I get a new table is created instead of the new row being added to the table. For example, the first table displays the first record retrieved (call this A). then the second table displays the first two records retrieved (A from before, and the new one which is B), the third table displays A,B and the new one called C, and so on.

this is the code i have below, I dont know if it is a big error im not seeing or if it is just a small mistake ive not seen:


sqlQuery = MY CORRECT QUERY;;

sqlQuery = "MY CORRECT QUERY; ";


stmt =
stmt =


这篇关于在数据库上使用'while FetchObject()'来检索记录并放入表中返回重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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