php - 如何在php中显示数据 View mysql

标签 php mysql

我做了一个 View mysql和php想通过过滤器来显示操作,但是结果无法显示。这是代码 该函数用于显示数据

function tampilDataRegFilter($data){
        $query=mysql_query("SELECT * from dataPendaftaran WHERE nm_mhs LIKE '%$data%'");
        $no_rows=mysql_num_rows($query);
        if($no_rows==1){
            while($row=mysql_fetch_array($query)){
                $data[]=$row;
                return $data;
            }
        }  
    }

此处显示数据

<?php
        //tampil berdasar filter
        if($_POST['do']=="find"){
            $arrayBayarReg=$data->tampilDataRegFilter($_POST['q']);
        }
        if(count($arrayBayarReg)){
            foreach($arrayBayarReg as $data){
                ?>
                <tr class="tabcont">
                    <td class="tabtxt" align="center"><?php echo $c=$c+1; ?>.</td>
                    <td class="tabtxt" align="center"><?php echo $data['kode_bayar'];?></td>
                    <td class="tabtxt" align="center"><?php echo $data['nm_mhs'];?></td>
                    <td class="tabtxt" align="center"><?php echo $data['tgl_bayar']; ?></td>
                    <td class="tabtxt" align="center"><?php echo $data['jumlah']; ?></td>
                    <td class="tabtxt" align="center"><?php echo $data['keterangan']; ?></td>
                </tr>
                <?php
            }
        }else{
            echo 'Not Found !';
        }
    ?>

如何解决这个问题,我是 php 初学者。非常感谢您的帮助:)

最佳答案

function tampilDataRegFilter($data){
    $query=mysql_query("SELECT * from dataPendaftaran WHERE nm_mhs LIKE '%$data%'");
    $no_rows=mysql_num_rows($query);
    if($no_rows >= 1){ // Does this if there was more than or 1 row. Not only if there was one row
        $data = array();
        while($row=mysql_fetch_array($query)){
            $data[]=$row; // Does not return data here
        }
        return $data; // Return should only be done when all the data is in the array, which is after while loop is finished
    } else {
        return false;
    }
}

我也会改变这个:

<?php
    //tampil berdasar filter
    if($_POST['do']=="find"){
        $arrayBayarReg=$data->tampilDataRegFilter($_POST['q']);
    }
    if($arrayBayarReg !== false){ // Returns false if no rows were found
        foreach($arrayBayarReg as $data1){ // Wouldnt overwrite the $data variable which apparently holds your data object.
            ?>
            <tr class="tabcont">
                <td class="tabtxt" align="center"><?php echo $c=$c+1; ?>.</td>
                <td class="tabtxt" align="center"><?php echo $data1['kode_bayar'];?></td>
                <td class="tabtxt" align="center"><?php echo $data1['nm_mhs'];?></td>
                <td class="tabtxt" align="center"><?php echo $data1['tgl_bayar']; ?></td>
                <td class="tabtxt" align="center"><?php echo $data1['jumlah']; ?></td>
                <td class="tabtxt" align="center"><?php echo $data1['keterangan']; ?></td>
            </tr>
            <?php
        }
    }else{ // Returned false, so no rows were found
        echo 'Not Found !';
    }
?>

试试这个。

关于php - 如何在php中显示数据 View mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25953320/

相关文章:

php - 在 SSL 握手中,是否可以颠倒角色?

php - 理解 <?php print $node->content ['body' ] ['#value' ]; ?> 在 Drupal 中

python - 如何停止 scrapy 蜘蛛但处理所有想要的项目?

php - 尝试在 codeigniter 的一列中上传两个图像 file_path

Javascript 不等待使用 bootstrap-wizard.js 的 ajax

PHP 重定向回上一页

php - firebase 存储和从 php 上传

php - MySQL查询选出成绩不全的学生 'A'

mysql - 如何在 MySQL 中返回数据透视表输出?

mysql - Group By MySQL 查询返回带有参数的特定结果