我正在使用PHP5和MySQL开发网站。我使用mysqlnd驱动程序,并且有一个类,使用预处理语句执行任何查询。
我有一张表,存储关于照片的信息,如其id、标题、拍摄地点的纬度和经度等。
我创建了以下存储过程,并根据这里找到的Haversin公式进行了实现:http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL 该存储过程为:
我有一张表,存储关于照片的信息,如其id、标题、拍摄地点的纬度和经度等。
我创建了以下存储过程,并根据这里找到的Haversin公式进行了实现: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();
}
}
返回了我预期的所有结果,但为什么准备好的语句没有起作用?