Big problem with MySql => PDO

Hello,

Sorry for my English I use a translator frensh => english:)

I have a problem for convertire my current code MySql in PDO, I want to keep the same structure.

This is my MySql structure:

[php]

<?php class Dbase{ private $_host = "localhost"; private $_user = "root"; private $_password = ""; private $_name = "dbase"; private $_conndb = FALSE; public $_last_query = NULL; public $_affected_rows = 0; public $_insert_keys = array(); public $_insert_values = array(); public $_update_sets = array(); public $_id; public function __construct(){ $this->connect(); } private function connect(){ $this->_conndb = mysql_connect($this->_host,$this->_user,$this->_password); if(!$this->_conndb){ die("Database connection failed:
".mysql_error()); }else{ $_select = mysql_select_db($this->_name,$this->_conndb); if(!$_select){ die("Database selection failed:
".mysql_error()); } } mysql_set_charset("UTF-8", $this->_conndb); } public function close(){ if(!mysql_close($this->_conndb)){ die("Closing connection failed."); } } public function escape($value){ if(function_exists("mysql_real_escape_string")){ if(get_magic_quotes_gpc()){ $value = stripslashes($value); } $value = mysql_real_escape_string($value); }else{ if(!get_magic_quotes_gpc()){ $value = addslashes($value); } } return $value; } public function query($sql){ $this->_last_query = $sql; $result = mysql_query($sql, $this->_conndb); $this->displayQuery($result); return $result; } public function displayQuery($result){ if(!$result){ $output = "Database query failed: ".mysql_error()."
"; $output .= "Last SQL query was: ".$this->_last_query; die($output); }else{ $this->_affected_rows = mysql_affected_rows($this->_conndb); } } public function fetchAll($sql){ $result = $this->query($sql); $out = array(); while($row = mysql_fetch_assoc($result)){ $out[] = $row; } mysql_free_result($result); return $out; } public function fetchOne($sql){ $out = $this->fetchAll($sql); return array_shift($out); } public function lastId(){ return mysql_insert_id($this->_conndb); } } [/php] I am trying to self resolve but and don't kan resolve, i try this but is not work: [php] <?php class Dbase{ private $_pdo_host_dbname = "mysql:host=127.0.0.1;dbname=dbase"; private $_user = "root"; private $_password = ""; private $_conndb = FALSE; public $_last_query = NULL; public $_affected_rows = 0; public $_insert_keys = array(); public $_insert_values = array(); public $_update_sets = array(); public $_id; public function __construct(){ $this->connect(); } private function connect(){ try{ $this->_conndb = new PDO($this->_pdo_host_dbname,$this->_user,$this->_password); } catch { echo 'Connection failed'; } } public function escape($value){ if(get_magic_quotes_gpc()){ $value = stripslashes($value); } $value = $this->_conndb->quote($value); return $value; } public function query($sql){ $this->_last_query = $sql; $result = $this->_conndb->query($sql); $this->displayQuery($result); return $result; } public function displayQuery($result){ if(!$result){ $output = "Database query failed: ".$this->_conndb->errorInfo()."
"; $output .= "Last SQL query was: ".$this->_last_query; die($output); }else{ $this->_affected_rows = $this->_conndb->rowCount($this->_conndb); } } public function fetchAll($sql){ $result = $this->query($sql); $out = array(); while($row = $this->_conndb->fetch(PDO::FETCH_ASSOC)){ $out[] = $row; } return $out; } public function fetchOne($sql){ $out = $this->fetchAll($sql); return array_shift($out); } public function lastId(){ return $this->_conndb->lastInsertId($this->_conndb); } } [/php] Please help me

throw this part of the script out: :o

[php]
public function escape($value){
if(get_magic_quotes_gpc()){
$value = stripslashes($value);
}
$value = $this->_conndb->quote($value);
return $value;
}[/php]

Reason: http://us1.php.net/manual/en/security.magicquotes.php

Thank you, y have thid problem
Fatal error: Call to undefined method PDO::rowCount() in …/classes/Dbase.php on line 42

I make some changes but error report this Fatal error: Call to undefined method PDO::rowCount() in …/Dbase.php on line 42
[php]

<?php class Dbase{ private $_pdo_host_dbname = "mysql:host=127.0.0.1;dbname=dbase"; private $_user = "root"; private $_password = ""; private $_conndb = FALSE; public $_last_query = NULL; public $_affected_rows = 0; public $_insert_keys = array(); public $_insert_values = array(); public $_update_sets = array(); public $_id; public function __construct(){ $this->connect(); } private function connect(){ try{ $this->_conndb = new PDO($this->_pdo_host_dbname,$this->_user,$this->_password); } catch (Exception $e) { echo 'Connection failed: '.$e->getMessage(); } } public function escape($value){ if(get_magic_quotes_gpc()){ $value = stripslashes($value); } $value = $this->_conndb->quote($value); return $value; } public function query($sql){ $this->_last_query = $sql; $result = $this->_conndb->query($sql); $this->displayQuery($result); return $result; } public function displayQuery($result){ if(!$result){ $output = "Database query failed: ".$this->_conndb->errorInfo()."
"; $output .= "Last SQL query was: ".$this->_last_query; die($output); }else{ $this->_affected_rows = $this->_conndb->rowCount($this->_conndb); } } public function fetchAll($sql){ $result = $this->query($sql); $out = array(); while($row = $this->_conndb->fetch(PDO::FETCH_ASSOC)){ $out[] = $row; } return $out; } public function fetchOne($sql){ $out = $this->fetchAll($sql); return array_shift($out); } public function lastId(){ return $this->_conndb->lastInsertId($this->_conndb); } } [/php]

my problem is resolved:

<?php class Dbase{ private $_pdo_host_dbname = "mysql:host=127.0.0.1;dbname=dbase"; private $_user = "root"; private $_password = ""; private $_conndb = FALSE; public $_last_query = NULL; public $_affected_rows = 0; public $_insert_keys = array(); public $_insert_values = array(); public $_update_sets = array(); public $_id; public function __construct(){ $this->connect(); } private function connect(){ try{ $this->_conndb = new PDO($this->_pdo_host_dbname,$this->_user,$this->_password); } catch (Exception $e) { echo 'Connection failed: '.$e->getMessage(); } } public function query($sql){ $this->_last_query = $sql; $result = $this->_conndb->query($sql); $this->displayQuery($result); return $result; } public function displayQuery($result){ if(!$result){ $output = "Database query failed: ".$this->_conndb->errorInfo()."
"; $output .= "Last SQL query was: ".$this->_last_query; die($output); }else{ $this->_affected_rows = $this->_conndb->rowCount($this->_conndb); } } public function fetchAll($sql){ $result = $this->_conndb->query($sql); if ($result) return $result->fetchAll(PDO::FETCH_ASSOC); } public function fetchOne($sql){ $out = $this->fetchAll($sql); return array_shift($out); } public function lastId(){ return $this->_conndb->lastInsertId($this->_conndb); } }
Sponsor our Newsletter | Privacy Policy | Terms of Service