从mysql获取数据

时间:2018-04-03 04:10:48

标签: php mysql mysqli

当我从列中检索数据时,它只返回一行并复制它

这就是问题所在:

  

<div id="Mapel">
                        <label for="mapel">Mapel </label>
                        <button type="button" name="tambahMapel" id="tambahMapel"
                        class="btn btn-info" title="Tambah Mapel">+</button>
                        <select name="mapel" class="form-control">
                        <option selected>Pilih...</option>
                        <?php
                        require_once '../class/mapel.php';
                        $mapel = new mapel(NULL,$tingkat);
                        $namaMapel= $mapel->tampil();
                        $idMapel = $mapel->getIdMapel();
                        foreach($namaMapel as $val){
                            echo "<option>".$val."</option>";
                        }
                        ?>
                        </select>
                    </div>

这就是我使用的类中的方法:

public function tampil(){
        $mysqli = new mysqli("localhost", "root", "", "UjianWare");
        $query = "select nama_mapel from mapel "
                . "where id_tingkatan ='".$this->getIdTingkatan()."'";
            $hasil = $mysqli->query($query);
            return $hasil->fetch_row();
    }

5 个答案:

答案 0 :(得分:1)

use mysqli_fetch_assoc();  instead of fetch_row();

如果它不起作用那么

$arr = [];
$stmt = $mysqli->prepare("SELECT id, name, age FROM myTable WHERE name = ?");
$stmt->bind_param("s", $_POST['name']);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()) {
  $arr[] = $row;
}
if(!$arr) exit('No rows');
var_export($arr);
$stmt->close();

https://websitebeaver.com/prepared-statements-in-php-mysqli-to-prevent-sql-injection

答案 1 :(得分:0)

您没有循环查看结果。你需要做

public function tampil(){
        $mysqli = new mysqli("localhost", "root", "", "UjianWare");
        $query = "select nama_mapel from mapel "
                . "where id_tingkatan ='".$this->getIdTingkatan()."'";
            $hasil = $mysqli->query($query);
            while($row = $hasil->fetch_row());
            {
            $rows[]=$row;
            }
            return $rows;
    }

使用while循环,您可以在$rows数组中添加值,并在函数末尾返回该数组。

答案 2 :(得分:0)

尝试以下方式更改查询,

$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()){
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="query";

$result=mysqli_query($con,$sql);

//Associative array
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);

//Numeric array

$row=mysqli_fetch_array($result,MYSQLI_NUM);

//Free result set

return mysqli_free_result($result);

答案 3 :(得分:0)

顺便说一下:你不应该在函数中调用数据库。 use是1time并在查询数据时调用$ conn。上传到主机时,它会更快地编辑和修复

答案 4 :(得分:0)

感谢Md Nazrul Islam,我在他的帮助下修复了我的代码。 这适用于遇到同样问题的每个人,我会发布我的最终代码,以便每个人都能理解

这是我班上的方法,我已经改变了

public function tampil(){
    $mysqli = new mysqli("localhost", "root", "", "UjianWare");
    $query = "select nama_mapel from mapel "
            . "where id_tingkatan =?";
        $hasil = $mysqli->prepare($query);
        $hasil->bind_param("s", $this->getIdTingkatan());
        $hasil->execute();
        $result = $hasil->get_result();
        while ($row =$result->fetch_assoc()){
            $arr[] = $row;
        }
        return $arr;
}

在这里另一个:

<div id="Mapel">
                        <label for="mapel">Mapel </label>
                        <button type="button" name="tambahMapel" id="tambahMapel"
                        class="btn btn-info" title="Tambah Mapel">+</button>
                        <select name="mapel" class="form-control">
                        <option selected>Pilih...</option>
                        <?php
                        require_once '../class/mapel.php';
                        $mapel = new mapel(NULL,$tingkat);
                        $namaMapel= $mapel->tampil();
                        $idMapel = $mapel->getIdMapel();
                        foreach($namaMapel as $val){
                            foreach ($val as $yo){
                            echo "<option>".$yo."</option>";
                            }
                        }
                        ?>
                        </select>
                    </div>