sevsar Posted March 11, 2014 Share Posted March 11, 2014 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 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:<br />".mysql_error()); }else{ $_select = mysql_select_db($this->_name,$this->_conndb); if(!$_select){ die("Database selection failed:<br />".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()."<br />"; $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); } } I am trying to self resolve but and don't kan resolve, i try this but is not work: <?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()."<br />"; $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); } } Please help me Quote Link to comment Share on other sites More sharing options...
.josh Posted March 11, 2014 Share Posted March 11, 2014 Please explain what is not working. One thing I do see is that you aren't using try..catch properly. At a minimum, catch expects an Exception arg passed to it. Example: private function connect(){ try{ $this->_conndb = new PDO($this->_pdo_host_dbname,$this->_user,$this->_password); } catch (Exception $e) { echo 'Connection failed: '.$e->getMessage(); } } But beyond that, I see some issues that aren't so much issues as code that could be written better, but regardless, I am not going to make wild guesses at what you mean by "not working". You need to provide more details. Quote Link to comment Share on other sites More sharing options...
sevsar Posted March 11, 2014 Author Share Posted March 11, 2014 thank you, now y have this error : Call to undefined method PDO::rowCount() in .../Dbase.php on line 50 Quote Link to comment Share on other sites More sharing options...
sevsar Posted March 11, 2014 Author Share Posted March 11, 2014 I am lost with PDO Quote Link to comment Share on other sites More sharing options...
.josh Posted March 11, 2014 Share Posted March 11, 2014 I understand you are new to PDO. But you need to make an effort to figure it out. We're here to help you when you are making an effort, not spoon feed you. Especially for a "convert to new syntax" type of problem, where the answers are easy to find. google "php pdo version of [insert mysql function here]". The message tells you what the problem is. You are making a call to a method that doesn't exist. google "php pdo version of mysql_affected_rows" and literally the first result is your friend. Quote Link to comment Share on other sites More sharing options...
sevsar Posted March 11, 2014 Author Share Posted March 11, 2014 i try the resolve my problem 1 month but im lost with PDO, thank you for you help i go try the find solution Quote Link to comment Share on other sites More sharing options...
.josh Posted March 11, 2014 Share Posted March 11, 2014 There are literally thousands of links and pages and info out there on this, especially since mysql functions are deprecated and a lot of people are moving away from it. There are even tutorials out there that more or less put it all in one place as a "migration guide" (no, I'm not going to be your personal google service). This is not a language barrier. This is a "level of effort you are willing to make" barrier. But if you really have been trying your hardest and still no joy..no offense, but if it has taken you one month and you still have not figured out how to convert mysql to pdo, perhaps you should consider hiring someone to do it for you? Quote Link to comment Share on other sites More sharing options...
Solution sevsar Posted March 11, 2014 Author Solution Share Posted March 11, 2014 thank you for your suggestion, my probleme is resolved, on personal help me and explain my how it work: <?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()."<br />"; $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); } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.