Escaping from MySQL Query

Can someone please help with the echo statement, I’ve tried to escape out but cant get it to work. need to generate the following line for every recordset returned.

L.marker([35.00,-86.00]).bindLabel(‘Unit:1001’).addTo(map).showLabel(true).bindPopup(‘Active’);

Here is the code.
[php]<?php
date_default_timezone_set(‘America/Chicago’);
// Connects to your Database
mysql_connect(“localhost”, “x”, “x”) or die(mysql_error());
mysql_select_db(“gps”) or die(mysql_error());
$data = mysql_query(“SELECT lat, lng,id,callsign FROM x”)
or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
echo "L.marker([$lat,$lng]).bindLabel(‘Unit:$callsign’).addTo(map).showLabel(true).bindPopup(‘Active’);
}
?>[/php]

This is not for a commercial project its for the local storm spotters / responders.

You’re missing a " at the end of the statement.

A couple of problems, simply putting your code into [ PHP ] tags, you can see that the syntax highlighting has picked up the issue with you string closure (or lack thereof).

Problem 1:
[php]echo "L.marker([$lat,$lng]).bindLabel(‘Unit:$callsign’).addTo(map).showLabel(true).bindPopup(‘Active’);[/php]
You haven’t closed the surrounding speech marks (need " at the end), you should also use concatenation.

Problem 1 - Solution:
[php]echo ‘L.marker([’.$lat.’,’.$lng.’]).bindLabel(“Unit:’.$callsign.’”).addTo(map).showLabel(true).bindPopup(“Active”)’;[/php]

[hr]

Problem 2:
[php]while($info = mysql_fetch_array( $data ))
{
//Here you use $lat, $lng, $callsign … where did these variables come from?
//The only variables you can currently use are $info or $data[/php]

Problem 2 - Solution #1
[php]while($info = mysql_fetch_assoc( $data ))
{
extract($info); //convert array elements into variables ($[key] = [value])
echo ‘L.marker([’.$lat.’,’.$lng.’]).bindLabel(“Unit:’.$callsign.’”).addTo(map).showLabel(true).bindPopup(“Active”)’;
}[/php]

Problem 2 - Solution #2
[php]while($info = mysql_fetch_assoc( $data ))
{
echo ‘L.marker([’.$info[‘lat’].’,’.$info[‘lng’].’]).bindLabel(“Unit:’.$info[‘callsign’].’”).addTo(map).showLabel(true).bindPopup(“Active”)’;
}[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service