|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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:
The full code of the product.php file is attached. |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
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. |
|
#8
|
|||
|
|||
|
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:
and replace then by two siple line like, PHP Code:
Finally I removed from every file the line:, PHP Code:
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? |
|
#9
|
|||
|
|||
|
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; } } |
|
#10
|
|||
|
|||
|
thanks Ramz for the extra info.
I'm beginning to learn the fun of PHP.... ![]() |
|
#11
|
|||
|
|||
|
cart
thanks for all your help guys.... my problem was also with the connection of db
|
|
#12
|
|||
|
|||
|
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. |
|
#13
|
|||
|
|||
|
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? |
|
#14
|
|||
|
|||
|
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. |