Jump to content

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean


yoyo

Recommended Posts

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">
<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
Share on other sites

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:

 

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
Share on other sites

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">
<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?
Edited by yoyo
Link to comment
Share on other sites

 

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

Edited by Ch0cu3r
Link to comment
Share on other sites

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
Share on other sites

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.