yoyo Posted December 1, 2013 Share Posted December 1, 2013 Hi, I am newbie for PHP and MySQL I found this error, please help me. Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Shopping\products.php on line 39 Here the full coding: <?php include("includes/db.php"); include("includes/functions.php"); error_reporting (E_ALL ^ E_NOTICE); if($_REQUEST['command']=='add' && $_REQUEST['productid']>0) { $pid=$_REQUEST['productid']; addtocart($pid,1); header("location:shoppingcart.php"); exit(); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Products</title> <script language="javascript"> function addtocart(pid){ document.form1.productid.value=pid; document.form1.command.value='add'; document.form1.submit(); } </script> </head> <body> <form name="form1"> <input type="hidden" name="productid" /> <input type="hidden" name="command" /> </form> <div align="center"> <h1 align="center">Products</h1> <table border="0" cellpadding="2px" width="600px"> <?php $result=mysql_query("select * from products"); while($row=mysql_fetch_array($result)){extract($row); ?> <tr> <td><img src="<?php echo $row['picture']?>" /></td> <td> <b><?php echo $row['name']?></b><br /> <?php echo $row['description']?><br /> Price:<big style="color:green"> RM<?php echo $row['price']?></big><br /><br /> <input type="button" value="Add to Cart" onclick="addtocart(<?php echo $row['serial']?>)" /> </td> </tr> <tr><td colspan="2"><hr size="1" /></td> <?php } ?> </table> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/284413-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean/ Share on other sites More sharing options...
denno020 Posted December 1, 2013 Share Posted December 1, 2013 Looks like your query has failed for some reason. Hence why a boolean (false) is being passed to mysql_fetch_array Link to comment https://forums.phpfreaks.com/topic/284413-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean/#findComment-1460808 Share on other sites More sharing options...
aysiu Posted December 1, 2013 Share Posted December 1, 2013 Either products isn't a real table, the table is totally empty, or the db.php include at the beginning did not successfully connect you to the database. P.S. From the PHP website: Quote This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. The extension they're talking about is the one you're using. Link to comment https://forums.phpfreaks.com/topic/284413-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean/#findComment-1460811 Share on other sites More sharing options...
yoyo Posted December 1, 2013 Author Share Posted December 1, 2013 I had amended to this : <?php include("includes/db.php"); include("includes/functions.php"); error_reporting (E_ALL ^ E_NOTICE); if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){ $pid=$_REQUEST['productid']; addtocart($pid,1); header("location:shoppingcart.php"); exit(); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Products</title> <script language="javascript"> function addtocart(pid){ document.form1.productid.value=pid; document.form1.command.value='add'; document.form1.submit(); } </script> </head> <body> <form name="form1"> <input type="hidden" name="productid" /> <input type="hidden" name="command" /> </form> <div align="center"> <h1 align="center">Products</h1> <table border="0" cellpadding="2px" width="600px"> <? $result=mysql_query("select * from products"); while($row=mysql_fetch_array($result)){ ?> <tr> <td><img src="<?=$row['picture']?>" /></td> <td> <b><?=$row['name']?></b><br /> <?=$row['description']?><br /> Price:<big style="color:green"> $<?=$row['price']?></big><br /><br /> <input type="button" value="Add to Cart" onclick="addtocart(<?=$row['serial']?>)" /> </td> </tr> <tr><td colspan="2"><hr size="1" /></td> <? } ?> </table> </div> </body> </html> It didn't have error but it still can't display the database in phpMyAdmin... What should I do? Link to comment https://forums.phpfreaks.com/topic/284413-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean/#findComment-1460813 Share on other sites More sharing options...
Ch0cu3r Posted December 1, 2013 Share Posted December 1, 2013 Quote It didn't have error but it still can't display the database in phpMyAdmin... What should I do? What do you mean by this? Is this addtocart($pid,1); supposed to add the product to the database? What is the code for this php function Link to comment https://forums.phpfreaks.com/topic/284413-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean/#findComment-1460843 Share on other sites More sharing options...
aysiu Posted December 1, 2013 Share Posted December 1, 2013 You're using PHPMyAdmin? Good. What happens when you leave the PHP aside for now and use PHPMyAdmin and paste this in as a SQL query? select * from products Link to comment https://forums.phpfreaks.com/topic/284413-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean/#findComment-1460850 Share on other sites More sharing options...
MDCode Posted December 1, 2013 Share Posted December 1, 2013 Are short tags on? Link to comment https://forums.phpfreaks.com/topic/284413-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean/#findComment-1460852 Share on other sites More sharing options...
MDCode Posted December 1, 2013 Share Posted December 1, 2013 Edit: Editing my last post created a new post...? Ignore this one anyways Link to comment https://forums.phpfreaks.com/topic/284413-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean/#findComment-1460853 Share on other sites More sharing options...
yoyo Posted December 2, 2013 Author Share Posted December 2, 2013 It didn't display the product, because in my database I do insert the data. the function.php code is : <? function get_product_name($pid){ $result=mysql_query("select name from products where serial=$pid"); $row=mysql_fetch_array($result); return $row['name']; } function get_price($pid){ $result=mysql_query("select price from products where serial=$pid"); $row=mysql_fetch_array($result); return $row['price']; } function remove_product($pid){ $pid=intval($pid); $max=count($_SESSION['cart']); for($i=0;$i<$max;$i++){ if($pid==$_SESSION['cart'][$i]['productid']){ unset($_SESSION['cart'][$i]); break; } } $_SESSION['cart']=array_values($_SESSION['cart']); } function get_order_total(){ $max=count($_SESSION['cart']); $sum=0; for($i=0;$i<$max;$i++){ $pid=$_SESSION['cart'][$i]['productid']; $q=$_SESSION['cart'][$i]['qty']; $price=get_price($pid); $sum+=$price*$q; } return $sum; } function addtocart($pid,$q){ if($pid<1 or $q<1) return; if(is_array($_SESSION['cart'])){ if(product_exists($pid)) return; $max=count($_SESSION['cart']); $_SESSION['cart'][$max]['productid']=$pid; $_SESSION['cart'][$max]['qty']=$q; } else{ $_SESSION['cart']=array(); $_SESSION['cart'][0]['productid']=$pid; $_SESSION['cart'][0]['qty']=$q; } } function product_exists($pid){ $pid=intval($pid); $max=count($_SESSION['cart']); $flag=0; for($i=0;$i<$max;$i++){ if($pid==$_SESSION['cart'][$i]['productid']){ $flag=1; break; } } return $flag; } ?> Link to comment https://forums.phpfreaks.com/topic/284413-warning-mysql_fetch_array-expects-parameter-1-to-be-resource-boolean/#findComment-1460916 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.