Postingan lainnya
Bagaimana cara mengatasi warning : mysql ?
Web tidak menampilkan data dari database.
Bagaimana mengatasi
Warning: mysql_query() expects parameter 1 to be string, resource given in /storage/ssd5/857/4335857/public_html/search.php on line 20
Warning: mysql_num_rows() expects parameter 1 to be resource, null given in /storage/ssd5/857/4335857/public_html/search.php on line 22
yang muncul saat membuka menu yg berisi data yg berkaitan dg database. Ketika dijalankan dilocalhost tidak ada error atau warning seperti itu sama sekali. Ketika dihosting, ada coding warning seperti itu yg muncul. Servername, Username, Password, Database sudah dicocokkan dg data pada website tsb.
<?php
include "cek.php";
//if category and keyword already have values (which means user accessed it through search box in index page
//then proceed the following by showing ONLY what user requests
if(isset($_GET['category'])||isset($_GET['keyword'])){
$category = $_GET['category'];
$keyword = $_GET['keyword'];
//search for data in table buku where category contains keyword
$sql = "SELECT * FROM buku WHERE $category LIKE '%$keyword%'";
$result = mysql_query($connection, $sql);
$count=mysql_num_rows($result);
//if category and keyword are still null (which means user accessed it through navbar)
//then proceed the following by showing all data stored in database
}else{
$sql = "SELECT * FROM buku;";
$result = mysql_query($connection, $sql);
$count=mysql_num_rows($result);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>SMK Perikanan</title>
<link rel="stylesheet" href="bootstrap/css/bootstrap.css">
<script src="jquery/jquery-1.11.2.min.js"></script>
<script src="bootstrap/js/bootstrap.js"></script>
<style type="text/css">
/*to make the background image responsive and cover the page*/
body{
background-size: cover;
}
</style>
</head>
<body background="img/bg_lib.jpg">
<!-- navigation bar -->
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="index.php">SMK Perikanan</a>
</div>
<div>
<ul class="nav navbar-nav">
<li><a href="index.php">Beranda</a></li>
<li class="active"><a href="">Daftar Buku</a></li>
<li><a href="about.php">Informasi</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<a href="login-page.php">
<button type="button" class="btn btn-primary btn-md">
<span class="glyphicon glyphicon-log-in"></span> Login Admin
</button>
</a>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="well well-lg" style="background: white">
<h1 class="page-header">Pencarian Buku</h1>
<table class="table table-hover table-bordered">
<tr>
<th>No.</th>
<th>Judul</th>
<th>Pengarang</th>
<th>Penerbit</th>
<th>Kategori</th>
<th>Tahun terbit</th>
<th>Jilid edisi</th>
<th>ISBN</th>
</tr>
<?php
if($count>0){
$number = 1;
echo "<p class='text-info'>Ditemukan $count data cocok dengan keyword yang Anda masukkan<p>";
while($row = mysql_fetch_assoc($result)) {
echo "<tr> <td>" . $number. "</td><td> " .
$row["judul"]. "</td><td> " .
$row["pengarang"]."</td><td>".
$row["penerbit"]."</td><td>".
$row["kategori"]."</td><td>".
$row["thn_terbit"]."</td><td>".
$row["jilid_edisi"]."</td><td>".
$row["isbn"]."</td></tr>";
$number++;
}
}else{
echo "Maaf, tidak ditemukan data yang cocok.";
}
?>
</table>
</div>
</div>
</body>
</html>
5 Jawaban:
Coba agan cari bagain
Select * from buku
Ada 2 ( ; )
@JoksKur
Sudah diperbaiki nih, bro Tp tetep problemnya sama
mysqli harus menggunakan 2 parameter, yg agan tulis mysql_query. Coba ubah jadi :
$result = mysqli_query($connection, $sql);
atau kalau ingin tetep mysql_query coba jadi :
$result = mysql_query($sql);
Intinya di agan mana yg support, mysqli version atau mysql version.
@Nandar
Awalnya pake mysqli, dilocalhostnya. Namun setelah dihosting, tidak tampil sama sekali halaman webnya. Ketika diganti dari mysqli menjadi mysql, web baru muncul nih. Jadi saya pake mysql. Sudah saya ganti menjadi
$result = mysql_query($sql);
Lalu muncul ini, bro
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /storage/ssd5/857/4335857/public_html/search.php on line 22
error mysql_num_rows() expects parameter 1 to be resource, boolean given in terjadi karena query yang agan gunain error, coba lihat :
$sql = "SELECT * FROM buku;";
ada lebih dari 2 tanda ; coba hapus ; setelah kata buku. kalau masih error coba pake mysql_error()supaya tau errornya dimana.
Kalau boleh tau, dihosting pake php versi berapa?