/** * function description * * @param * @return void */ public function execute() { $sql = $this->genSql(); $ret = false; Sp_Log::debug(__CLASS__ . '->' . __FUNCTION__ . ': ' . $sql); $dbh = $this->getDbh(); //var_dump($dbh); $input_parms = $this->getParams(); //var_dump($input_parms); if (is_array($input_parms) && count($input_parms) > 0) { $sth = $dbh->prepare($sql); //print_r($this->parms($sql,$input_parms));exit; if (!$sth) { Sp_Log::warning('table: ' . $this->_table_key . ',' . __CLASS__ . '->' . __FUNCTION__ . ': ' . print_r($sth->errorInfo(), true)); } $ret = $sth->execute($input_parms); if ($ret && in_array($this->_operate, array('UPDATE', 'DELETE'))) { return $sth->rowCount(); } } else { //echo $sql; $ret = $dbh->exec($sql); } if (!$ret) { Sp_Log::warning('table: ' . $this->_table_key . ',' . __CLASS__ . '->' . __FUNCTION__ . ': ' . print_r($dbh->errorInfo(), true)); } if ($ret && $this->_operate == 'INSERT' && $dbh->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql') { $ret = $dbh->lastInsertId(); } return $ret; }
/** * function description * * @param * @return void */ private function getDoSth($count = false) { $sql = $this->genSql($count); Sp_Log::debug(__CLASS__ . '->' . __FUNCTION__ . ': ' . $sql); $dbh = $this->getDbh(); //var_dump($dbh); $input_parms = $this->getParams(); //var_dump($input_parms); if (is_array($input_parms) && count($input_parms) > 0) { $sth = $dbh->prepare($sql); $ret = $sth->execute($input_parms); if (!$ret) { Sp_Log::warning(__CLASS__ . '->' . __FUNCTION__ . ' execute: ' . print_r($sth->errorInfo(), true)); } } else { $sth = $dbh->query($sql); if (!$sth) { Sp_Log::warning(__CLASS__ . '->' . __FUNCTION__ . ' query: ' . print_r($dbh->errorInfo(), true)); } } if (!$sth) { Sp_Log::warning(__CLASS__ . '->' . __FUNCTION__ . ' table : ' . $this->_table . ', : ' . print_r($dbh->errorInfo(), true)); } return $sth; }
/** * 执行一条SQL,并返回 Statment 对象 * * @param string $table_key * @param string $sql * @param array $params * @return object */ public static function query($table_key, $sql, $params = null) { $dbh = self::dbo($table_key); self::injectLog($dbh, $sql); $sql = self::prefixTableName($sql, $dbh->table_prefix); if (is_null($params) || !is_array($params) || count($params) == 0) { $sth = $dbh->query($sql); } else { $sth = $dbh->prepare($sql); if ($sth) { $sth->execute($params); } } if (!$sth) { Sp_Log::warning(__CLASS__ . '::' . __FUNCTION__ . ' : ' . print_r($dbh->errorInfo(), true) . "\n" . $table_key . ': ' . $sql); return false; } return $sth; }