Citra45Abadi
Citra45Abadi

Reputation: 207

while loop stopped at 1st loop because mysqli error

the error reads

mysqli_fetch_array() expects parameter 1 to be mysqli_result, double given in C:\xampp\htdocs\beta\jawa\isi.php on line 744

line 744 is : while($r=mysqli_fetch_array($hasil))

the full code :

<?php
$cari = "SELECT * FROM produk WHERE " ;
for ($i=0; $i<=$jml_kata; $i++) {
    $cari .= "deskripsi LIKE '%$pisah_kata[$i]%' OR nama_produk LIKE '%$pisah_kata[$i]%'";
    if ($i < $jml_kata ) {
        $cari .= " OR "; 
    }
}

$cari .= " ORDER BY id_produk DESC LIMIT 12";
$hasil  = mysqli_query($con,$cari);

while($r=mysqli_fetch_array($hasil)) {

    include "diskon_stok.php";
    echo"<div class='container_produk'>
        <a href='produk-$r[id_produk]-$r[produk_seo].html'><img src='http://images.rajafotocopy.com/foto_produk/$r[gambar]'/></a>
        <div class='detail'> 
            <span style='font-family : sui'>$r[nama_produk]</span><br />
            $divharga
        </div>
    </div>";
}
?>

I don't understand what's wrong with mysqli_result....? it is returning the 1st loop but after that it throws an error and expects parameter 1 to be mysqli_result?

Upvotes: 0

Views: 128

Answers (2)

Barmar
Barmar

Reputation: 782315

Something in diskon_stok.php is doing:

$hasil = <something>;

where <something> is a number.

So when the loop repeats and it calls mysqli_fetch_array($hasil), the variable no longer contains the query result, it contains this number, and that call fails.

Change the name of the variable you're using for the query result.

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

while($r=mysqli_fetch_array($result)){

Upvotes: 1

Eric Brown
Eric Brown

Reputation: 1462

The problem is the while loop. While loops are for boolean (true/false) expressions, when you really want to loop through the list itself. Put more simply, a while loop is basically an if statement asked over and over again as long as it is true. For what you are asking for, I would recommend a foreach loop. You can set it up as something like this:

$resultArray = mysqli_fetch_array($hasil);

foreach($resultArray as $r){
      [Code to execute]
}

This should work more for your needs.

Upvotes: 0

Related Questions