Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2010
    Posts
    74
    Thanks
    9
    Thanked 0 Times in 0 Posts

    If Else in two different tables

    im trying to get an if else statement that finds out if the field is equal to another field in another table

    sorta like this

    PHP Code:
    if ($field1(of-table-a) == $field1(of-table-b) {
        echo 
    "1";
    } else {
        echo 
    "0";

    can't figure out a way to do this, but im thinking its simple and i just dont know

    thanks

  • #2
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    The way to do this is like this.

    Create queries for the information you want to pull.
    PHP Code:
    $q1 mysql_query("SELECT name FROM users WHERE id='$id'");
    $q1row mysql_fetch_array($q1);

    $q2 mysql_query("SELECT weaponid FROM skills WHERE id='5'");
    $q2row mysql_fetch_array($q2); 
    Now you can use the IF and ELSE statement for two tables.

    PHP Code:
    if (($q1row['name'] == 'Mark') && $q2row['weaponid'] == '71')
    {
    //This matches
    }
    else
    {
    //doesn't matter

    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • Users who have thanked masterofollies for this post:

    thilss0o (03-13-2010)

  • #3
    New Coder
    Join Date
    Feb 2010
    Posts
    74
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Ok yea, I was thinking that but I wasn't sure how to write it. Thanks.

  • #4
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Welcome
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    you can also combine the two queries into one.
    Code:
    SELECT 
        t1.name AS name,
        t2.weaponid AS weaponid
    FROM
        users AS t1
    LEFT JOIN
        skills AS t2
    WHERE
        t1.id = '$id' AND t2.id = 5
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •