Jump to content


Photo

Sessions-related error,


  • Please log in to reply
2 replies to this topic

#1 nickholt1972

nickholt1972
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts
  • LocationBury, Lancs, UK

Posted 06 October 2006 - 04:24 PM

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 errormessagesville

My products.php page starts with the following code

<?php
session_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();
}
?>
then it does some other SELECT FROM related shenanigans, before giving it some of this.. (and its this line which brings about the error)

while ($row = mysql_fetch_array($result)) {

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.

<a href="' . $_SERVER['PHP_SELF'] .'?buy=' . $row['id'] . '">Buy</a>

When I run the page, the number of items in my cart goes up, which is fine. However, I get the following message:

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
(line 3 is while ($row = mysql_fetch_array($result)) {)

What I think i happening is the while ($row... 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?

Nick Holt - Future Guru

#2 nickholt1972

nickholt1972
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts
  • LocationBury, Lancs, UK

Posted 06 October 2006 - 10:15 PM

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)


<?php
session_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>

<?php

include_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>

I'd really appreciate your input.

Thanks.
Nick Holt - Future Guru

#3 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 07 October 2006 - 03:30 PM

$choice does not appear to get a value from anywhere, effectively meaning non of these:

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');}
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:

<?php
// first make sure we actually have a choice
if ($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;
}
?>

"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users