将PHP函数添加到MySQL查询中

将PHP函数添加到MySQL查询中

问题描述:

I have 2 questions from this query.

$query = mysql_query($con, "SELECT id, ".calcDistance($mLat, $mLng, ."lat"., ."lng".)." as distnc FROM users WHERE fName LIKE '%".$search."%' OR lName LIKE '%".$search."%' ORDER BY distnc");

Now before I go on with the questions let me explain my variables and functions.

calcDistance() is a function that returns the distance from 2 latitudes and longitude, actually latitude and longitude of the logged in user and another user. And the parameters passed are the logged in user latitude, logged in user longitude, second user latitude and second user longitude.

$mLat is a variable for the logged in user latitude

$mLng is a variable for the logged in user longitude

lat and lng are column names in the MySQL database table called users.

Now my main aim is to show searched result ordering the result by the nearest user with the search word. Which is the reason I am using the calcDistance() function in the query.

FIRST QUESTION

When I run this query, I get a Parse error: syntax error, unexpected '.'. I don't know why I am getting this error.

SECOND QUESTION

I tried passing numbers directly as the parameters so as to stop the error I get from the 1st question. But I still get another error Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given.

My aim is to order a user search according to the nearest user. Using the calcDistance() returns the distance between the logged in user and another user like I said.

The lat and lng columns are columns where each user latitude and longitude are saved

我有2条来自此查询的问题。 p>

  $ query  = mysql_query($ con,“SELECT id”,。calcDistance($ mLat,$ mLng,。“lat”。,。“lng”。)。“as distnc FROM users WHERE fName LIKE'%”。$ search。“%  '或'lName LIKE'%“。$ search。”%'ORDER BY distnc“); 
  code>  pre> 
 
 

现在在我继续提问之前让我解释一下我的变量 code>和函数 code>。 p>

calcDistance() code>是一个返回2纬度和经度距离的函数 ,实际上是登录用户和另一个用户的纬度和经度。 传递的参数是登录用户纬度 code>,登录用户经度 code>,第二用户纬度 code>和第二用户经度 code >。 p>

$ mLat code>是登录用户纬度的变量 p>

$ mLng code >是登录用户经度的变量 p>

lat code>和 lng code>是MySQL数据库表中名为的列名 用户 code>。 p>

现在我的主要目的是显示搜索结果,由最近的用户用搜索词对结果进行排序。 这是我在查询中使用 calcDistance() code>函数的原因。 p>

第一个问题 strong> p> \ n

当我运行此查询时,我得到 Parse错误:语法错误,意外'。' code>。 我不知道为什么会收到此错误。 p>

第二个问题 strong> p>

我尝试直接传递数字作为 参数,以便停止我从第一个问题得到的错误。 但是我仍然得到另一个错误警告:mysqli_fetch_array()期望参数1是mysqli_result,布尔给定 code>。 p>

我的目标是根据订单进行用户搜索。 最近的用户。 使用 calcDistance() code>返回登录用户与我说的其他用户之间的距离。 p>

lat code>和 lng code>列是保存每个用户纬度和经度的列 p> div>

And after some research, I go my answer for here https://gist.github.com/statickidz/8a2f0ce3bca9badbf34970b958ef8479

METHOD 1

This should roughly sort the items on distance in MySQL, and should work in SQLite. If you need to sort them preciser, you could try using the Pythagorean theorem (a^2 + b^2 = c^2) to get the exact distance.

SELECT * FROM table ORDER BY ((lat-$user_lat)*(lat-$user_lat)) + ((lng - $user_lng)*(lng - $user_lng)) ASC

METHOD 2

SELECT id, (6371 * acos (cos ( radians($user_lat) )* cos(radians( lat ) ) * cos( radians( lng ) - radians($user_lng)) + sin ( radians($user_lat) ) * sin( radians( lat ) ))) AS distance FROM table HAVING distance < 30 ORDER BY distance LIMIT 0 , 20;