mike12255 Posted March 19, 2009 Share Posted March 19, 2009 I got a peice of code which in basic is just a function with a query in it. Sometimes the variable $amount is non existant though because the the user does not have the correct session. This is a simple placement error of my if statment im sure, i just cant tell were i should move the else statment, or the if statment. Anyone got ideas? <?php function getqty(){ $cartContent = array(); $sid = session_id(); $pid = $_GET['p']; $pid = mysql_real_escape_string($pid) $sql = "SELECT * FROM tbl_cart WHERE ct_session_id = '$sid' AND pd_id = '$pid'"; if ($result = mysql_query($sql)){ while($row = mysql_fetch_array($result)){ $amount = $row['ct_qty']; } }else{ $amount = "0"; } return $amount; }?> Quote Link to comment Share on other sites More sharing options...
hellonoko Posted March 19, 2009 Share Posted March 19, 2009 Not sure if this make sense but. Say the session is empty or wrong. Then your: if ($result = mysql_query($sql)) Is going to return nothing. And so your return $amount; will return empty? Maybe put in a if ct_session_id == EMPTY or NULL then set a value for $amount. if session != empty do SQL else return $amount = 0; Not sure if that is right or makes sense? Quote Link to comment Share on other sites More sharing options...
hellonoko Posted March 19, 2009 Share Posted March 19, 2009 Forgot to mention you should also add or die(mysql_error()); After your sql statements and any errors will show up and that always helps. Quote Link to comment Share on other sites More sharing options...
mike12255 Posted March 19, 2009 Author Share Posted March 19, 2009 I tried the to things below, just getting the same error. Notice: Undefined variable: amount in /home/schoolw1/public_html/nifty/library/cart-functions.php on line 98 You currently have of Medium Impression Kit in your cart, ** Line 98 is the return amount line <?php function getqty(){ $cartContent = array(); $sid = session_id(); $pid = $_GET['p']; $pid = mysql_real_escape_string($pid) $sql = "SELECT * FROM tbl_cart WHERE ct_session_id = '$sid' AND pd_id = '$pid'"; if(!empty($sid)){ $result = mysql_query($sql); while($row = mysql_fetch_array($result)){ $amount = $row['ct_qty']; }else{ $amount = "0"; } return $amount; }?> <?php function getqty(){ $cartContent = array(); $sid = session_id(); $pid = $_GET['p']; $pid = mysql_real_escape_string($pid) $sql = "SELECT * FROM tbl_cart WHERE ct_session_id = '$sid' AND pd_id = '$pid'"; $result = mysql_query($sql) or die (mysql_error()); while($row = mysql_fetch_array($result)){ $amount = $row['ct_qty']; return $amount; }?> Quote Link to comment Share on other sites More sharing options...
mike12255 Posted March 19, 2009 Author Share Posted March 19, 2009 I just preset $amount and it seems if the amount of rows is non existant it dosnt overwrite the variable: $amount = 0; $result = mysql_query($sql) or die (mysql_error()); while($row = mysql_fetch_array($result)){ $amount = $row['ct_qty']; } return $amount; } Quote Link to comment 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.