0

我在 php 脚本中循环 odbc_fetch_array 有一点问题...问题是第二个问题:我的数据库中有一些数据,我想在其中搜索特殊数据...我将数据放入数组$row = odbc_fetch_array('$array')中,但我只是卡住了在这一行的最后记录......所以我想知道如何打印这个数组中的所有数据......

这是我的php代码:

while ($row = odbc_fetch_array($surnames))
                {
                    echo json_encode($row);

                    /*$f_name = trim(odbc_result($surnames, "F_NAME"));
                    $s_name = trim(odbc_result($surnames, "S_NAME"));*/

                    //getting birthdate from personal id
                    $bd = trim(odbc_result($surnames, "PERS_KOD"));

                    $day = substr($bd, 0, 2);
                    $month = substr($bd, 2, 2);
                    $year = substr($bd, 4, 2);

                    $birthdate = $day.".".$month.".".$year;

                    $table = "
                                <table>
                                    <th>Name</th>
                                    <th>Surname</th>
                                    <th>Birth Date</th>

                                    <tr>
                                        <td>".$row['f_name']."</td>
                                        <td>".$row['s_name']."</td>
                                        <td>".$birthdate."</td>
                                    </tr>
                                </table>
                             "; 
                    $data['result']['table'] = $table;
                }

编辑1

这里在echo里面

json_encode(tr)<tr><td>ALEVTINA <\/td><td>KARPOVA <\/td><td>14.07.27<\/td><\/tr>"null"json_encode(tr)<tr><td>DACE <\/td><td>KARPOVA <\/td><td>08.10.77<\/td><\/tr>"nullnullnull"json_encode(tr)<tr><td>OLGA <\/td><td>KARPOVA

这里在var_dump外面

array(65) { [0]=> string(89) "<tr><td>GA�INA </td><td>KARPOVA </td><td>08.03.56</td></tr>"

编辑2

也许我的 html 页面有问题,我在哪里取回这张表?这是代码$.ajax ({ data: { action: "search_patient_surname", surname: inserted_surname }, success: function(a) { console.log(a); if (a.errors.length > 0) { //vivodit input_error.val(a.errors.join(' | ')); } $('#Persons').html(a.result.table); } });

4

1 回答 1

1

问题是您$data['result']['table']的每次迭代都会被覆盖,留下最后一条记录。

此外,我假设您想要一个包含多行而不是多个表的表。

尝试这个:

$result_tr = array();
while ($row = odbc_fetch_array($surnames))
{
    echo json_encode($row);

    /*$f_name = trim(odbc_result($surnames, "F_NAME"));
    $s_name = trim(odbc_result($surnames, "S_NAME"));*/

    //getting birthdate from personal id
    $bd = trim(odbc_result($surnames, "PERS_KOD"));

    $day = substr($bd, 0, 2);
    $month = substr($bd, 2, 2);
    $year = substr($bd, 4, 2);

    $birthdate = $day.".".$month.".".$year;

    $tr = "<tr><td>{$row['f_name']}</td><td>{$row['s_name']}</td><td>{$birthdate}</td></tr>";
    $result_tr[] = $tr;
}

$table = "";
if (!empty($result_tr)) {
    $table = "<table><th>Name</th><th>Surname</th><th>Birth Date</th>";
    foreach ($result_tr as $row) {
        $table .= $row;
    }
    $table .= "</table>";
}

$data['result']['table'] = $table;
于 2013-08-16T08:18:45.663 回答