• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 462
  • Last Modified:

Convert Query to a Prepared Statement

Hi

I am currently learning to use Prepared Statements in PHP/MySQLi.

I have an existing mysql query that normally works but I want to convert it to a prepared statement query instead.

$qry = "SELECT *,(((acos(sin((".$latitude."*pi()/180)) * sin((`Latitude`*pi()/180))+cos((".$latitude."*pi()/180)) * cos((`Latitude`*pi()/180)) * cos(((".$longitude."- `Longitude`)*pi()/180))))*180/pi())*60*1.1515) as distance FROM properties WHERE distance <= ".$distance;

Can anyone do that for me as I am struggling with it?

Thanks,

Daz
0
DAZTOMKINS
Asked:
DAZTOMKINS
1 Solution
 
ropennerCommented:
change the username and password
You have 4 parameters and so they all become Question Marks (?)

<?PHP
$mysqli = new mysqli('localhost', 'username', 'password', 'world');
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$qry = "SELECT *,(((acos(sin((?*pi()/180)) * sin((`Latitude`*pi()/180))+cos((?*pi()/180)) * cos((`Latitude`*pi()/180)) * cos(((?- `Longitude`)*pi()/180))))*180/pi())*60*1.1515) as distance FROM properties WHERE distance <= ?";
$stmt = $mysqli->prepare($qry);

if ( false===$stmt ) {
  die('prepare() failed: ' . htmlspecialchars($mysqli->error));
}


$stmt->bind_param('dddd',$latitude, $latitude, $longitude, $distance);
$latitude=52.3;
$longitude=112.1234;
$distance=34.3;
$stmt->execute();
$stmt->bind_result($d, $la,$lo,$r); 
while($value=$stmt->fetch()) {
	print "$d $la $lo $r<BR>";
}
?>

Open in new window

0
 
DAZTOMKINSAuthor Commented:
Thank you.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now