Page 1 of 1

Loop through records if var=var & var2=var2?!?

Posted: Mon Jun 16, 2008 7:59 am
by jmansa
I'm trying to create a script which looks through a db table for a certain uid (it can have several of records) and if that uid is found get just the row with example records=1.

This is what I have done so far:

Code: Select all

$query="SELECT * FROM sponsor WHERE uid='".$uid."'"; 
$result=mysql_query($query) or die("Unable to find requested user");
$currSponsor = mysql_fetch_array($result);
 
$hole = $currSponsor['hole'];
I then need to do this:

Code: Select all

if ($currSponsor['hole'] == '1') { 
echo 'Hole1 here'; ]
if ($currSponsor['hole'] == '2') { 
echo 'Hole2 here'; ]
How do I go about this??? Have hit an wall!

Re: Loop through records if var=var & var2=var2?!?

Posted: Mon Jun 16, 2008 8:20 am
by impulse()
You could add

Code: Select all

WHERE hole IN (1,2)
into your SQL query, if you only needed those out of the database anyway.

Otherwise you could write

Code: Select all

if($currSponsor['hole'] == 1 || $currSponsor['hole'] == 2) { echo "Hole 1/2 here"; }

Re: Loop through records if var=var & var2=var2?!?

Posted: Mon Jun 16, 2008 12:47 pm
by califdon
jmansa wrote:I'm trying to create a script which looks through a db table for a certain uid (it can have several of records) and if that uid is found get just the row with example records=1.

This is what I have done so far:

Code: Select all

$query="SELECT * FROM sponsor WHERE uid='".$uid."'"; 
$result=mysql_query($query) or die("Unable to find requested user");
$currSponsor = mysql_fetch_array($result);
 
$hole = $currSponsor['hole'];
I then need to do this:

Code: Select all

if ($currSponsor['hole'] == '1') { 
echo 'Hole1 here'; ]
if ($currSponsor['hole'] == '2') { 
echo 'Hole2 here'; ]
How do I go about this??? Have hit an wall!
You seem to be saying that the field uid is not unique, which is a serious problem right to begin with. A field that's named "id" implies that it is a unique identifier. If it is not unique, it is misnamed. That said, if there may be multiple records with the same uid and different values of hole, then all you have to do is expand your WHERE clause, using AND. Then you can do whatever you want by testing the value of hole as you cycle through the returned records in a WHILE code block.

Another error in your code is your die() statement. When a query doesn't find any matching records, that's not an SQL error, so the die() would not be triggered. The die() is triggered only when the SQL contains an error, such as a syntax error or when it names a table or field that doesn't exist, etc.

Further, the syntax of your IF statements is invalid. You have no closing curly brackets for the IFs.

I can't figure out what you are trying to do, but possibly it is to show all records with a particular value in the text field uid (not a primary key) and display all that match, but with different action, depending on the value in hole. If that's what you want, it would be done like this:

Code: Select all

$query="SELECT * FROM sponsor WHERE uid='$uid' AND hole IN(1,2)";
$result=mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result)==0) {
    echo "Unable to find requested user";
} else {
    while ( $currSponsor = mysql_fetch_array($result) ) {
        if ( $currSponsor['hole'==1]) {
            echo "Hole1 here";
        }
        if ( $currSponsor['hole'==2]) {
            echo "Hole2 here";
        }
    }
}