General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old February 9th, 2003, 01:11 AM
javier odio javier odio is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 2 javier odio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question shopping cart problem

Does anyone knows how to solve my problem... im doing shopping cart tutorial but i receive this warning on my browser


Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\inetpub\wwwroot\cart\products.php on line 43

<?php
while($row = mysql_fetch_array($result))
?>


please help me out im a newbie....


here is more code:

</td>
<td width="10%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
<b>Add</b>
</font>
</td>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td width="30%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["itemName"]; ?>
</font>












Last edited by javier odio : February 9th, 2003 at 10:04 AM.

Reply With Quote
  #2  
Old February 9th, 2003, 09:58 AM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
javier,

It's most likely a problem with a line above the mentioned one... PHP is funny like that!

Would it be possible to paste some of the code that is above that line...
__________________
____________________________________________
Developer Shed Weekly Writer | DevArticles Forum Moderator
Build Your Own KlipFolio Klip With PHP
FrankManno.com - Under Construction
Design Interactive Group - Under Construction

Reply With Quote
  #3  
Old February 9th, 2003, 10:13 PM
bmy78 bmy78 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 6 bmy78 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I'm also having the same problem with the shopping cart tutorial on this site. I'm getting the same error message:

"Warning: Supplied argument is not a valid MySQL result resource in /www/htdocs/cart/products.php on line 25"


HERE IS THE CODE IN PRODUCTS.PHP:

<?php

// This page will list all of the items
// from the items table. Each item will have
// a link to add it to the cart

include("db.php");

// Get a connection to the database

$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
$result = mysql_query("select * from items order by itemName asc");

?>

<html>
<head>
<title>Products</title>
</head>
<body>


<?php

while($row = mysql_fetch_array("$result"))
{
?>
<table>
<tr>
<td width='30%' height='25'>
<font face="verdana" size="1" color="black">
<?php echo $row["itemName"]; ?>
</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["itemPrice"]; ?>
</font>
</td>
<td width="50%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["itemDesc"]; ?>
</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="1" color="black">
<a href="cart.php?action=add_item&id=<?php echo $row['itemId']; ?>&qty=1">Add Item</a>
</font>
</td>
</tr>
<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>
<td width="100%" colspan="4">
<font face="verdana" size="1" color="black">
<a href="cart.php">Your Shopping Cart >></a>
</font>
</td>
</tr>
</table>
<?php
}
?>

</body>
</html>

LINE 25 IS:
while($row = mysql_fetch_array("$result"))

HERE IS CODE FOR THE PHP INCLUDE FILE:


<?php

// this page contains the connection routine for the
// database as well as getting the ID of the cart, etc

$dbServer = "localhost";
$dbUser = "cart";
$dbPass = "cart123";
$dbName = "cart";

function ConnectToDb($server, $user, $pass, $database)
{
// Connect to the database and return
// true/false depending on whether or
// not a connection could be made

$s = mysql_connect($server, $user, $pass);
$d = mysql_select_db($database, $s);

if(!$s || !$d)
echo "Error: There is something wrong with the database.";
else
return true;
}

function GetCartId()
{

// This function will generate an encrypted string
// and will set it as a cookie using set_cookie.
// This will also be used as the cookieId field
// in the cart table.

if(isset($_COOKIE["cartId"]))
return $_COOKIE["cartId"];
else
{
// There is no cookie set. We will set the cookie
// and return the value of the users session ID

session_start();
setcookie("cartId", session_id(), time() + ((3600 * 24) * 30));
return session_id();
}
}

?>

IF ANYONE CAN HELP ME, I'D GREATLY APPRECIATE IT!

Thanks.

Reply With Quote
  #4  
Old February 10th, 2003, 09:27 AM
Dr. Lundy Dr. Lundy is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Hoek van Holland, The Netherlands
Posts: 4 Dr. Lundy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Yep I'm having the same problem, I'm getting a bit frustrated. I'm using the supported material scripts, but the error message returned over and over again.

Beneath is the product.php code from supported material. Does somebody know the remedie....

This the error message:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\phpdev\www\cart\products.php on line 43

And the code is:
PHP Code:
<?php
while($row mysql_fetch_array($result))
{
?>


The full code of the product.php file is attached.
Attached Files
File Type: txt products.txt (2.2 KB, 231 views)

Reply With Quote
  #5  
Old February 10th, 2003, 08:14 PM
ramz ramz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Juiz de Fora - MG- Brazil
Posts: 93 ramz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via ICQ to ramz Send a message via MSN to ramz
Try this:

$dbServer = "localhost";
$dbUser = "cart";
$dbPass = "cart123";
$dbName = "cart";

function ConnectToDb($server, $user, $pass, $database)
{
$s = mysql_connect($server, $user, $pass);
if(!$s) { print "Not connected"; }
if(!mysql_select_db($database, $s)) {
print "Could not open the database";
} else {
return $s;
}
}
__________________
Regards,
Ramiro Varandas Jr.

Reply With Quote
  #6  
Old February 10th, 2003, 08:56 PM
Ben Rowe
Guest
Dev Articles Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Fellas,

If your getting that error then that means there is something WRONG with the $result variable

$result = mysql_query("select * from items order by itemName asc");

Make sure that your connected correctly. Then ensure that the query is CORRECT.

Most likley it will be your connection details.

Reply With Quote
  #7  
Old February 10th, 2003, 10:09 PM
bmy78 bmy78 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 6 bmy78 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Okay, now I feel stupid...

I put the variable $query in quotes in the mysql_query() function. I've taken out the "'s and now the program works.

Thanks guys for your help. Much appreciated.

--Brian.

Reply With Quote
  #8  
Old February 11th, 2003, 08:14 AM
Dr. Lundy Dr. Lundy is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Hoek van Holland, The Netherlands
Posts: 4 Dr. Lundy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Oke it works Thanks for the tips....

The problem was something with the database connection. I removed the following lines:

From the file db.php,

PHP Code:
 $dbServer "localhost";
                
$dbUser "";
    
$dbPass "";
    
$dbName "cart";

    function 
ConnectToDb($server$user$pass$database)
    {
        
// Connect to the database and return
        // true/false depending on whether or
        // not a connection could be made.


        
$s = @mysql_connect($server$user$pass);
        
$d = @mysql_select_db($database$s);

        if(!
$s || !$d)
            return 
false;
        else
            return 
true;
    } 


and replace then by two siple line like,

PHP Code:
 mysql_connect("localhost","","");
mysql_select_db("cart"); 


Finally I removed from every file the line:,

PHP Code:
 $cxn = @ConnectToDb($dbServer$dbUser$dbPass$dbName); 


Because I'm a newbie with PHP I've got a few questions.

1. What does variable $cxn do?
2. What's the main difference between my solution and the original lines?

Reply With Quote
  #9  
Old February 11th, 2003, 09:40 AM
ramz ramz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Juiz de Fora - MG- Brazil
Posts: 93 ramz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via ICQ to ramz Send a message via MSN to ramz
The $cxn is the holder of the database connection.
The problem in the db.php file is that the database connectio function - ConnectToDb - is returning a boolean true/false value, instead of returning the pointer to the database connection.

The difference between that code and yours is that with that code you could create a new connection to the database wherever you want. For example, if you have a function that you need to query data from the database inside that function, you'll need only to do:

$new_conn = ConnectToDb("server","user","pass","database");

The solution to that function is what I've posted before. You should do this:

function ConnectToDb($server, $user, $pass, $database)
{
$s = @mysql_connect($server, $user, $pass);
if(!$s) { print "Not connected!"; }
if(!@mysql_select_db($database,$s)) {
return false;
} else {
return $s;
}
}

Reply With Quote
  #10  
Old February 11th, 2003, 09:46 AM
Dr. Lundy Dr. Lundy is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Hoek van Holland, The Netherlands
Posts: 4 Dr. Lundy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Post

thanks Ramz for the extra info.

I'm beginning to learn the fun of PHP....

Reply With Quote
  #11  
Old February 14th, 2003, 05:53 PM
javier odio javier odio is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 2 javier odio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
cart

thanks for all your help guys.... my problem was also with the connection of db

Reply With Quote
  #12  
Old February 19th, 2003, 10:44 AM
AK564677 AK564677 is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: uk
Posts: 5 AK564677 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
still got an error

The error shown is

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\public_html\products.php on line 43.

Ive even changes db.php functions to

function ConnectToDb($server, $user, $pass, $database)
{
$s = @mysql_connect($server, $user, $pass);
if(!$s) { print "Not connected!"; }
if(!@mysql_select_db($database,$s)) {
return false;
} else {
return $s;
}
}

as described by ramz but i still get the line 43 error , ive given the db.php code for example. I would appreciate it if anyone could help me with this problem , either by emailing me the corrected php code or just my helping me get rid of the dreaded lin 43 error.
Attached Files
File Type: txt db.txt (986 Bytes, 236 views)

Reply With Quote
  #13  
Old February 19th, 2003, 12:17 PM
ramz ramz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Juiz de Fora - MG- Brazil
Posts: 93 ramz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via ICQ to ramz Send a message via MSN to ramz
Hi!
I was looking at the code and I see that line 43 is the: session_start();.
Can you send the url of the error page or put here the PHP error message?

Reply With Quote
  #14  
Old March 10th, 2003, 05:40 PM
afeltmate afeltmate is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 4 afeltmate User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I'm getting an error, I know it's not a connection issue because my products page works. My Cart page doesn't though;

here is the errors when I click "Add Item"

Warning: Cannot send session cookie - headers already sent by (output started at /home/andy/public_html/cart/db.php:49) in /home/andy/public_html/cart/db.php on line 41

Warning: Cannot send session cache limiter - headers already sent (output started at /home/andy/public_html/cart/db.php:49) in /home/andy/public_html/cart/db.php on line 41

Warning: Cannot add header information - headers already sent by (output started at /home/andy/public_html/cart/db.php:49) in /home/andy/public_html/cart/db.php on line 42

Warning: Supplied argument is not a valid MySQL result resource in /home/andy/public_html/cart/cart.php on line 72
?>


Here is my code for cart.php:

<?php

include("db.php");

switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "update_item":
{
UpdateItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "remove_item":
{
RemoveItem($_GET["id"]);
ShowCart();
break;
}
default:
{
ShowCart();
}
}


function AddItem($itemId, $qty)
{
$result = mysql_query("select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");

$row = mysql_fetch_row($result);
$numRows = $row[0];

if($numRows == 0)
{
// This item doesn't exist in the users cart,
// we will add it with an insert query
@mysql_query("insert into cart(cookieId, itemId, qty) values('" . GetCartId() . "', $itemId, $qty)");
}
else
{
// This item already exists in the users cart,
// we will update it instead
UpdateItem($itemId, $qty);
}
}

function UpdateItem($itemId, $qty)
{
mysql_query("update cart set qty = $qty where cookieId = '" . GetCartId() . "' and itemId = $itemId");
}

function RemoveItem($itemId)
{
mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId");
}


function ShowCart()
{

$result = mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc");

echo $result;

while($row = mysql_fetch_array($result))
{
// Increment the total cost of all items
$totalCost += ($row["qty"] * $row["itemPrice"]);
?>
//HTML begins
<table>
<tr>
<td width="15%" height="25">
<font face="verdana" size="1" color="black">
<select name="<?php echo $row["itemId"]; ?>" onChange="UpdateQty(this)">

<?php

for($i = 1; $i <= 20; $i++)
{
echo "<option ";
if($row["qty"] == $i)
{
echo " SELECTED ";
}
echo ">" . $i . "</option>";
}
?>

</select>
</font>
</td>
<td width="55%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["itemName"]; ?>
</font>
</td>

<td width="20%" height="25">
<font face="verdana" size="1" color="black">
$<?php echo number_format($row["itemPrice"], 2, ".", ","); ?>
</font>
</td>

<td width="10%" height="25">
<font face="verdana" size="1" color="black">
<a href="cart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>">Remove</a>
</font>
</td>
</tr>
</table>
<?php
} //end while

} //end function
?>

?>


Any help would be appreciated.

Reply With Quote