Jump to content

Recommended Posts

i'm sorry im newbie of php...

i found this...

http://forums.phpfreaks.com/topic/181256-solved-warning-mysql-fetch-array-expects-parameter-1-to-be-resource/

but in my case i didn't use $result in my while....why it doesnt work? somebody help me...thx before 

 

ERROR on line 17coba.php

 

$total_record = mysql_fetch_array(mysql_query('SELECT COUNT(`product_id`) AS rows FROM `product`' )); //ERROR
$total_record=$total_record['rows'];
 
$total_page=ceil($total_record/$per_page);
if($page>$total_page) $page=$total_page;
       
$query_exec = get_all_product();
echo "<table align='center'>";
echo "<tr align=center>";
echo"<th>Image</th><th>Name</th><th>About</th><th>Price</th><th>Stoks</th><th>Category</th>";
echo "</tr>";
while ($row = mysql_fetch_assoc($query_exec)){
echo "<tr align=center>";
echo"<td><img src='".$row['product_image']."'/><td>";
echo "<td>" .$row['product_name']. "</td>";
echo "<td>" .$row['product_description']. "</td>";
echo "<td>" .$row['product_price']. "</td>";
echo "<td>" .$row['product_stok']. "</td>";
echo "<td>" .$row['category_name']. "</td>";
echo "<td><input type=button value=delete name='delete' onClick = lemparproduct('".$row['product_id']."')></td>";
echo "<td><input type='hidden' id='idproducthidden' value='".$row['product_id']."'>
 <input type=button value=update name='updates' onClick = wew('".$row['product_id']."')></td>";
echo "</tr>";}
echo "</table>";
 
echo 'page : <select onchange="window.location=\'?p=\'+this.value">';
for($i=1;$i<=$total_page;$i++) 
echo '<option' . ( $i==$page ? ' selected' : '' ) . '>' . $i . '</option>';
echo '</select><br>';
Edited by BrianAustin

mysql_query('SELECT COUNT(`product_id`) AS rows FROM `product`' ) this part is not right. put it as a variable

$query = mysql_query('SELECT COUNT(`product_id`) AS rows FROM `product`' )or die(mysql_error()); // i know mysql_ not supported but oh well
$total_record = mysql_fetch_assoc($query);

and query will give you the exact error that is wrong :)

Edited by _EmilsM

welcome to phpfreaks, please use the code tags around your code when posting it in the forum, it makes everyones life much easier.

 

break down your query execution to make the code more managable, and add - as _EmilsM said - an or die() with the mysql_error() output to see what part of it is failing and why:

 

$sql = 'SELECT COUNT(`product_id`) AS rows FROM `product`';
$result = mysql_query($query) or die("ERROR :<br>".mysql_error()."<br><br>Occured when trying to run the following query :<br><br>".$sql);
$total_record = mysql_fetch_array($result);
$total_record=$total_record['rows'];

$per_page = 5; 
$page_query = mysql_query("SELECT COUNT(*) FROM product");
$pages = ceil(mysql_result($page_query, 0) / $per_page); 
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page; 
$query = mysql_query("SELECT * FROM product p JOIN category c ON p.category_ids = c.category_id LIMIT $start, $per_page");
while($query_row = mysql_fetch_assoc($query)){
    echo "<tr align=center>";
				echo "<td>";
				echo"<img src='".$row['product_image']."'/>";
				echo "<td>" .$row['product_name']. "</td>";
				echo "<td>" .$row['product_description']. "</td>";
				echo "<td>" .$row['product_price']. "</td>";
				echo "<td>" .$row['product_stok']. "</td>";
				echo "<td>" .$row['category_name']. "</td>";
				echo "<td><input type=button value=delete name='delete' onClick = lemparproduct('".$row['product_id']."')></td>";					
				echo "<td><input type='hidden' id='idproducthidden' value='".$row['product_id']."'>
						  <input type=button value=update name='updates' onClick = wew('".$row['product_id']."')></td>";							
			echo "</tr>";			
} 
if($pages >= 1 && $page <= $pages){
    for($x=1; $x<=$pages; $x++){
        echo ($x == $page) ? '<b><a href="?page='.$x.'">'.$x.'</a></b> ' : '<a href="?page='.$x.'">'.$x.'</a> ';
    }
}

HI there sory for that plain code....can u teach me ...why that error happen?  :confused:  sorry i'm newbie

 

Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\coba\view\product_view.php on line113

 

// $pages = ceil(mysql_result($page_query, 0) / $per_page);

 

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\coba\view\product_view.phpon line 117

 

// while($query_row = mysql_fetch_assoc($query)){

Edited by BrianAustin

your error is because the query is failing, as such it is returning boolien false instead of the result resource that the mysql_result() and mysql_fetch_assoc() require. That is also not the same code that you posted in your OP.  working ith the OP first - replace your single query line here :

 

$total_record = mysql_fetch_array(mysql_query('SELECT COUNT(`product_id`) AS rows FROM `product`' )); //ERROR

with the following (revised from inital post as I mixed up some variables):

 

$sql = 'SELECT COUNT(`product_id`) AS rows FROM `product`';
$query = mysql_query($sql) or die("ERROR :<br>".mysql_error()."<br><br>Occured when trying to run the following query :<br><br>".$sql);
$result = mysql_fetch_array($query);
$total_record=$result['rows'];

then apply the same design to the other queries in the second code section that you posted :

 

...
$page_sql = "SELECT COUNT(*) FROM product";
$page_query = mysql_query($page_sql) or die ("ERROR :<br>".mysql_error()."<br><br>Occured when trying to run the following query :<br><br>".$page_sql);
$page_results = mysql_result($page_query, 0)
$pages = ceil($page_results / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$sql = "SELECT * FROM product p JOIN category c ON p.category_ids = c.category_id LIMIT $start, $per_page";
$query = mysql_query($sql) or die ("ERROR :<br>".mysql_error()."<br><br>Occured when trying to run the following query :<br><br>".$sql);
while($query_row = mysql_fetch_assoc($query)){
...

 

This is basic database debugging, you use the "or die()" function to stop the code execution in the event that the transaction with the database is unsuccessful.  You then include the database server error and the query string that was sent to the server in there to show you what went wrong with the database transaction and what was sent in that transaction.  This gives you something to work with when trying to find out what and where the problem is.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.