jlindsey Posted December 18, 2012 Share Posted December 18, 2012 My problem is I am having trouble narrowing down a search in a query. Here is my query below. $query = mysql_query("SELECT * FROM users WHERE FamilyID='$Family' AND username='$user'"); $numrows = mysql_num_rows($query); if ($numrows == 0) { } else $errormsg = "You Have Already Created A Family."; Basically what I need is for the query to find the FamilyID and Username row using the variable $user which is the current user that is logged in. The if statement then asks if the FamilyID row is empty, then proceed, if not then echo out an errormsg. The problem is that $numrows will obviously look at both the FamilyID and username row and will always echo out the errormsg instead of proceeding through the rest of the script. The username field is always going to be filled with data. What I need is a query that will select from the user that is currently logged in but also narrow it down by the FamilyID row. If that FamilyID row is empty, then I want the script to proceed. If not I want it to echo out an errormsg. Any Help would be much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/272145-having-trouble-with-a-query/ Share on other sites More sharing options...
requinix Posted December 18, 2012 Share Posted December 18, 2012 (edited) I'm lost. Under what conditions, dealing with $Family/FamilyID and $user/username, do you want to [a] proceed with whatever or show the error message? Edited December 18, 2012 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/272145-having-trouble-with-a-query/#findComment-1400128 Share on other sites More sharing options...
mrMarcus Posted December 18, 2012 Share Posted December 18, 2012 My problem is I am having trouble narrowing down a search in a query. Here is my query below. $query = mysql_query("SELECT * FROM users WHERE FamilyID='$Family' AND username='$user'"); $numrows = mysql_num_rows($query); if ($numrows == 0) { } else $errormsg = "You Have Already Created A Family."; Basically what I need is for the query to find the FamilyID and Username row using the variable $user which is the current user that is logged in. The if statement then asks if the FamilyID row is empty, then proceed, if not then echo out an errormsg. The problem is that $numrows will obviously look at both the FamilyID and username row and will always echo out the errormsg instead of proceeding through the rest of the script. The username field is always going to be filled with data. What I need is a query that will select from the user that is currently logged in but also narrow it down by the FamilyID row. If that FamilyID row is empty, then I want the script to proceed. If not I want it to echo out an errormsg. Any Help would be much appreciated. I'm also lost. Please talk variable/column names and not "row" and such as your usage is incorrect and hard to follow. The if statement then asks if the FamilyID row is empty, then proceed, if not then echo out an errormsg. The "if" statement is not checking if FamilyID is empty. It is checking for the number of returned records from the query, if any. Your issue would be better straightened out through conditions before executing the query. When you initially login your users, you should then also check for that `FamilyID` value and store it within a $_SESSION variable, just as you (I'm assuming) do with the user ID/name/etc. That way, you can run some conditions before firing the query to check if FamilyID contains a value for that user, and deal with that accordingly. Quote Link to comment https://forums.phpfreaks.com/topic/272145-having-trouble-with-a-query/#findComment-1400130 Share on other sites More sharing options...
Jessica Posted December 18, 2012 Share Posted December 18, 2012 $query = mysql_query("SELECT * FROM users WHERE FamilyID='$Family' AND username='$user'"); In addition to what was already said, typically a "somethingID" would be a number, and should not be in quotes. You should ALSO be using the user ID not a username. Quote Link to comment https://forums.phpfreaks.com/topic/272145-having-trouble-with-a-query/#findComment-1400146 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.