SirChick Posted October 15, 2007 Share Posted October 15, 2007 I have a query that is not finding the record when one does exist.. no error occurs but the variable when echo'd always equals 0. Not sure why.. This is what i have: $getpaper = "SELECT * FROM useritem WHERE UserID='$_SESSION[Current_User]' And ItemID = '1'" or die(mysql_error()); // Fetch the row from the database if (!($paperrow = mysql_fetch_assoc($GetUserID))) { $TotalPaper = 0; }Else{ $TotalPaper = $paperrow['Quantity']; } $_SESSION[Current_User] is used via a global session carried around via the global include. Quote Link to comment https://forums.phpfreaks.com/topic/73347-query-problem/ Share on other sites More sharing options...
darkfreaks Posted October 15, 2007 Share Posted October 15, 2007 <?php $TotalPaper = 0;/// will always be zero change this line ?> Quote Link to comment https://forums.phpfreaks.com/topic/73347-query-problem/#findComment-370067 Share on other sites More sharing options...
MadTechie Posted October 15, 2007 Share Posted October 15, 2007 $getpaper = "SELECT * FROM useritem WHERE UserID='{$_SESSION['Current_User']}' And ItemID = '1'" or die(mysql_error()); // Fetch the row from the database if (!($paperrow = mysql_fetch_assoc($GetUserID))) //Makes no sense! { $TotalPaper = 0; }else{ $TotalPaper = $paperrow['Quantity']; } Quote Link to comment https://forums.phpfreaks.com/topic/73347-query-problem/#findComment-370070 Share on other sites More sharing options...
Barand Posted October 15, 2007 Share Posted October 15, 2007 @darkfreaks - read the code. If no row returned it sets a default value of 0 Quote Link to comment https://forums.phpfreaks.com/topic/73347-query-problem/#findComment-370071 Share on other sites More sharing options...
kenrbnsn Posted October 15, 2007 Share Posted October 15, 2007 You're not doing any query. Also, if you're only using one field, just retrieve that field. You should be checking to see if any rows are being returned: <?php $query = "SELECT Quantity FROM useritem WHERE UserID='$_SESSION[Current_User]' And ItemID = '1'"; $rs = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error()); if (mysql_num_rows($rs) != 1) $TotalPaper = 0; else { $paperrow = mysql_fetch_assoc($rs); $TotalPaper = $paperrow['Quantity']; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/73347-query-problem/#findComment-370072 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.