Can someone hellp me out here? I keep getting this message:
Warning: Cannot use a scalar value as an array in /data/.../class.cart.php on line 56

Here's the code:

  function addCartItem($name, $color, $size) {
    global $database;

$sql = "SELECT product_id FROM products WHERE product_name='$name' AND product_color='$color' AND product_size='$size' LIMIT 1";
$result = $database->query($sql);
$row = mysql_fetch_array($result);

/* LINE 56 BELOW */  
$_SESSION['cart'][$row['product_id']] = ( $_SESSION['cart'][$row['product_id']] + 1 ); return TRUE; }

    Use [man]is_array/man/[man]var_dump/man to verify that $row and $_SESSION['cart'] are both arrays.

    If $row isn't an array, debug your SQL query.

      Well, I did as you suggested, and I'm totally lost...

      First, I changed the code above to:

        function addCartItem($name, $color, $size) {
          global $database;
      
      $sql = "SELECT product_id FROM products WHERE product_name='$name' AND product_color='$color' AND product_size='$size' LIMIT 1";
      $result = $database->query($sql);
      $row = mysql_fetch_array($result);
      
      if(!isset($_SESSION['cart'])) {
      	$_SESSION['cart'] = '';
      }
      
      if(is_array($row)) {
      	if(is_array($_SESSION['cart'])) {
          $_SESSION['cart'][$row['product_id']] = ( $_SESSION['cart'][$row['product_id']] + 1 );
          return TRUE;
          } else {
      	print_r($_SESSION);
      	echo '<p>';
      	var_dump($_SESSION);
          }
      } else {
      	print_r($_SESSION);
      	echo '<p>';
      	var_dump($_SESSION);
      }
        } 
      
      

      And it output this:

      Array ( [url] => /dev_site/viewcart.php [cart] => ) 
      array(2) { ["url"]=> &string(22) "/dev_site/viewcart.php" ["cart"]=> string(0) "" } 
      

      It works fine on one server, but I move it to a shared hosting environment and this happens...

        is $row returning any errors or even any rows?

        Try the following

        $row = mysql_fetch_array($result) or die( mysql_error() );
        
        print 'Rows Returned: ' . mysql_num_rows( $result );

        if $database->query( ) returns BOOLEAN true or false on success/failure, you may want to try something like

        if( $database->query( $sql ) )
        {
        	// Proceed to set $_SESSION['cart'][$row['product_id']]
        	$row = mysql_fetch_array($result);
        
        // etc
        
        }
        else
        {
        	// Some default Value here
        }
          Write a Reply...