mysql_fetch_array(): supplied argument is not a valid MySQL help please?

Discussion in 'PHP' started by swapshop, Dec 20, 2008.

  1. #1
    Found this script by smooth designers but it has a error I cant work out?

    http://www.smoothdesigners.com/2008/05/creating-a-simple-full-screen-visitor-map-with-google-maps/

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/domainname/map/visitormap.php on line 14

    Code is

    require 'config.php';

    mysql_connect(DB_HOST, DB_USER, DB_PASS);
    mysql_select_db(DB_NAME);

    $ip = $_SERVER['REMOTE_ADDR'];
    $userinfo = IPtoCoords($ip);

    $user = mysql_query('SELECT `location` FROM `visitor_map` WHERE `location` = \'' . $userinfo['location'] . '\'');
    $totalusers = mysql_query('SELECT count(id) from visitor_map');
    $totalHits = mysql_query('SELECT sum(hits) from visitor_map');
    $total = mysql_fetch_array($totalusers);
    $thits = mysql_fetch_array($totalHits);
    if(!mysql_fetch_row($user) && $userinfo)
    mysql_query('INSERT INTO `visitor_map` (`location`, `longitude`, `latitude`) VALUES (\'' . $userinfo['location'] . '\', ' . $userinfo['longitude'] . ', ' . $userinfo['latitude'] . ')') or die(mysql_error());
     
    swapshop, Dec 20, 2008 IP
  2. caribbean_hatch

    caribbean_hatch Peon

    Messages:
    19
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It's because it's bad coding. The fact that the sample on their web site doesn't work should tell you something. The table their code creates neglects to create a column called 'hits', so of course querying it with the code will produce an error...
     
    caribbean_hatch, Dec 20, 2008 IP
  3. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Is this line working?
    'SELECT `location` FROM `visitor_map` WHERE `location` = \'' . $userinfo['location'] . '\''
    Code (markup):
    Try replacing it with this:
    "SELECT `location` FROM `visitor_map` WHERE `location` = '".$userinfo['location']."'"
    Code (markup):
    As for the totalHits query - does the field "hits" exist and is it an integer?
     
    Yesideez, Dec 20, 2008 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Try.

    var_dump(mysql_error());
    PHP:
    after the line.
     
    Danltn, Dec 20, 2008 IP
  5. swapshop

    swapshop Peon

    Messages:
    656
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Danltn can you explain better your suggestion?


    Yesideez changed to below is this correct?

    $user = mysql_query("SELECT `location` FROM `visitor_map` WHERE `location` = '".$userinfo['location']."'"


    As for the totalHits query - does the field "hits" exist and is it an integer?

    Sorry I dont know?

    caribbean_hatch So I need to add column called 'hits' ??? Does any one know how to point me in the right direction?


    Ps thanks for all the replies
     
    swapshop, Dec 20, 2008 IP
  6. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    $user = mysql_query("SELECT `location` FROM `visitor_map` WHERE `location` = '".$userinfo['location']."'");
    PHP:
     
    Yesideez, Dec 20, 2008 IP
  7. swapshop

    swapshop Peon

    Messages:
    656
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yesideez $user = mysql_query("SELECT `location` FROM `visitor_map` WHERE `location` = '".$userinfo['location']."'");

    Ok cant see any errors


    I need to add column called 'hits' ???

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/.domainname.com/web-site-design/map/visitormap.php on line 15
     
    swapshop, Dec 20, 2008 IP
  8. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Be warned that when PHP returns a line number for an error the actual cause of the error may actually be earlier in the script.

    caribbean_hatch is right - there's no "hits" field - I've no idea what it should be.
     
    Yesideez, Dec 20, 2008 IP
  9. swapshop

    swapshop Peon

    Messages:
    656
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #9
    true I understand that and yes I need to work out the hits column

    trying

    --
    -- Table structure for table `visitor_map`
    --

    CREATE TABLE IF NOT EXISTS `visitor_map` (
    `id` int(11) NOT NULL auto_increment,
    `hits` int(11) NOT NULL,
    `location` varchar(32) NOT NULL,
    `longitude` float NOT NULL,
    `latitude` float NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

    --
    -- Dumping data for table `visitor_map`
    --
     
    swapshop, Dec 20, 2008 IP
  10. swapshop

    swapshop Peon

    Messages:
    656
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #10
    the other thing I do not understand is this function part in bold

    I have a google map and yahoo map api but..... whats XX is that the starting location?

    function IPtoCoords($ip)
    {
    $dom = new DOMDocument();

    $ipcheck = ip2long($ip);
    if($ipcheck == -1 || $ipcheck === false)
    trigger_error('Invalid IP, what are you doing? :|', E_USER_ERROR);
    else
    $uri = 'http://api.hostip.info/?ip=' . $ip;

    $dom->load($uri);
    $location = (strpos($dom->getElementsByTagName('name')->item(1)->nodeValue, 'Unknown') === false)
    ? $dom->getElementsByTagName('name')->item(1)->nodeValue
    : $dom->getElementsByTagName('countryAbbrev')->item(0)->nodeValue;

    if($location == 'XX')
    return false;
    else
    {
    $dom->load('http://local.yahooapis.com/MapsService/V1/geocode?appid=' . Yahoo_Key . '&location=' . $location);
    $longitude = $dom->getElementsByTagName('Longitude')->item(0)->nodeValue;
    $latitude = $dom->getElementsByTagName('Latitude')->item(0)->nodeValue;
    return array('location' => $location, 'longitude' => $longitude, 'latitude' => $latitude);
    }
    }

    ?>
     
    swapshop, Dec 20, 2008 IP
  11. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Just had another look at the opriginal scripts - these two lines can be removed (add // at the start to disable them)
    //$totalHits = mysql_query('SELECT sum(hits) from visitor_map');
    PHP:
    //$thits = mysql_fetch_array($totalHits);
    PHP:
    That's the only places those two variables (totalhits and thits are being used!)

    Now try the scripts and see if you get any errors.
     
    Yesideez, Dec 20, 2008 IP
  12. swapshop

    swapshop Peon

    Messages:
    656
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #12
    No errors appear so you think its a extra table?

    Added a include on to a page that is getting hits

    called it tracker.php

    require_once('map/tracker.php');

    <?php
    require 'config.php';
    mysql_connect(DB_HOST, DB_USER, DB_PASS);
    mysql_select_db(DB_NAME);
    $ip = $_SERVER['REMOTE_ADDR'];
    $userinfo = IPtoCoords($ip);
    $user = mysql_query('SELECT `location` FROM `visitor_map` WHERE `location` = \'' . $userinfo['location'] . '\'');
    if(!mysql_fetch_row($user) && $userinfo)
    mysql_query('INSERT INTO `visitor_map` (`location`, `longitude`, `latitude`) VALUES (\'' . $userinfo['location'] . '\', ' . $userinfo['longitude'] . ', ' . $userinfo['latitude'] . ')') or die(mysql_error());
    ?>

    this should enter hits/locations etc into the DB right and then from visitormap.php I should now see some pointers?
     
    swapshop, Dec 20, 2008 IP
  13. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #13
    It's not an extra table - might be worth contacting the authors and let them know hits is missing from the table.

    As for the last piece of code you posted - hits isn't being accessed in there either.
     
    Yesideez, Dec 20, 2008 IP
  14. swapshop

    swapshop Peon

    Messages:
    656
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #14
    yes I have posted to the author to see what they say. Thanks for your help :) I will carry on trying to see why it doesnt log any pointers to the map
     
    swapshop, Dec 20, 2008 IP
  15. swapshop

    swapshop Peon

    Messages:
    656
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #15
    swapshop, Dec 20, 2008 IP
  16. swapshop

    swapshop Peon

    Messages:
    656
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #16
    how do you find out if your host allows a script to open a remote file or url?

    failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request

    trying to run this basic function?

    <?php

    function IPtoCoords($ip)
    {
    $dom = new DOMDocument();

    $ipcheck = ip2long($ip);
    if($ipcheck == -1 || $ipcheck === false)
    trigger_error('Invalid IP, what are you doing? :|', E_USER_ERROR);
    else
    $uri = 'http://api.hostip.info/?ip=' . $ip;

    $dom->load($uri);
    $location = (strpos($dom->getElementsByTagName('name')->item(1)->nodeValue, 'Unknown') === false)
    ? $dom->getElementsByTagName('name')->item(1)->nodeValue
    : $dom->getElementsByTagName('countryAbbrev')->item(0)->nodeValue;

    if($location == 'XX')
    return false;
    else
    {
    $dom->load('http://local.yahooapis.com/MapsService/V1/geocode?appid=YahooDemo&location=' . $location);
    $longitude = $dom->getElementsByTagName('Longitude')->item(0)->nodeValue;
    $latitude = $dom->getElementsByTagName('Latitude')->item(0)->nodeValue;
    return array('location' => $location, 'longitude' => $longitude, 'latitude' => $latitude);
    }
    }

    //$ip = $_SERVER['REMOTE_ADDR'];
    $ip = '65.55.232.11';
    $userinfo = IPtoCoords($ip);

    echo $ip;
    echo "<br /><br />";
    echo $userinfo;
    echo "<br /><br />";
    echo $ipcheck;

    ?>
     
    swapshop, Dec 20, 2008 IP
  17. swapshop

    swapshop Peon

    Messages:
    656
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #17
    <?
    /* Your Google Maps API key */
    define('API_KEY', 'mygooglekey');
    /* Your Yahoo Application Key */
    define('Yahoo_Key', 'myYahookey');

    //$ip = $_SERVER['REMOTE_ADDR'];
    $ip = '65.55.232.11';
    //$userinfo = IPtoCoords($ip);

    //function IPtoCoords($ip)
    //{
    $dom = new DOMDocument();

    $ipcheck = ip2long($ip);
    if($ipcheck == -1 || $ipcheck === false)
    trigger_error('Invalid IP, what are you doing? :|', E_USER_ERROR);
    else
    $uri = 'http://api.hostip.info/?ip=' . $ip;

    $dom->load($uri);
    $location = (strpos($dom->getElementsByTagName('name')->item(1)->nodeValue, 'Unknown') === false)
    ? $dom->getElementsByTagName('name')->item(1)->nodeValue
    : $dom->getElementsByTagName('countryAbbrev')->item(0)->nodeValue;

    if($location == 'XX')
    return false;
    else
    {

    $dom->load('http://local.yahooapis.com/MapsService/V1/geocode?appid=' . Yahoo_Key . '&location=' . $location);

    $longitude = $dom->getElementsByTagName('Longitude')->item(0)->nodeValue;

    $latitude = $dom->getElementsByTagName('Latitude')->item(0)->nodeValue;

    //return array('location' => $location, 'longitude' => $longitude, 'latitude' => $latitude);

    //return array('location' => $location, 'longitude' => $longitude, 'latitude' => $latitude);
    }
    //}

    echo $ipcheck;
    echo "<br /><br />";
    echo $uri;
    echo "<br /><br />";
    //echo $dom;
    echo "<br /><br />";
    echo $longitude;
    echo "<br /><br />";
    echo $latitude;
    echo "<br /><br />";
    echo $location;
    echo "<br /><br />";
    echo Yahoo_Key;
    echo "<br /><br />";
    echo $dom;
    ?>

    trying to check this function works but I get

    Catchable fatal error: Object of class DOMDocument could not be converted to string in /home/domainame.com/visitors-map/visitormap.php on line 58

    Line in bold seems to be my issue?

    Output from the echos is

    1094182923
    http://api.hostip.info/?ip=65.55.232.11
    -98.58
    39.83
    US
    vGj0q5fV34FdXkvfhBw62tmyYahooKey-MKCAuj3G30s0iNqlBorNoZ.tLSM

    If I input a line

    http://local.yahooapis.com/MapsServ...w62tmyYahooKey-MKCAuj3G30s0iNqlBorNoZ.tLSM=US


    Catchable fatal error: Object of class DOMDocument could not be converted to string in /domain.com/visitors-map/test100.php on line 57


    result from Yahoo is

    <ResultSet xsi:schemaLocation="urn:yahoo:maps http://api.local.yahoo.com/MapsService/V1/GeocodeResponse.xsd">
    −
    <Result precision="country">
    <Latitude>39.83</Latitude>
    <Longitude>-98.58</Longitude>
    <Address/>
    <City/>
    <State/>
    <Zip/>
    <Country>US</Country>
    </Result>
    </ResultSet>
    −
    <!--
    ws02.search.scd.yahoo.com compressed/chunked Sun Dec 21 01:04:59 PST 2008
    -->
     
    swapshop, Dec 21, 2008 IP
  18. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #18
    SWAPSHOP.. Hey, could you put [ php] [ /php] around your code to make it more readable to us trying to help. Thanks.
     
    exodus, Dec 21, 2008 IP
  19. swapshop

    swapshop Peon

    Messages:
    656
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #19
    sorry worked my issues out free google / yahoo visitor map to those who want it
     
    swapshop, Dec 21, 2008 IP