-1

Help Me i tried to fix this but its still error this my code :

            if(isset($_GET['page'])){
                $nopage=$_GET['page'];
            }else $nopage=1;

            $offset=($nopage-1)*$dataperpage;

            $sql = mysqli_query($koneksi, "SELECT * FROM barang ORDER BY idbarang LIMIT $offset, $dataperpage ")or die('Error');
            if(mysqli_num_rows($sql) == 0){
                echo "Tidak ada produk!";
            }else{
                while($data = mysqli_fetch_assoc($sql)){
        ?>
            <div class="span4">
                <div class="icons-box">
                    <div class="title"><h3><?php echo $data['namabarang']; ?></h3></div>
                    <img src="<?php echo "images/".$data['gambar']; ?>" />
                    <div><h3>Rp.<?php echo number_format($data['harga'],2,",",".");?></h3></div>
                    <div class="clear"><a href="detailproduk.php?hal=detailbarang&kd=<?php echo $data['idbarang'];?>" class="btn btn-lg btn-danger">Detail</a> <a href="detailproduk.php?hal=detailbarang&kd=<?php echo $data['idbarang'];?>" class="btn btn-lg btn-success">Beli</a></div>
                </div>
            </div>
        <?php   

            }
          }  
            $data=mysql_fetch_array(mysql_query("SELECT * FROM barang"));

            $jumdata=$data;//tak input manual karena mysql_fetch_arraynya error gak tau kenapa

            $jumpage=ceil($jumdata/$dataperpage);

            //echo $jumpage;

            $showpage=$nopage;

            if($nopage>1)
                echo "<a href='".$_SERVER['PHP_SELF']."?page=".($nopage-1)."'>&lt;&lt; Prev </a>";
            for ($page=1;$page<=$jumpage;$page++){
                if((($showpage>=$nopage-3)&&($page<=$nopage+3))||($page==1)||($page==$jumpage)){
                    if(($showpage==1)&&($page!=2))
                        echo "...";
                    if(($showpage!=($jumpage-1))&&($page==$jumpage))
                        echo "...";
                    if ($page==$nopage)
                        echo"<b>".$page."</b>";
                    else echo "<a href='".$_SERVER['PHP_SELF']."?page=".$page."'>".$page."</a>";
                    $showpage=$page;
                }
            }
            if($nopage<$jumpage)
                echo "<a href='".$_SERVER['PHP_SELF']."?page=".($nopage+1)."'>  Next &gt;&gt; </a>";

        ?>

when i run it, it always Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\kovet\Tokomaster\produk.php on line 80

Calvter ID
  • 1
  • 1
  • 1
  • The documentation shows that a resource is provided on success, and FALSE on error. If you are receiving a boolean, your query is failing for some reason. – James May 19 '17 at 23:08
  • 1
    You can't mix and match `mysql_query` and `mysqli_query`. One way to avoid making this mistake is to use the object-oriented interface to `mysqli` exclusively: `$db = new mysqli(...)` and then things like `$db->query()` and so on make it very hard to call the obsolete `mysql_*` functions for lack of a letter `i`. – tadman May 19 '17 at 23:16
  • **WARNING**: When using `mysqli` you should be using [parameterized queries](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [`bind_param`](http://php.net/manual/en/mysqli-stmt.bind-param.php) to add user data to your query. **DO NOT** use string interpolation or concatenation to accomplish this because you have created a severe [SQL injection bug](http://bobby-tables.com/). **NEVER** put `$_POST`, `$_GET` or **any** user data directly into a query, it can be very harmful if someone seeks to exploit your mistake. – tadman May 19 '17 at 23:16

3 Answers3

2

It looks like you used mysqli_query and mysqli_fetch_array above the mysql_query and mysqli_fetch_array.

Since it looks like it's being done procedurally try changing your mysql stuff which has been deprecated to mysqli. It's likely you haven't made a mysql connection and I'm not sure if they will both work if your connection is made through mysqli.

JacobW
  • 826
  • 5
  • 15
0

mysql_query returns FALSE if the query had an error, in your case, where you are using it for a SELECT type query. Store the result in a variable, then compare it to FALSE to see if the query was successful, before calling mysql_fetch_array

0

This means that mysql_fetch_array () is expecting 2 parameters, they would be:

  • Parameter 1: Database connection Variable which contains you database table, username and password info.

  • Parameter 2: Query.

CodePlague
  • 89
  • 10