Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取错误mysql\u fetch\u array()希望参数1是资源_Php_Mysql - Fatal编程技术网

Php 获取错误mysql\u fetch\u array()希望参数1是资源

Php 获取错误mysql\u fetch\u array()希望参数1是资源,php,mysql,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 -----------------------------------

我有以下错误:

警告: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)){
?>

请改用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);  
    ?>  
<?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);
?> 

和mysqli示例:-

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

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

?>


请不要使用mysql_*函数,它已弃用且易受sql注入攻击。使用PDO或MySQLi.count将返回整数而不是数组?为什么使用变量$koneksi?@heri s,请检查我的答案是否解决了您的问题