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

Convert Query to a Prepared Statement


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?


1 Solution
change the username and password
You have 4 parameters and so they all become Question Marks (?)

$mysqli = new mysqli('localhost', 'username', 'password', 'world');
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
$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);
$stmt->bind_result($d, $la,$lo,$r); 
while($value=$stmt->fetch()) {
	print "$d $la $lo $r<BR>";

Open in new window

DAZTOMKINSAuthor Commented:
Thank you.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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