得到一个错误mysql_fetch_array()期望参数1是资源

时间:2016-06-08 04:34:45

标签: php mysql

我有这些错误:

  

警告:mysql_fetch_array()期望参数1为资源,第14行的C:\ xampp \ htdocs \ store \ lihatBarang.php中给出布尔值

  

警告:mysql_fetch_array()要求参数1为资源,在第20行的C:\ xampp \ htdocs \ store \ lihatBarang.php中给出布尔值

这是我的代码

<?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)){
?>

2 个答案:

答案 0 :(得分:0)

检查您的查询是否返回值。将您的代码更改为(使用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

?>

答案 1 :(得分:-1)

请使用mysqli而不是mysql,如果您不想使用mysqli,则不需要传递与查询的连接。 mysql查询示例: -

 <?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);  
    ?>  

和mysqli示例: -

<?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);
?>