php - Automatically query MySQL and output results with AJAX -
i have problem display gps coords automatically on google map. when implement map static works fine, try dynamically. means when new gps coord saved database, map should refresh automatically.
i implemented in 1 .php file. show map coords user must select entry, link entry looks -> http://trackmyrun1.at/index.php?page=show_runs&data1=get&data2=1
<?php $result = mysql_query("select * fifa_gps run_id=".$_get['data2'].""); while ($line = mysql_fetch_array($result)) { $cords[] = "new google.maps.latlng(" . $line['lat'] . ", " . $line['longt'] . "),"; } $cord_start = explode("(", $cords[0]); $cord_pos_start = explode(")", $cord_start[1]); $cord_end = explode("(", $cords[count($cords)-1]); $cord_pos_end = explode(")", $cord_end[1]); echo $sys->overall_distance($_get['data2']); ?> <div id="map-canvas" style="height:400px"/> <script type="text/javascript"> var seconds = 1; var divid = "map-canvas"; var url = "http://localhost/alt/fifa-europaliga/index.php?page=show_runs&data1=get&data2=<?php echo $_get['data2']; ?>"; function refreshdiv(){ // xmlhttprequest object var xmlhttp; try{ xmlhttp=new xmlhttprequest(); // firefox, opera 8.0+, safari } catch (e){ try{ xmlhttp=new activexobject("msxml2.xmlhttp"); // internet explorer } catch (e){ try{ xmlhttp=new activexobject("microsoft.xmlhttp"); } catch (e){ alert("your browser not support ajax."); return false; } } } // timestamp preventing ie caching request fetch_unix_timestamp = function() { return parseint(new date().gettime().tostring().substring(0, 10)) } var timestamp = fetch_unix_timestamp(); var nocacheurl = url+"?t="+timestamp; // code... xmlhttp.onreadystatechange=function(){ if(xmlhttp.readystate==4){ document.getelementbyid(divid).innerhtml=xmlhttp.responsetext; settimeout('refreshdiv()',seconds*1000); } } xmlhttp.open("get",nocacheurl,true); xmlhttp.send(null); } // start refreshing process var seconds; window.onload = function startrefresh(){ settimeout('refreshdiv()',seconds*1000); } <!-- refreshdiv(); // --> </script> <script type="text/javascript"> function initialize() { var mylatlng = new google.maps.latlng(<?php echo $cord_pos_start[0]; ?>); var mapoptions = { zoom: 15, center: mylatlng, maptypeid: google.maps.maptypeid.roadmap }; var map = new google.maps.map(document.getelementbyid("map-canvas"), mapoptions); var flightplancoordinates = [ <?php echo implode('', $cords); ?> ]; var image_start = 'http://labs.google.com/ridefinder/images/mm_20_green.png'; var marker = new google.maps.marker({ position: mylatlng, map: map, title:"start", icon: image_start }); var image_end = 'http://labs.google.com/ridefinder/images/mm_20_red.png'; var endmarker = new google.maps.marker({ position: new google.maps.latlng(<?php echo $cord_pos_end[0]; ?>), map: map, title:"ende", icon: image_end }); var flightpath = new google.maps.polyline({ path: flightplancoordinates, strokecolor: "#ff0000", strokeopacity: 0.5, strokeweight: 5 }); flightpath.setmap(map); } initialize(); </script> ....
before first refresh see map , path on , after 1 refresh error:
warning: mysql_fetch_array() expects parameter 1 resource, boolean given in c:\xampp\htdocs\alt\fifa-europaliga\content\show_runs.php on line 4
notice: undefined variable: cords in c:\xampp\htdocs\alt\fifa-europaliga\content\show_runs.php on line 8
notice: undefined offset: 1 in c:\xampp\htdocs\alt\fifa-europaliga\content\show_runs.php on line 9
notice: undefined variable: cords in c:\xampp\htdocs\alt\fifa-europaliga\content\show_runs.php on line 11
can me?
before can run query, must connect server , select database.
$db = mysql_connect('hostname|ip', 'user', 'password'); mysql_select_db('database_name', $db); $result = mysql_query('select ...', $db);
if query fails, mysql_query return false, explains error expecting resource , getting boolean.
the warning you're getting mysql_fetch_array needing resource means query failing. reasons may fail include:
- not connected correct database
- missing permissions on database
- table doesn't exist, mistyped, etc
- mistyped fields, query, etc. (bad query)
in cases, can check error printing out mysql_error()
.
Comments
Post a Comment