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.

Php extract from mysql... I need help

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

  1. #1
    Hello,

    I need a little help... I try to extract from database mysql... but I have an problem... not work
    Can anyone help me?
    <?php
    $channel_name = $_GET['v'];
    $ads="";
    $result = mysql_query("SELECT * FROM `table` WHERE `channel_name`='{$channel_name}';");
    if (mysql_num_rows($result) > 0) {
    while($row = mysql_fetch_array($result)) {
    $adscode_big = $row["adscode_big"];
    $ads1 = $row["ad1"];
    if($adscode_big == '')
    {
    $ads = $ads1 ;
    }
    else
    {
    $ads = $adscode_big;
    }
    }
    ?>
    I try to extract with condition... what is wrong ?

    Thanks for all sugestion!
     
    Solved! View solution.
    floriano, Aug 30, 2015 IP
  2. #2
    Seriously...
    First: DO NOT USE mysql_query. It's deprecated, and should not be used.
    Second: DO NOT PUT unescaped variables into a query. EVER.
    Third... oh, man.

    Okay, let's for a moment say you had used PDO (a modern DB-class within PHP) to access your database:
    
    <?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:
    This assumes you have a database-object already established named $dbh, and uses the built-in prepared queries methodology to sanitize the content.
    There's no reason to create multiple variables when you can just use the result variables.
    The query above, and the handling, assumes there's only one row to fetch each time - if there are multiple rows, you'll always get the last one in the result set.
    Normally, a PDO-query is run in a try/catch-block - I personally don't do that, since I think it messes up the code too much - this is based on my own implementation, but apart from the missing error-catching, it works just fine.
     
    PoPSiCLe, Aug 30, 2015 IP
  3. floriano

    floriano Well-Known Member

    Messages:
    74
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    113
    #3
    Thanks so much for your help!!

    I have an question, how I can select from two table:

    adscode_small and channel_name is in tabel1
    and ad1 is is tabel2

    how can I do this SELECT ?
     
    floriano, Aug 30, 2015 IP