我在PHP中有一个函数(见底部),它查询MySQL数据库。 当我使用以下值时:
使用SQL语句:
SELECT user,
scoreVal AS score,
UNIX_TIMESTAMP(timestamp) AS timestamp
FROM Score
WHERE timestamp >= :from
AND timestamp <= :to
AND map = :map
ORDER BY scoreVal DESC, timestamp ASC
LIMIT :limit
在phpMyAdmin中,我得到以下结果:
然而,PHP PDO返回一个空数组。
到目前为止我尝试调试:
由此,我认为这是函数中占位符的问题,但是我一直无法找到他们失败的原因。这很可能发生在PHP端,因为MySQL没有错误抛出错误文件。
这是我正在使用的函数,传递的变量是:
功能:
/**
* Gets the highscore list for the map in that timespan
* @param integer $map Id of map for which to fetch the highscore
* @param integer $limit Maximum no. of records to fetch
* @param integer $from Timestamp from when to find rank
* @param integer $to Timestamp up to when to find rank
* @return array Array of highscores arranged by rank for the map in the format [{"user"=>$user,"score"=>score,"timestamp" => timestamp}]
*/
function get_highscore_list($map,$limit,$from,$to){
$sql = "SELECT user,scoreVal AS score,UNIX_TIMESTAMP(timestamp) AS timestamp FROM Score WHERE timestamp >= :from AND timestamp <= :to AND map = :map ORDER BY scoreVal DESC, timestamp ASC LIMIT :limit";
if ($to==intval(0)){
$max =1;
$sql = str_replace(":to","NOW()",$sql,$max);
}
try{
$conn = request_connection();
$stmt = $conn->prepare($sql);
$stmt->execute(array(':map'=>$map,':from'=>$from,':limit'=>$limit));
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
}catch(PDOException $e){
$_POST["exception"]=$e;
continue;
}
return $result;
}
EDITS
MySQL表的格式:
我已经尝试输出$ conn-&gt; errorInfo();但是由于没有抛出错误,我得到一个值数组: [00000,NULL,NULL]
request_connection函数只返回此函数的结果,它适用于我的所有其他语句。
/**
* Creates a new PDO connection to the database specified in the configuration file
* @author Ignacy Debicki
* @return PDO A new open PDO connection to the database
*/
function create_connection(){
try {
$config = parse_ini_file('caDB.ini');
$conn = new PDO('mysql' . ':host=' . $config['dbHost'] . ';dbname=' . $config['db'],$config['dbPHPUser'], $config['dbPHPPass']);
date_default_timezone_set($config['dbTimezone']);
return $conn;
} catch(PDOException $e){
throw new Exception("Failed to initiate connection",102,$e);
}
}
由于
答案 0 :(得分:3)
经过几个小时的尝试,我终于找到了解决办法。
我在创建连接时遗漏的两个重要声明是:
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
启用错误报告(请参阅https://stackoverflow.com/a/8776392/2891273)。
一旦我打开它,抓住我的问题很简单,这是因为覆盖了:to参数如果$ to为0,则$conn->execute()
语句中传递的参数数量不匹配sql查询中的参数数量。
我的解决方案是使用$conn->bindValue()
代替每个参数,使用if语句检查是否绑定到:to参数。以下是我的解决方案:
function get_highscore_list($map,$limit,$from,$to){
$sql='SELECT user, scoreVal AS score, UNIX_TIMESTAMP(timestamp) AS timestamp FROM Score WHERE map = :map AND timestamp >= :from AND timestamp <= :to ORDER BY scoreVal DESC, timestamp ASC LIMIT :limit';
if ($to==0){
$sql = str_replace(":to",'CURRENT_TIMESTAMP()',$sql);
}
$conn = request_connection();
$stmt = $conn->prepare($sql);
$stmt->bindValue(':map',$map,PDO::PARAM_INT);
$stmt->bindValue(':from',$from,PDO::PARAM_INT);
if ($to!=0){
$stmt->bindValue(':to',$to,PDO::PARAM_INT);
}
$stmt->bindValue(':limit',$limit,PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}