gpt4 book ai didi

php - PHP 中出现 PDOException,MySQL 中没有错误

转载 作者:行者123 更新时间:2023-11-29 08:33:23 25 4
gpt4 key购买 nike

这是正在运行的查询:

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;
}
}

最佳答案

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

关于php - PHP 中出现 PDOException,MySQL 中没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15939262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com