Jump to content

Having Trouble With A Query


jlindsey

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.