Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Tested with ZF 1.12.4-dev, PHP 5.3.26, MySQL 5.6.13</strong></p> <pre><code>Zend_Db_Table::setDefaultAdapter($adapter); $table = new Zend_Db_Table("TableName"); $startLat = 39.0; $startLng = 122.0; $select = $table-&gt;select() -&gt;from($table, array("latitude", "longitude", "distance" =&gt; "SQRT( POW(69.1 * (latitude - $startLat), 2) + POW(69.1 * ($startLng - longitude) * COS(latitude / 57.3), 2))")) -&gt;having("`distance` &lt; ?", 25) -&gt;order("distance"); $rowset = $table-&gt;fetchAll($select); print_r($rowset-&gt;toArray()); </code></pre> <p><strong>Tested with ZF 2.2.5-dev, PHP 5.3.26, MySQL 5.6.13</strong></p> <pre><code>use Zend\Db\TableGateway\TableGateway; use Zend\Db\Sql\Select; use Zend\Db\Sql\Expression; $table = new TableGateway("TableName", $adapter); $startLat = 39.0; $startLng = 122.0; $rowset = $table-&gt;select( function (Select $select) { global $startLat, $startLng; $select-&gt;columns( array( "latitude", "longitude", "distance" =&gt; new Expression("SQRT( POW(69.1 * (latitude - ?), 2) + POW(69.1 * (? - longitude) * COS(latitude / 57.3), 2))", array($startLat, $startLng)) ) ); $select-&gt;having-&gt;lessThan("distance", 25); $select-&gt;order("distance"); } ); print_r($rowset-&gt;toArray()); </code></pre> <p><strong>Test data and test SQL query (without PHP):</strong></p> <pre><code>use test; drop table if exists TableName; create table TableName ( id int auto_increment primary key, latitude numeric(9,4) NOT NULL, longitude numeric(9,4) NOT NULL, key (latitude,longitude) ); insert into TableName (latitude, longitude) values (10.0, 10.0), (20.0, 20.0), (30.0, 30.0), (40.0, 40.0), (50.0, 50.0), (60.0, 60.0), (70.0, 70.0), (39.2, 122.2); set @startlat = 39.0; set @startlng = 122.0; SELECT latitude, longitude, SQRT( POW(69.1 * (latitude - @startlat), 2) + POW(69.1 * (@startlng - longitude) * COS(latitude / 57.3), 2)) AS distance FROM TableName HAVING distance &lt; 25 ORDER BY distance; </code></pre> <p><strong>Output of SQL test:</strong></p> <pre><code>$ mysql -E &lt; 19057187.sql *************************** 1. row *************************** latitude: 39.2000 longitude: 122.2000 distance: 17.484284526375415 </code></pre> <p><strong>Output of PHP test:</strong></p> <pre><code>$ php 19057187.php Array ( [0] =&gt; Array ( [latitude] =&gt; 39.2000 [longitude] =&gt; 122.2000 [distance] =&gt; 17.484284526375415 ) ) Array ( [0] =&gt; Array ( [latitude] =&gt; 39.2000 [longitude] =&gt; 122.2000 [distance] =&gt; 17.48428452637566 ) ) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload