到目前为止
class PDOExtender
{
private $_DBO;
public function openConnection()
{
$dsn = "mysql:host=".DB_HOST.";dbname=".DB_NAME;
$this->_DBO = new PDO($dsn, DB_USER, DB_PASS, array(PDO::ATTR_PERSISTENT => true));
}
}
我知道这个类实际上并没有扩展 pdo 对象,我不知道为什么我这样调用它。
我的问题
我需要添加此类,以便我可以进行一些查询,尤其是使用准备好的语句。有人可以给我反馈要添加什么来完成这个。
我目前正在从另一个类中调用此类,
$this->_siteRegistry = Registry::singleton();
$this->_siteRegistry->storeObject("PDOExtender", "DBO");
$this->_DBO = $this->_siteRegistry->getObject("DBO");
try {
$this->_DBO->openConnection();
} catch(Exception $e) {
throw new Exception("server");
}
我的旧方法,没有 PDO
在我的旧项目中,我只使用了 mysql,但我自己创建了代码,我确信这不是完成任务的最佳方式,所以我宁愿不使用集成了 pdo 的代码。
就是这个,
public function runQuery($query, $functionArray = array(), $needResults)
{
$stmtParam = "";
$parameters = array();
$results = array();
foreach ($functionArray as $v) {
$stmtParam .= "s";
}
array_unshift($functionArray, $stmtParam);
$stmt = $this->_dbConnection->prepare($query);
call_user_func_array(array($stmt, 'bind_param'), $functionArray);
$stmt->execute();
if($needResults) {
$meta = $stmt->result_metadata();
while ( $field = $meta->fetch_field() ) {
$parameters[] = &$row[$field->name];
}
call_user_func_array(array($stmt, 'bind_result'), $parameters);
while ( $stmt->fetch() ) {
$x = array();
foreach( $row as $key => $val ) {
$x[$key] = $val;
}
$results[] = $x;
}
return $results;
} else return true;
}
完成
我需要帮助将我的旧代码转换为与 pdo 一起使用。
如果有人知道任何展示如何执行此操作的优秀教程,请链接它们,我确实看过但找不到任何。
感谢您的宝贵时间
克里斯
最佳答案
我还有一个用于数据库连接的单例类,您可能需要检查它:
class DBConnection extends Singleton{
private $connexion_pdo=null;
protected function initialize(){
$this->connexion_pdo = null;
try{
$this->connexion_pdo = new PDO(PDO_DSN,PDO_USR,PDO_PSW);
}
catch(PDOException $erreur){
throw new Exception($erreur->getMessage());
}
$this->connexion_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->connexion_pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
}
public function execute($query,$values=array()){
try {
$this->connexion_pdo->beginTransaction();
$prepare_execute = $this->getPDOStatement($query);
$prepare_execute->execute($values);
$this->connexion_pdo->commit();
} catch(PDOException $erreur){
throw new Exception($erreur->getMessage());
}
}
public function executeQuery($query,$values=array()){
try {
$resp = array();
//$this->connexion_pdo->beginTransaction();
$prepare_execute = $this->getPDOStatement($query);
$prepare_execute->execute($values);
$resp = $prepare_execute->fetchAll();
//$this->connexion_pdo->commit();
return $resp;
} catch(PDOException $erreur){
throw new Exception($erreur->getMessage());
}
}
public function getPDOStatement($query){
try {
$prepare_execute = null;
$prepare_execute = $this->connexion_pdo->prepare($query,array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
return $prepare_execute;
} catch(PDOException $erreur){
throw new Exception($erreur->getMessage());
}
}
}
我对 INSERT、DELETE 和 UPDATE 使用“execute()”,对 SELECT 使用“executeQuery()”。我知道这段代码可以改进,但主要思想在那里 =D
我看到您正在使用 while 循环来转换查询结果。我建议您创建另一个类来管理它。必须有一个查询数据库的类,另一个将结果映射到你需要的(数组,对象等)
祝你好运!
关于php - 在对象中使用 PDO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5912667/