• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 470
  • 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
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