1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Don't know how to make this work

Discussion in 'PHP' started by qwikad.com, Sep 13, 2015.

  1. #1
    $conn = mysql_connect("localhost", "some_user", "password");
    
    if (!$conn) {
        echo "Unable to connect to DB: " . mysql_error();
        exit;
    }
    
    if (!mysql_select_db("some_database")) {
        echo "Unable to select mydbname: " . mysql_error();
        exit;
    }
    
    $sql = "SELECT id FROM  some_table";
    
    $result = mysql_query($sql);
    
    if (!$result) {
        echo "Could not successfully run query ($sql) from DB: " . mysql_error();
        exit;
    }
    
    while ($row = mysql_fetch_assoc($result)) {
    
    echo $row["id"];
    echo "\r\n";
    
    }
    
    mysql_free_result($result);
    Code (markup):
    So it prints the result like this:

    112234 142134 145534 etc.
    SEMrush
    I also have a $row["adid"] that is 112234 but when I try to do this, nothing happens:

    <?php 
    if($row["adid"] == $row["id"]){
    ?>
    DO SOMETHING
    <?php } else { ?>
    DO NOTHING
    <?php
    }
    ?>
    Code (markup):
    Not sure how to make this work.

    PS Yes, I know the function is deprecated.
     
    Solved! View solution.
    qwikad.com, Sep 13, 2015 IP
    SEMrush
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,630
    Likes Received:
    724
    Best Answers:
    152
    Trophy Points:
    470
    #2
    If you're expecting to have row['adid'] from a query where you're asking to only fetch 'id' from the database... then you'll be out of luck. Either you need to specify both columns in the SELECT-query, or you need to rethink how you do things.
    And yes, you should redo the whole mysql_ thing.
     
    PoPSiCLe, Sep 13, 2015 IP
  3. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    5,991
    Likes Received:
    1,125
    Best Answers:
    22
    Trophy Points:
    400
    #3
    Yeah, I am trying to add something new to something that I've had for a while. It's tough since I am not flexible in PHP / MySQL.
     
    Last edited: Sep 14, 2015
    qwikad.com, Sep 13, 2015 IP
  4. #4
    Eh... I'm confused?
    The code you pasted above has one SELECT-statement, which looks for "id". If you're doing another SELECT statement from another table to get the 'adid' one, then you'll have to do two loops / or something else - or join queries.
    Also, if you're gonna intersperse one or more loops with results from two queries, a wise move will be to name the results something different, so as to avoid messing up the content of "$row".

    So - are the 'adid' and 'id' in two different tables?

    You also seem to want to do nothing if there is no match, so why not just check to see if there is a match in the query? First, you do a query for 'adid', and loop through that result. WITHIN that loop, you check to see if the 'id' column matches the result from the 'adid' query. Something like this (pseudocode):
    
    $adid_query = mysql_query("SELECT adid FROM some_table");
    while ($adid = mysql_fetch($adid_query)) {
      $check = $adid['adid'];
      $id_query = mysql_query("SELECT id FROM some_other_table WHERE id = $check");
      // do something
    }
    
    PHP:
     
    PoPSiCLe, Sep 13, 2015 IP
  5. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    5,991
    Likes Received:
    1,125
    Best Answers:
    22
    Trophy Points:
    400
    #5
    Now that you pointed me in the right direction I think I know what needs to be done. Thanks!
     
    qwikad.com, Sep 14, 2015 IP
    PoPSiCLe likes this.
  6. freelanceDeveloper

    freelanceDeveloper Member

    Messages:
    59
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    43
    #6
    Although this does work, I wouldn't put the query in the while loop.
    Get the data needed, store it in 1 (or 2) array(s) and loop the array(s) (or use array_search) to make the comparison.

    or use inner join to retrieve only the records who have a match in both tables.
    
    select A.*,B.*
    from adid A
    inner join othertable B
    on A.ADID = B.SOMETHINGMATCHING
    
    Code (markup):
    maybe with where clause if value comes from form or so (?)
     where A.ADID=$_POST['someIntegerParam'] limit 1
    Code (markup):
    ps : just to illustrate , I know it's not secure etc...
     
    Last edited: Sep 14, 2015
    freelanceDeveloper, Sep 14, 2015 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,630
    Likes Received:
    724
    Best Answers:
    152
    Trophy Points:
    470
    #7
    I wouldn't put the query in the loop either, but using mysql it is the only way. I would use prepared queries and just iterate through the prepared values.
     
    PoPSiCLe, Sep 14, 2015 IP
  8. freelanceDeveloper

    freelanceDeveloper Member

    Messages:
    59
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    43
    #8
    there are other ways... in below (dirty example) you only query the db twice instead of << select count(adid) from some_table >> times

    
    $adid_query = mysql_query("SELECT adid FROM some_table");
    while ($row1 = mysql_fetch($adid_query)) {
           $check = $row1['adid'];
    }
    
    $other_query = mysql_query("SELECT x FROM some_other_table");
    while ($row2 = mysql_fetch($other_query)) {
           $x[] = $row2['x'];
    }
    
    $match = false;
    foreach($check as $val)
    {
          if(array_search($val, $x){
                $match = true;
                break;
          }
    }
    
    if($match){
    
    }
    
    Code (markup):
     
    freelanceDeveloper, Sep 16, 2015 IP