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.

how I can select from two table ?

Discussion in 'PHP' started by floriano, Aug 30, 2015.

  1. #1
    I have an question, how I can select from two table, Tables unrelated to each other:
    adscode_big and channel_name is in tabel1
    and ad1 is on tabel2

    this is my code:
    <?php
    
    $ads = '';
    
    $query = $dbh->prepare("SELECT * FROM `table` WHERE `channel_name` = :channel_name");
    
    if ($query->execute(array(':channel_name'=>$_GET['v']))) {
       while ($row = $query->fetch()) {
         if (empty($row['adscode_big'])) {
           $ads = $row['ad1'];
         } else {
           $ads = $row['adscode_big'];
         }
       }
    } else {
       //do error-handling here
    }
    
    ?>
    PHP:
    how can I do this SELECT ?

    Thanks in advance!
     
    floriano, Aug 30, 2015 IP
  2. wwfc_barmy_army

    wwfc_barmy_army Well-Known Member

    Messages:
    122
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #2
    If they are unrelated why don't you just do a select query before this one and store the result in a variable and call it on line 10?
     
    wwfc_barmy_army, Aug 31, 2015 IP
    KangBroke likes this.
  3. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #3
    KangBroke, Aug 31, 2015 IP
  4. floriano

    floriano Well-Known Member

    Messages:
    74
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    113
    #4
    Please, you can tall me an example ?
     
    floriano, Aug 31, 2015 IP
  5. wwfc_barmy_army

    wwfc_barmy_army Well-Known Member

    Messages:
    122
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #5
    Well it's pretty much what you've done so far, but again... So if I was doing it I would do something like...
    <?php
    
    //Whatever id you want from table 2
    $id = 1;
    
    if ($stmt = $dbh->prepare("SELECT ad1 FROM table2 WHERE id = ?")) {
    
        /* bind parameters */
        $stmt->bind_param("i", $id);
    
        /* execute query */
        $stmt->execute();
    
        /* bind result variables */
        $stmt->bind_result($ad1);
    
        /* fetch value */
        $stmt->fetch();
    
        /* close statement */
        $stmt->close();
    }
    
    //$ad1 now contains the value from table 2
    //echo $ad1;
    
    $ads = '';
    $channel_name = $_GET['v'];
    
    
    if ($stmt = $dbh->prepare("SELECT adscode_big FROM `table` WHERE `channel_name` = ?")) {
    
        /* bind parameters */
        $stmt->bind_param("s", $channel_name);
    
        /* execute query */
        $stmt->execute();
    
        /* bind result variables */
        $stmt->bind_result($adscode_big);
    
        /* fetch value */
        while ($stmt->fetch()) {
    
            if (empty($adscode_big)) {
                   $ads = $ad1;
            } else {
                $ads = $adscode_big;
            }
    
        }
    
        /* close statement */
        $stmt->close();
    }
    
    
    echo $ads;
    
    ?>
    PHP:
    Hope this helps.
     
    wwfc_barmy_army, Aug 31, 2015 IP
  6. floriano

    floriano Well-Known Member

    Messages:
    74
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    113
    #6
    Thank you for help... what I mistake... files not work ! This is my realy code...

    
    <?php
    include ('configurations.php');
    //Whatever id you want from table 2
    $id = 1;
    
    if ($stmt = $dbh->prepare("SELECT ad1 FROM ads WHERE id = $id")) {
    
        /* bind parameters */
        $stmt->bind_param("i", $id);
    
        /* execute query */
        $stmt->execute();
    
        /* bind result variables */
        $stmt->bind_result($ad1);
    
        /* fetch value */
        $stmt->fetch();
    
        /* close statement */
        $stmt->close();
    }
    
    //$ad1 now contains the value from table 2
    echo $ad1;
    
    $ads = '';
    $channel_name = $_GET['v'];
    
    if ($stmt = $dbh->prepare("SELECT adscode_big FROM `userlogin_tbl` WHERE `channel_name` = $channel_name")) {
    
        /* bind parameters */
        $stmt->bind_param("s", $channel_name);
    
        /* execute query */
        $stmt->execute();
    
        /* bind result variables */
        $stmt->bind_result($adscode_big);
    
        /* fetch value */
        while ($stmt->fetch()) {
    
            if (empty($adscode_big)) {
                   $ads = $ad1;
            } else {
                $ads = $adscode_big;
            }
    
        }
    
        /* close statement */
        $stmt->close();
    }
    
    
    echo $ads;
    
    ?>
    
    PHP:
    Thanks in advance!
     
    floriano, Aug 31, 2015 IP
  7. wwfc_barmy_army

    wwfc_barmy_army Well-Known Member

    Messages:
    122
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #7

    Use the SQL statements with question marks. You then bind the values.

    <?php
    include ('configurations.php');
    //Whatever id you want from table 2
    $id = 1;
    
    if ($stmt = $dbh->prepare("SELECT ad1 FROM ads WHERE id = ?")) {
    
        /* bind parameters */
        $stmt->bind_param("i", $id);
    
        /* execute query */
        $stmt->execute();
    
        /* bind result variables */
        $stmt->bind_result($ad1);
    
        /* fetch value */
        $stmt->fetch();
    
        /* close statement */
        $stmt->close();
    }
    
    //$ad1 now contains the value from table 2
    echo $ad1;
    
    $ads = '';
    $channel_name = $_GET['v'];
    
    if ($stmt = $dbh->prepare("SELECT adscode_big FROM `userlogin_tbl` WHERE `channel_name` = ?")) {
    
        /* bind parameters */
        $stmt->bind_param("s", $channel_name);
    
        /* execute query */
        $stmt->execute();
    
        /* bind result variables */
        $stmt->bind_result($adscode_big);
    
        /* fetch value */
        while ($stmt->fetch()) {
    
            if (empty($adscode_big)) {
                   $ads = $ad1;
            } else {
                $ads = $adscode_big;
            }
    
        }
    
        /* close statement */
        $stmt->close();
    }
    
    
    echo $ads;
    
    ?>
    PHP:
    See the documentation here:
    http://php.net/manual/en/mysqli.prepare.php
     
    wwfc_barmy_army, Aug 31, 2015 IP
  8. floriano

    floriano Well-Known Member

    Messages:
    74
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    113
    #8
    Thanks so much!

    how can I check if the code is working ?
    with isset like this ?

    I try, but not see isset on page...

    {
    if(isset($_GET['v']))
    }
     
    floriano, Aug 31, 2015 IP
  9. wwfc_barmy_army

    wwfc_barmy_army Well-Known Member

    Messages:
    122
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #9
    Not sure what you mean. I guess checked it it's there:

    if(isset($_GET['whatever'])){
    echo "set";
    } else {
    echo "not set";
    }
    PHP:
    Then tweak with the URL index.php?whatever=1 - then try removing it.
     
    wwfc_barmy_army, Aug 31, 2015 IP
  10. floriano

    floriano Well-Known Member

    Messages:
    74
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    113
    #10
    Thanks again for your help !

    I use mysql and server apache on centos 6 VPS...
    can be this as a problem for this code?
    can be an mistake on server settings ?

    not work ...
     
    floriano, Aug 31, 2015 IP
  11. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #11
    You know you'll need an object $dbh (an instantiation of the PDO / mysqli_ class) for it to work, right?
     
    PoPSiCLe, Sep 1, 2015 IP
  12. floriano

    floriano Well-Known Member

    Messages:
    74
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    113
    #12
    No, I dont know
    but, how I can modify my script ... for work it ?

    Thank so much for information !
     
    floriano, Sep 1, 2015 IP
  13. Sugavanas

    Sugavanas Well-Known Member

    Messages:
    686
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    170
    #13
    Why don't you use a script like ezSQL ? It is much more easier for sql queries. You could do two selects to an array and then merge them together. Check php documentations, they have quite number of ways to merge array without changing their keys as well.
     
    Sugavanas, Sep 4, 2015 IP