2016-06-08 74 views
-1

我有這些錯誤:得到一個錯誤mysql_fetch_array()預計參數1是資源

警告:mysql_fetch_array()預計參數1是資源,布爾在C中給出:\ XAMPP \ htdocs中\店\ lihatBarang.php線14上

警告:mysql_fetch_array()預計參數1是資源,在布爾C中給出:行\ XAMPP \ htdocs中\存儲\ lihatBarang.php 20

這是我的代碼

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

請不要使用mysql_ *功能,它的棄用,容易受到SQL注入。使用PDO或MySQLi。 – arunrc

+0

計數將返回整數不是數組? –

+0

爲什麼你使用變量$ koneksi? –

回答

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

?> 
相關問題