我正在使用 PHP5 和 MySQL 开发一个网站。我使用 mysqlnd 驱动程序,并且我有一个使用准备好的语句来执行我想要的任何查询的类。
我有一张桌子,用于存储有关照片的信息,例如照片的 ID、标题、拍摄地点的纬度和经度等。
我创建了以下存储过程并根据我在这里找到的内容实现了哈弗辛公式:http ://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL
程序是:
DELIMITER $$
CREATE PROCEDURE imagesNearMe(IN user_lat DOUBLE, IN user_lng DOUBLE, IN distance INT)
READS SQL DATA
BEGIN
DECLARE lat1 DOUBLE;
DECLARE lng1 DOUBLE;
DECLARE lat2 DOUBLE;
DECLARE lng2 DOUBLE;
SET lng1 = user_lng - (distance/ABS(COS(RADIANS(user_lat)) * 69));
SET lng2 = user_lng + (distance/ABS(COS(RADIANS(user_lat)) * 69));
SET lat1 = user_lat - (distance/69);
SET lat2 = user_lat + (distance/69);
SELECT id, title, description, location, lat, lng, user_id,
3956 * 2 * ASIN( SQRT(
POWER( SIN( (user_lat - ABS(lat)) * pi() / 180 / 2), 2) +
COS(user_lat * pi() / 180) * COS( ABS(lat) * pi() * 180) * POWER( SIN( (user_lng - lng) * pi() / 180 / 2), 2) ) ) as distance
FROM images
WHERE (lat IS NOT NULL) AND (lng IS NOT NULL)
AND (lat BETWEEN lat1 AND lat2)
AND (lng BETWEEN lng1 AND lng2)
ORDER BY distance ASC;
END$$
DELIMITER ;
当我从 MySQL 控制台调用它并返回预期的数据时,该过程工作正常。问题是当我尝试从我的 PHP 代码中调用该过程时。由于某种原因,我无法获取该过程返回的数据。
我有以下测试方法:
public function CallProcedure( $procedure, $arguments )
{
print_r($procedure);
echo "<br>*************************<br>";
if( ! $this->_sqlStatement = $this->_dbSession->prepare($procedure))
{
return FALSE;
}
else
{
//Prepare succeeded
if($arguments)
{
//if we have arguments we have to prepare and bind the to the query
//get the bind string
$bindString = array_shift($arguments);
//get references to the parameters passed to the query
$bindParamRefs = array();
foreach($arguments as $key => $value)
{
$bindParamRefs[$key] = &$arguments[$key];
}
//put a reference to the bindString inside the bindParamsRefs array
array_unshift($bindParamRefs, $bindString);
print_r($bindParamRefs);
echo "<br>*************************<br>";
//Bind the params to the query
if(!call_user_func_array(array($this->_sqlStatement, "bind_param"), $bindParamRefs))
{
return FALSE;
}
else
{
print_r($this->_sqlStatement);
echo "<br>*************************<br>";
if( ! $this->_sqlStatement->execute() )
{
return FALSE;
}
else
{
do
{
$this->_sqlStatement->store_result();
print_r($this->_sqlStatement);
echo "<br>*************************<br>";
$resultSet = array();
$resultMetadata = $this->_sqlStatement->result_metadata();
$resultVariables = array();
$resultData = array();
while($resultField = $resultMetadata->fetch_field())
{
$resultVariables[] = &$resultData[$resultField->name];
}
call_user_func_array(array($this->_sqlStatement, "bind_result"), $resultVariables);
$i = 0;
while( $this->_sqlStatement->fetch() )
{
$resultSet[$i] = array();
foreach($resultData as $fieldName => $fieldData)
{
$resultSet[$i][$fieldName] = $fieldData;
}
$i++;
}
print_r($resultSet);
echo "<br>*************************<br>";
print_r($resultMetadata);
echo "<br>*************************<br>";
print_r($resultVariables);
echo "<br>*************************<br>";
print_r($resultData);
echo "<br>*************************<br>";
$this->_sqlStatement->free_result();
}
while($this->_sqlStatement->more_results() && $this->_sqlStatement->next_result());
$this->_sqlStatement->close();
}
}
}
}
}
($this->_dbSession 是 mysqli 的一个实例)
上面代码产生的输出是:
CALL imagesNearMe(?, ?, ?)
*************************
Array ( [0] => ddi [1] => 38.246214 [2] => 38.246214 [3] => 150 )
*************************
mysqli_stmt Object ( [affected_rows] => -1 [insert_id] => 0 [num_rows] => 0 [param_count] => 3 [field_count] => 0 [errno] => 0 [error] => [sqlstate] => 00000 [id] => 1 )
*************************
mysqli_stmt Object ( [affected_rows] => 0 [insert_id] => 0 [num_rows] => 0 [param_count] => 3 [field_count] => 8 [errno] => 0 [error] => [sqlstate] => 00000 [id] => 1 )
*************************
Array ( )
*************************
mysqli_result Object ( [current_field] => 8 [field_count] => 8 [lengths] => [num_rows] => 0 [type] => 1 )
*************************
Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => )
*************************
Array ( [id] => [title] => [description] => [location] => [lat] => [lng] => [user_id] => [distance] => )
*************************
如您所见,即使语句正确执行,也不会返回任何数据,即使通过从 mysql 控制台调用具有完全相同参数的完全相同的过程返回 4 个结果。
我错过了什么吗?我完全迷失在这里。
提前感谢您的帮助。
更新
以下代码:
$dbSession = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT);
if($dbSession->connect_errno)
{
die($dbSession->connect_error);
}
else
{
$query = "CALL imagesNearMe(38.2462140, 21.735098, 50)";
if(!$dbSession->multi_query($query))
{
die($dbSession->error);
}
else
{
do
{
if($result = $dbSession->store_result())
{
var_dump($result->fetch_all());
echo "<br>******************************************************<br>";
$result->free();
}
else
{
die($dbSession->error);
}
}
while($dbSession->more_results() && $dbSession->next_result());
$dbSession->close();
}
}
返回我期望的所有结果,但为什么准备好的语句不起作用?