nickholt1972 Posted October 6, 2006 Share Posted October 6, 2006 I had a shopping page which was working fine only I didn't like it. So in trying to fix what wasn't broken ... guess what? I broke it.I introduced sessions and shortly afterwards made the short trip to errormessagesvilleMy products.php page starts with the following code[code]<?phpsession_start();if (!isset($_SESSION['cart'])) { $_SESSION['cart'] = array();}if (isset($_GET['buy'])) { // Add item to the end of the $_SESSION['cart'] array $_SESSION['cart'][] = $_GET['buy']; header('location: ' . $_SERVER['PHP_SELF'] . '?' . SID); exit();}?>[/code]then it does some other SELECT FROM related shenanigans, before giving it some of this.. (and its this line which brings about the error)[code]while ($row = mysql_fetch_array($result)) {[/code]within my while statement, I display my product details and include the following link, which is SUPPOSED to add the product id to my array of purchased products.[code]<a href="' . $_SERVER['PHP_SELF'] .'?buy=' . $row['id'] . '">Buy</a>[/code]When I run the page, the number of items in my cart goes up, which is fine. However, I get the following message:[i]Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/.sites/123/site117/web/display2.inc.php on line 3[/i](line 3 is [b]while ($row = mysql_fetch_array($result)) {[/b])What I think i happening is the [b]while ($row...[/b] line is losing track of the preceding SELECT line so there's effectively no data but this only happened since I started piddling about with these pesky sessions.can any one suggest a solution? Quote Link to comment https://forums.phpfreaks.com/topic/23190-sessions-related-error/ Share on other sites More sharing options...
nickholt1972 Posted October 6, 2006 Author Share Posted October 6, 2006 Further to my original post, I think I might not have explained myself too well so i'm posting the complete script (more or less, slightly simplified so you've less to trawl through)[code]<?phpsession_start();if (!isset($_SESSION['cart'])) { $_SESSION['cart'] = array();}if (isset($_GET['buy'])) { // Add item to the end of the $_SESSION['cart'] array $_SESSION['cart'][] = $_GET['buy']; header('location: ' . $_SERVER['PHP_SELF'] . '?' . SID); exit();}?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>apple tree nappies online shop</title></head><body><?phpinclude_once 'dbconnect.inc.php';if ($choice == "allnappies"){$result = @mysql_query('SELECT * FROM stocks WHERE alphatype = "NAPPY" AND display = "yes" ORDER BY id ASC'); }elseif ($choice == "onesize"){$result = @mysql_query('SELECT * FROM stocks WHERE sizetype = "One Size" AND display = "yes" ORDER BY id ASC');}elseif ($choice == "multisize"){$result = @mysql_query('SELECT * FROM stocks WHERE sizetype = "Multi Size" AND display = "yes" ORDER BY id ASC');} // Display the title of each entry with a 'buy' link in a table // THIS FOLLOWING LINE IS THE ONE GIVING ME AN ERROR //while ($row = mysql_fetch_array($result)) {<table class="products"><tr><td>' . $row['alphaname'] . '</td></td><td><a href="' . $_SERVER['PHP_SELF'] .'?buy=' . $row['id'] . '">Buy</a></td></tr></table>'; }?><?php echo count($_SESSION['cart']); ?><p><a href="cart.php">View Cart</a></p></body></html>[/code]I'd really appreciate your input.Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/23190-sessions-related-error/#findComment-105278 Share on other sites More sharing options...
redbullmarky Posted October 7, 2006 Share Posted October 7, 2006 $choice does not appear to get a value from anywhere, effectively meaning non of these:[code]if ($choice == "allnappies"){$result = @mysql_query('SELECT * FROM stocks WHERE alphatype = "NAPPY" AND display = "yes" ORDER BY id ASC'); }elseif ($choice == "onesize"){$result = @mysql_query('SELECT * FROM stocks WHERE sizetype = "One Size" AND display = "yes" ORDER BY id ASC');}elseif ($choice == "multisize"){$result = @mysql_query('SELECT * FROM stocks WHERE sizetype = "Multi Size" AND display = "yes" ORDER BY id ASC');}[/code]will get executed and meaning that $result, which you expect to be the result of a query, will not get set,resulting in the error you've gotten here. it's worth noting that in many cases, using the error suppressor (@) can make debugging pretty tricky. as the error coming back from mysql_query will only be stuff like not being able to connect to the database, or invalid SQL, you'd be better doing a proper check elsewhere - i.e, let the mysql_query's throw errors if they have to, so you can fix them. something like:[code]<?php// first make sure we actually have a choiceif ($choice){ $query = ''; // using switch to simplify all the ifs/elses etc switch ($choice) { case 'allnappies': $query = 'SELECT * FROM stocks WHERE alphatype = "NAPPY" AND display = "yes" ORDER BY id ASC'; break; case 'onesize': $query = 'SELECT * FROM stocks WHERE sizetype = "One Size" AND display = "yes" ORDER BY id ASC'; break; case 'multisize': $query = 'SELECT * FROM stocks WHERE sizetype = "Multi Size" AND display = "yes" ORDER BY id ASC'; break; } // if we have a query, run it. if ($query) { $result = mysql_query($query) or die('Problem with '.$query); // rest of code here... } else { echo 'Invalid choice'; die; }}else{ echo 'No choice specified'; die;}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23190-sessions-related-error/#findComment-105512 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.