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 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2009
    Posts
    90
    Thanks
    5
    Thanked 0 Times in 0 Posts

    statement to get a value from field on a specific row

    Hi,

    I have a simple banner tracking page and once I have updated the click of the user I need to re direct the user to the website based on the id sent from the banner which will appear in the form of
    trackit.php?id=2

    So id=2 points to the table 'links' and obviously row '2' as from the id query, in the table where the field is link I wish to use a statement to get the corrosponding link for that row, having a bit of trouble on this part getting it to work, table structure as follows

    here's the php as it includes the statments I have tried
    - the +1 click works.. so nearly there

    PHP Code:
    <?php

    // Error reporting:
    error_reporting(E_ALL^E_NOTICE);

    // Including the connection file:
    mysql_connect ('xx.xx.xx.xx.xx''someuser''mypass') ;
    mysql_select_db ('somedb');

        
    /* If the visitor is not a search engine, count the click: */
        
    if(!is_bot())
        
    mysql_query("   INSERT INTO links SET id='".mysql_real_escape_string($_GET['id'])."'
                        ON DUPLICATE KEY UPDATE clicks=clicks+1"
    );

    //   http://community.mybb.com/archive/index.php/thread-13028.html
    $query mysql_query($db"SELECT * FROM links WHERE id = '".mysql_real_escape_string($_GET['id'])."'");
    $rows $db->fetch_array($query);
    $site $row['link'];
    header'Location: '.$site.'') ;
    }
    else   
    header'Location: index.php?contents=error') ;             

    /* Helper functions: */

    function error($str)
    {
        die(
    $str);
    }


    function 
    is_bot()
    {
        
    /* This function will check whether the visitor is a search engine robot */
        
        
    $botlist = array("Teoma""alexa""froogle""Gigabot""inktomi",
        
    "looksmart""URL_Spider_SQL""Firefly""NationalDirectory",
        
    "Ask Jeeves""TECNOSEEK""InfoSeek""WebFindBot""girafabot",
        
    "crawler""www.galaxy.com""Googlebot""Scooter""Slurp",
        
    "msnbot""appie""FAST""WebBug""Spade""ZyBorg""rabaz",
        
    "Baiduspider""Feedfetcher-Google""TechnoratiSnoop""Rankivabot",
        
    "Mediapartners-Google""Sogou web spider""WebAlta Crawler","TweetmemeBot",
        
    "Butterfly","Twitturls","Me.dium","Twiceler");

        foreach(
    $botlist as $bot)
        {
            if(
    strpos($_SERVER['HTTP_USER_AGENT'],$bot)!==false)
            return 
    true;    // Is a bot
        
    }

        return 
    false;   // Not a bot
    }
    ?>

    Hopefully this is the right forum section not the php section!

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Rule #1, no actually it's Rule #0: Gotta check for query errors. If you don't handle the error, it'll happen and you'll never know it. Every time you call mysql_query(), you need to check the return value and provide some means of reporting the error if it happens. Just because the query is working right now, doesn't mean it will always work and when it fails you need to know about it.

    PHP Code:
    $query "   INSERT INTO links SET id='".mysql_real_escape_string($_GET['id'])."'
                        ON DUPLICATE KEY UPDATE clicks=clicks+1"
    ;
    $result mysql_query($query);
    if (!
    $result) {
        die (
    "Query error! Query text: $query<br />Error: ".mysql_error());

    Now then, your second call to mysql_query is passing a variable called $db to it, and I don't see $db defined anywhere. Not to mention, it looks like you are using the syntax for mysqli_query(), not mysql_query(). Also your fetch_array() call looks like you're thinking you're using mysqli, but you're not calling mysqli functions. Did you copy/paste code from somewhere in error?

  • #3
    New Coder
    Join Date
    Feb 2009
    Posts
    90
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Yeah I had another think on what I was actually trying to achieve here and had a look on the php manual - this helped alot http://php.net/manual/en/function.mysql-fetch-row.php

    ended up with this and works just fine

    PHP Code:
    <?php

    // Error reporting:
    error_reporting(E_ALL^E_NOTICE);

    // connection:
    mysql_connect ('xx.xx.xxx.xxx''someuser''somepass') ;
    mysql_select_db ('somedb');

        
    /* If the visitor is not a search engine, count the click: */
        
    if(!is_bot())
        
    mysql_query("   INSERT INTO links SET id='".mysql_real_escape_string($_GET['id'])."'
                        ON DUPLICATE KEY UPDATE clicks=clicks+1"
    );

    /*
    http://php.net/manual/en/function.mysql-fetch-row.php
    */
    $result mysql_query("SELECT id,link FROM links WHERE id = '".mysql_real_escape_string($_GET['id'])."'");
    if (!
    $result) {
    header'Location: index.php?contents=error') ;
    exit;
    }
    $row mysql_fetch_row($result);

    header'Location: '.$row[1].'');

    /* Helper functions: */

    function error($str)
    {
        die(
    $str);
    }


    function 
    is_bot()
    {
        
    /* This function will check whether the visitor is a search engine robot */
        
        
    $botlist = array("Teoma""alexa""froogle""Gigabot""inktomi",
        
    "looksmart""URL_Spider_SQL""Firefly""NationalDirectory",
        
    "Ask Jeeves""TECNOSEEK""InfoSeek""WebFindBot""girafabot",
        
    "crawler""www.galaxy.com""Googlebot""Scooter""Slurp",
        
    "msnbot""appie""FAST""WebBug""Spade""ZyBorg""rabaz",
        
    "Baiduspider""Feedfetcher-Google""TechnoratiSnoop""Rankivabot",
        
    "Mediapartners-Google""Sogou web spider""WebAlta Crawler","TweetmemeBot",
        
    "Butterfly","Twitturls","Me.dium","Twiceler");

        foreach(
    $botlist as $bot)
        {
            if(
    strpos($_SERVER['HTTP_USER_AGENT'],$bot)!==false)
            return 
    true;    // Is a bot
        
    }

        return 
    false;   // Not a bot
    }
    ?>

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You still have a mysql_query() call in there that isn't being checked for errors...


  •  

    Posting Permissions

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