0

这是正在运行的查询:

SELECT  `fleet_pilots`.`user_id` ,  `pilot_tracker`.`start_time` ,  `fleets`.`fleet_cta` 
FROM  `fleet_pilots` 
LEFT JOIN  `fleets` ON  `fleets`.`fleet_id` =  `fleet_pilots`.`fleet_id` 
LEFT JOIN  `pilot_tracker` ON  `pilot_tracker`.`user_id` =  `fleet_pilots`.`user_id` 
AND  `pilot_tracker`.`end_time` =  '0000-00-00 00:00:00'
WHERE  `fleet_pilots`.`fleet_id` =  '26'
AND  `fleet_pilots`.`user_approved` =  '1'

当我在 mysql 中运行查询时,我没有问题并得到我期望的结果。

当我在php中使用结果时出现问题:

$sql = "SELECT `fleet_pilots`.`user_id`, `pilot_tracker`.`start_time`, `fleets`.`fleet_cta`
        FROM `fleet_pilots`
        LEFT JOIN `fleets` ON `fleets`.`fleet_id` = `fleet_pilots`.`fleet_id`
        LEFT JOIN `pilot_tracker` ON `pilot_tracker`.`user_id` = `fleet_pilots`.`user_id` AND `pilot_tracker`.`end_time` = '0000-00-00 00:00:00'
        WHERE `fleet_pilots`.`fleet_id` = :fleet_id AND `fleet_pilots`.`user_approved` = '1'";
$this->db->query($sql);
$args = array(
    ':fleet_id' => $this->input['fleet_id'],
    );
$this->db->execute($args);

$fleet_users = array();

while ( $row = $this->db->fetch_row() )
{
    $fleet_users[] = $row['user_id'];

    if (isset($row['start_time']) && $row['fleet_cta'])
    {
        $sql = "UPDATE `pilots`
                SET `user_total_points` = `user_total_points` + :user_total_points, `user_points` = `user_points` + :user_points
                WHERE `user_id` = :user_id";
        $this->db->query($sql);
        $args = array(
            ':user_id' => $row['user_id'],
            ':user_points' => $this->conf->Config['point_per_min'] * ((strtotime('now') - strtotime($row['start_time']))/60),
            ':user_total_points' => $this->conf->Config['point_per_min'] * ((strtotime('now') - strtotime($row['start_time']))/60),
            );
        $this->db->execute($args);
    }
}

我收到此错误消息:

<br />
<b>Fatal error</b>:  Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error' in /usr/local/apache2/vhosts/trifectas.org/htdocs/fleetmaster/libs/database.class.php:109
Stack trace:
#0 /usr/local/apache2/vhosts/trifectas.org/htdocs/fleetmaster/libs/database.class.php(109): PDOStatement-&gt;fetch(2)
#1 /usr/local/apache2/vhosts/trifectas.org/htdocs/fleetmaster/libs/common.class.php(1170): Database-&gt;fetch_row()
#2 /usr/local/apache2/vhosts/trifectas.org/htdocs/fleetmaster/libs/common.class.php(1226): Common-&gt;submit_end_fleet()
#3 /usr/local/apache2/vhosts/trifectas.org/htdocs/fleetmaster/libs/common.class.php(225): Common-&gt;process_end_fleet()
#4 /usr/local/apache2/vhosts/trifectas.org/htdocs/fleetmaster/index.php(25): Common-&gt;__construct()
#5 {main}
  thrown in <b>/usr/local/apache2/vhosts/trifectas.org/htdocs/fleetmaster/libs/database.class.php</b> on line <b>109</b><br />

第 1170 行是 while ( $row = $this->db->fetch_row() ) 行。

任何帮助表示赞赏。

编辑:以下更改为我解决了这个问题。

$sql = "SELECT `fleet_pilots`.`user_id`, `pilot_tracker`.`start_time`, `fleets`.`fleet_cta`
        FROM `fleet_pilots`
        LEFT JOIN `fleets` ON `fleets`.`fleet_id` = `fleet_pilots`.`fleet_id`
        LEFT JOIN `pilot_tracker` ON `pilot_tracker`.`user_id` = `fleet_pilots`.`user_id` AND `pilot_tracker`.`end_time` = '0000-00-00 00:00:00'
        WHERE `fleet_pilots`.`fleet_id` = :fleet_id AND `fleet_pilots`.`user_approved` = '1'";
$this->db->query($sql);
$args = array(
    ':fleet_id' => $this->input['fleet_id'],
    );
$this->db->execute($args);
$row = $this->db->fetch_array();

$fleet_users = $result = array();

foreach ( $row AS $key => $value )
{
    $result[$key] = $value;
}

foreach ( $result AS $row )
{
    $fleet_users[] = $row['user_id'];

    if ( isset($row['start_time']) && $row['fleet_cta'] )
    {
        $sql = "UPDATE `pilots`
                SET `user_total_points` = `user_total_points` + :user_total_points, `user_points` = `user_points` + :user_points
                WHERE `user_id` = :user_id";
        $this->db->query($sql);
        $args = array(
            ':user_id' => $row['user_id'],
            ':user_points' => $this->conf->Config['point_per_min'] * ((strtotime('now') - strtotime($row['start_time']))/60),
            ':user_total_points' => $this->conf->Config['point_per_min'] * ((strtotime('now') - strtotime($row['start_time']))/60),
            );
        $this->db->execute($args);
    }
}

这是我的 PDO 包装器。

<?php

defined('IN_APP') || die('Hands off!');

class Database
{
    protected $connection;
    protected $result;
    protected $params;
    protected $executed = false;
    protected $_queryCounter = 0;

    public function __construct($dsn, $username = null, $password = null, $driver_options = null)
    {
        try
        {
            $this->connection = new PDO($dsn, $username, $password, $driver_options);
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch (PDOException $e)
        {
            $this->get_error($e);
        }
    }

    public function query($sql, $params = array())
    {
        $this->result = $this->connection->prepare($sql);
        $this->params = is_array($params) ? $params : array($params);
        $this->executed = false;
    }

    public function bind($pos, $value, $type = null)
    {
        if ( is_null($type) )
        {
            switch (true)
            {
                case is_int($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value):
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                default:
                    $type = PDO::PARAM_STR;
            }
        }

        $this->result->bindValue($pos, $value, $type);
    }

    public function bindParam($pos, $value, $type = null)
    {
        if ( is_null($type) )
        {
            switch (true)
            {
                case is_int($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value):
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                default:
                    $type = PDO::PARAM_STR;
            }
        }

        $this->result->bindParam($pos, $value, $type);
    }

    public function execute($vars = array())
    {
        $this->_queryCounter++;
        $this->executed = true;

        foreach ( $vars as $k => $v )
        {
            $this->bind($k, $v);
        }

        return $this->result->execute();
    }

    public function fetch_array($vars = array())
    {
        if ( !$this->executed )
        {
            $this->execute($vars);
        }

        return $this->result->fetchAll(PDO::FETCH_ASSOC);
    }

    public function fetch_row($vars = array())
    {
        if ( !$this->executed )
        {
            $this->execute($vars);
        }

        return $this->result->fetch(PDO::FETCH_ASSOC);
    }

    public function lastInsertId()
    {
        return $this->connection->lastInsertId();
    }

    public function rowCount($vars = array())
    {
        if ( !$this->executed )
        {
            $this->execute($vars);
        }

        return $this->result->rowCount();
    }

    public function beginTransaction()
    {
        $this->connection->beginTransaction();
    }

    public function commit()
    {
        $this->connection->commit();
    }

    public function queryCounter()
    {
        return $this->_queryCounter;
    }

    public function debugDumpParams()
    {
        return $this->result->debugDumpParams();
    }

    public function get_error($e)
    {
        $this->connection = null;
        die($e->getMessage());
    }

    public function getConnection()
    {
        return $this->connection;
    }

    public function __destruct()
    {
        $this->connection = null;
    }
}
4

1 回答 1

2

PDO::fetchRow不存在。假设您的$this->db对象是一个PDO实例,这是行不通的。PDO::query返回一个PDOStatement结果对象。您需要从中获取行,不是从PDO对象中获取。如果这没有帮助,您需要显示您的数据库类在做什么。

于 2013-04-11T01:33:19.403 回答