-1

I have these errors:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\store\lihatBarang.php on line 14

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\store\lihatBarang.php on line 20

this is my code

<?php
#------- memulai page number -------------------------------------------------------------------------------------#
$dataPerPage = 5; 
if(isset($_GET['hal']))
{   
    $noPage = $_GET['hal'];
}else{  
    $noPage = 1;
}
$offset   = ($noPage - 1) * $dataPerPage;
include "koneksi.php";
$ambil_data = mysql_query("select * from store order by id_barang desc limit $offset, $dataPerPage",$koneksi);
$hitung_record = mysql_query("SELECT COUNT(*) AS jumData FROM store",$koneksi);
$data          = mysql_fetch_array($hitung_record);
$jumData       = $data['jumData'];
$jumPage  = ceil($jumData/$dataPerPage);
# ceil digunakan untuk membulatkan hasil pembagian
#------- akhir page number -------------------------------------------------------------------------------------#

while($hasil_data = mysql_fetch_array($ambil_data)){
?>
dan9vu
  • 2,403
  • 3
  • 12
  • 30
heri s
  • 17
  • 2

2 Answers2

0

Check whether your query returns the values or not. Change your code to this (using mysqli_*):

<?php
#------- memulai page number --------------------------------------------------------------------#

$con=mysqli_connect("localhost","my_user","my_password","my_db");

$dataPerPage = 5; 
if(isset($_GET['hal']))
{   
    $noPage = $_GET['hal'];
}else{  
    $noPage = 1;
}
$offset   = ($noPage - 1) * $dataPerPage;
include "koneksi.php";
$ambil_data = mysqli_query($con,"select * from store order by id_barang desc limit $offset, $dataPerPage",$koneksi);
$hitung_record = mysqli_query($con,"SELECT COUNT(*) AS jumData FROM store",$koneksi);
$count=mysqli_num_rows($hitung_record);

if($count > 0){ // check if records exist

$data          = mysqli_fetch_array($hitung_record);
$jumData       = $data['jumData'];
$jumPage  = ceil($jumData/$dataPerPage);
# ceil digunakan untuk membulatkan hasil pembagian
#------- akhir page number -------------------------------------------------------------------------------------#

while($hasil_data = mysqli_fetch_array($ambil_data)){
// rest code here
} // end while
}// end if

?>
Nehal
  • 1,544
  • 4
  • 17
  • 30
-1

please use mysqli instead of mysql and if you don't want to use mysqli then you don't need to pass connection with query. mysql query example:-

 <?php  
    $con = mysql_connect("localhost", "root", "mypass") or  
        die("Could not connect: " . mysql_error());  
    mysql_select_db("tutorials");  
    $result = mysql_query("select * from tutorials");  
    echo "<h2>Here is a list of the topics:</h2>";  
    while ($row = mysql_fetch_array($result)) {  
        echo $row['name']."<br />";  
    }  
    mysql_close($con);  
    ?>  

and mysqli example:-

<?php
$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();
  }

// Perform queries
mysqli_query($con,"SELECT * FROM Persons");
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");

mysqli_close($con);
?> 
shubham715
  • 3,247
  • 1
  • 14
  • 27