lewy009 Posted April 5, 2012 Share Posted April 5, 2012 I have a problem with the example two:Coffee. I get the following errors with accessing wishlist and cart; wishlist.php An error occurred in script '/home/lewyh/public_html/cart.php' on line 81: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given cart.php An error occurred in script '/home/lewyh/public_html/cart.php' on line 81: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given I have run the 4 procedures in phpmyadmin for each cart and wishlist with no problems but still get the errors. I have checked the database user and allowed all privilages with no luck. I have droped the procedures and run them again with no luck. I have done a search and came accross the same problem but still couldnt seem to fix it. The temp URL is www.spafusion.co.uk Below is the php for cart.php and wishlist.php Cart.php <?php // This file manages the shopping cart. // This script is begun in Chapter 9. // Require the configuration before any PHP code: require ('./includes/config.inc.php'); // Check for, or create, a user session: if (isset($_COOKIE['SESSION'])) { $uid = $_COOKIE['SESSION']; } else { $uid = md5(uniqid('biped',true)); } // Send the cookie: setcookie('SESSION', $uid, time()+(60*60*24*30)); // Include the header file: $page_title = 'Coffee - Your Shopping Cart'; include ('./includes/header.html'); // Require the database connection: require (MYSQL); // Need the utility functions: include ('./includes/product_functions.inc.php'); // If there's a SKU value in the URL, break it down into its parts: if (isset($_GET['sku'])) { list($sp_type, $pid) = parse_sku($_GET['sku']); } if (isset ($pid, $sp_type, $_GET['action']) && ($_GET['action'] == 'add') ) { // Add a new product to the cart: $r = mysqli_query($dbc, "CALL add_to_cart('$uid', '$sp_type', $pid, 1)"); // For debugging purposes: //if (!$r) echo mysqli_error($dbc); } elseif (isset ($sp_type, $pid, $_GET['action']) && ($_GET['action'] == 'remove') ) { // Remove it from the cart. $r = mysqli_query($dbc, "CALL remove_from_cart('$uid', '$sp_type', $pid)"); } elseif (isset ($sp_type, $pid, $_GET['action'], $_GET['qty']) && ($_GET['action'] == 'move') ) { // Move it to the cart. // Determine the quantity: $qty = (filter_var($_GET['qty'], FILTER_VALIDATE_INT, array('min_range' => 1))) ? $_GET['qty'] : 1; // Add it to the cart: $r = mysqli_query($dbc, "CALL add_to_cart('$uid', '$sp_type', $pid, $qty)"); // Remove it from the wish list: $r = mysqli_query($dbc, "CALL remove_from_wish_list('$uid', '$sp_type', $pid)"); } elseif (isset($_POST['quantity'])) { // Update quantities in the cart. // Loop through each item: foreach ($_POST['quantity'] as $sku => $qty) { // Parse the SKU: list($sp_type, $pid) = parse_sku($sku); if (isset($sp_type, $pid)) { // Determine the quantity: $qty = (filter_var($qty, FILTER_VALIDATE_INT, array('min_range' => 0)) !== false) ? $qty : 1; // Update the quantity in the cart: $r = mysqli_query($dbc, "CALL update_cart('$uid', '$sp_type', $pid, $qty)"); } } // End of FOREACH loop. }// End of main IF. // Get the cart contents: $r = mysqli_query($dbc, "CALL get_shopping_cart_contents('$uid')"); if (mysqli_num_rows($r) > 0) { // Products to show! include ('./views/cart.html'); } else { // Empty cart! include ('./views/emptycart.html'); } // Finish the page: include ('./includes/footer.html'); ?> Wishlist.php <?php // This file manages the wish list. // This script is begun in Chapter 9. // Require the configuration before any PHP code: require ('./includes/config.inc.php'); // Check for, or create, a user session: if (isset($_COOKIE['SESSION'])) { $uid = $_COOKIE['SESSION']; } else { $uid = md5(uniqid('biped',true)); } // Send the cookie: setcookie('SESSION', $uid, time()+(60*60*24*30)); // Include the header file: $page_title = 'Coffee - Your Wish List'; include ('./includes/header.html'); // Require the database connection: require (MYSQL); // Need the utility functions: include ('./includes/product_functions.inc.php'); // If there's a SKU value in the URL, break it down into its parts: if (isset($_GET['sku'])) { list($sp_type, $pid) = parse_sku($_GET['sku']); } if (isset ($sp_type, $pid, $_GET['action']) && ($_GET['action'] == 'remove') ) { // Remove it from the wish list. $r = mysqli_query($dbc, "CALL remove_from_wish_list('$uid', '$sp_type', $pid)"); } elseif (isset ($sp_type, $pid, $_GET['action'], $_GET['qty']) && ($_GET['action'] == 'move') ) { // Move it to the wish list. // Determine the quantity: $qty = (filter_var($_GET['qty'], FILTER_VALIDATE_INT, array('min_range' => 1))) ? $_GET['qty'] : 1; // Add it to the wish list: $r = mysqli_query($dbc, "CALL add_to_wish_list('$uid', '$sp_type', $pid, $qty)"); // For debugging purposes: //if (!$r) echo mysqli_error($dbc); // Remove it from the cart: $r = mysqli_query($dbc, "CALL remove_from_cart('$uid', '$sp_type', $pid)"); // For debugging purposes: //if (!$r) echo mysqli_error($dbc); } elseif (isset($_POST['quantity'])) { // Update quantities in the wish list. // Loop through each item: foreach ($_POST['quantity'] as $sku => $qty) { // Parse the SKU: list($sp_type, $pid) = parse_sku($sku); if (isset($sp_type, $pid)) { // Determine the quantity: $qty = (filter_var($qty, FILTER_VALIDATE_INT, array('min_range' => 0)) !== false) ? $qty : 1; // Update the quantity in the wish list: $r = mysqli_query($dbc, "CALL update_wish_list('$uid', '$sp_type', $pid, $qty)"); } } // End of FOREACH loop. }// End of main IF. // Get the wish list contents: $r = mysqli_query($dbc, "CALL get_wish_list_contents('$uid')"); if (mysqli_num_rows($r) > 0) { // Products to show! include ('./views/wishlist.html'); } else { // Empty cart! include ('./views/emptylist.html'); } // Finish the page: include ('./includes/footer.html'); ?> Any help will be greatly appreciated. Lewis Link to comment Share on other sites More sharing options...
margaux Posted April 5, 2012 Share Posted April 5, 2012 It would help to have more information. Which queries are failing? Set up your debugging code to display your queries, then you'll be able to see what values are actually being passed to the stored procedures. Link to comment Share on other sites More sharing options...
1cookie Posted April 5, 2012 Share Posted April 5, 2012 wishlist.php An error occurred in script '/home/lewyh/public_html/cart.php' on line 81: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given Boolean given - you passed FALSE (more likely NULL) into the stored procedure (query) when you should have passed a mysqli_result. Check the stored procedure for syntax errors. Link to comment Share on other sites More sharing options...
lewy009 Posted April 6, 2012 Author Share Posted April 6, 2012 Thanks for the reply I am guessing the problem is one of the procedures for each the wishlist and cart Below are the four procedures i stored in the database for cart. My guess is that get_shopping_cart_contents proecdure is the problem but i cannot seem to figure it out what's wrong. can anyone see any syntax errors with my get_shopping_cart_contents procedure? I copied and pasted the procedures from wordpad when needed. DELIMITER $$ CREATE PROCEDURE update_cart (uid CHAR(32), type VARCHAR(6), pid MEDIUMINT, qty TINYINT) BEGIN IF qty > 0 THEN UPDATE carts SET quantity=qty, date_modified=NOW() WHERE user_session_id=uid AND product_type=type AND product_id=pid; ELSEIF qty = 0 THEN CALL remove_from_cart (uid, type, pid); END IF; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE add_to_cart (uid CHAR(32), type VARCHAR(6), pid MEDIUMINT, qty TINYINT) BEGIN DECLARE cid INT; SELECT id INTO cid FROM carts WHERE user_session_id=uid AND product_type=type AND product_id=pid; IF cid > 0 THEN UPDATE carts SET quantity=quantity+qty, date_modified=NOW() WHERE id=cid; ELSE INSERT INTO carts (user_session_id, product_type, product_id, quantity) VALUES (uid, type, pid, qty); END IF; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE remove_from_cart (uid CHAR(32), type VARCHAR(6), pid MEDIUMINT) BEGIN DELETE FROM carts WHERE user_session_id=uid AND product_type=type AND product_id=pid; END$$ DELIMITER ; DELIMITER $$ CREATE PROCEDURE get_shopping_cart_contents (uid CHAR(32)) BEGIN SELECT CONCAT("O", ncp.id) AS sku, c.quantity, ncc.category, ncp.name, ncp.price, ncp.stock, sales.price AS sale_price FROM carts AS c INNER JOIN non_coffee_products AS ncp ON c.product_id=ncp.id INNER JOIN non_coffee_categories AS ncc ON ncc.id=ncp.non_coffee_category_id LEFT OUTER JOIN sales ON (sales.product_id=ncp.id AND sales.product_type='other' AND ((NOW() BETWEEN sales.start_date AND sales.end_date) OR (NOW() > sales.start_date AND sales.end_date IS NULL)) ) WHERE c.product_type="other" AND c.user_session_id=uid UNION SELECT CONCAT("C", sc.id), c.quantity, gc.category, CONCAT_WS(" - ", s.size, sc.caf_decaf, sc.ground_whole), sc.price, sc.stock, sales.price FROM carts AS c INNER JOIN specific_coffees AS sc ON c.product_id=sc.id INNER JOIN sizes AS s ON s.id=sc.size_id INNER JOIN general_coffees AS gc ON gc.id=sc.general_coffee_id LEFT OUTER JOIN sales ON (sales.product_id=sc.id AND sales.product_type='coffee' AND ((NOW() BETWEEN sales.start_date AND sales.end_date) OR (NOW() > sales.start_date AND sales.end_date IS NULL)) ) WHERE c.product_type="coffee" AND c.user_session_id=uid; END$$ DELIMITER ; Link to comment Share on other sites More sharing options...
Larry Posted April 7, 2012 Share Posted April 7, 2012 I'm afraid 1cookie may have led you astray. If there were a syntax error in the procedure, you would not have been able to create it. How did you run the stored procedure separately (i.e., what specific steps did you take)? Link to comment Share on other sites More sharing options...
margaux Posted April 7, 2012 Share Posted April 7, 2012 It looks like you're getting a mysql error 2 - which means the file can't be opened. For starters check the stored procedure on the database for any extra characters such as ; or / Link to comment Share on other sites More sharing options...
lewy009 Posted April 10, 2012 Author Share Posted April 10, 2012 I'm afraid 1cookie may have led you astray. If there were a syntax error in the procedure, you would not have been able to create it. How did you run the stored procedure separately (i.e., what specific steps did you take)? The steps i took was I opened the sql data in word pad and copied and pasted the sql to create the tables every table was run seperatley. Then i copied and pasted the sample data and run them seperatly. Then for the procedures i copied and pasted them when asked in the book seperatley. so after the tables were made and the sample data was entered i run the following all seperatly Select_categories Select_products select_sale_items Then i updated select_products Then i ran the stored procedures the in the following order; add_to_cart remove_from_cart update_cart get_shopping_cart_contents I run all this through phpmyadmin my host is hostgator. I still cannot find a problem with the stored procedure would it be the stored procedure though if it is a copy and paste from coffee sample? Link to comment Share on other sites More sharing options...
Larry Posted April 16, 2012 Share Posted April 16, 2012 Okay. Did you provide valid data for all of the stored procedures when testing them separately? Link to comment Share on other sites More sharing options...
Antonio Conte Posted April 17, 2012 Share Posted April 17, 2012 And PLEASE use the code tags when providing code. It makes it so much easier to read code when you have tabs in there. if ( code_tags_used($your_post) ) { echo "Good boy, now I might bother to help"; } else { echo "Now I might not bother"; } function code_tags_used($input) { return ( (strpbrk($input, 'code') && strpbrk($input, '/code')) != false ) ? true : false; } Link to comment Share on other sites More sharing options...
rob Posted April 17, 2012 Share Posted April 17, 2012 And PLEASE use the code tags when providing code. It makes it so much easier to read code when you have tabs in there. if ( code_tags_used($your_post) ) { echo "Good boy, now I might bother to help"; } else { echo "Now I might not bother"; } function code_tags_used($input) { return ( (strpbrk($input, 'code') && strpbrk($input, '/code')) != false ) ? true : false; } <smirk> Link to comment Share on other sites More sharing options...
Michael vdP Posted April 17, 2012 Share Posted April 17, 2012 Rob, you forgot the end tag </smirk> ;-) Link to comment Share on other sites More sharing options...
lewy009 Posted April 17, 2012 Author Share Posted April 17, 2012 its working now something to do with the hostgator server i was using. I tried starting again and the stored procedures got corrupted and I couldn't store and new procedures. I done exactly the same on my blue host server and it all worked fine. Link to comment Share on other sites More sharing options...
Larry Posted April 18, 2012 Share Posted April 18, 2012 Glad it's working and thanks for letting us know. Link to comment Share on other sites More sharing options...
Recommended Posts