Convert Query to a Prepared Statement

Posted on 2011-04-20
Last Modified: 2013-12-12

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?


Question by:DAZTOMKINS
    LVL 8

    Accepted 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


    Author Closing Comment

    Thank you.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now