Jump to content

Supervan

Members
  • Posts

    52
  • Joined

  • Last visited

Everything posted by Supervan

  1. How would I remove an instance of a class inside a array The content is stored inside an array. and the array inside $_SESSION ['cart'] //My Items class variables are protected. class Item { protected $id; protected $name; .... I will have have Multiple instances of $item inside the array. Cant use unset because its protected $item = new Item($result->id, $result->name); $_SESSION['cart'][] = $item;
  2. I think i must use a combination of messages. End-user messages and system PDOExceptions thats get directed to the admins
  3. Hi, could you please help me to simplify my code. Using 2 classes 1. Db Class...........public function query 2. User class ...............public function updatename
  4. Thanks...I should have mentioned... Using 2 classes 1. Db Class...........public function query 2. User class ...............public function updatename
  5. Thanks for responding. Im not getting "throw new Exception('There was a problem updating.');"
  6. Hi, please help. I deliberately made a mistake in my query… can’t get the Exception to display an error message. public function updatename($name = null, $id = null) { if (!$id && $this->isLoggedIn()) { $id = $this->data()->id; } $parms = array(); $parms[] = array(':name', $name, PDO::PARAM_STR); $parms[] = array(':id', $id, PDO::PARAM_INT); if (!$this->_db->query("UPDATE users " . "SET name = :this_variable_not_found" . " WHERE id = :id", $parms)) { throw new Exception('There was a problem updating.'); } } public function query($sql, $data_in = array()) { $this->_error = false; if ($data_in) {// prepared query $this->_query = $this->_pdo->prepare($sql); // this example extends the pdo class foreach ($data_in as $arr) { if (isset($arr[2])) {// type supplied $this->_query->bindValue($arr[0], $arr[1], $arr[2]); } else {// no type supplied $this->_query->bindValue($arr[0], $arr[1]); // defaults to string type } } if ($this->_query->execute()) { $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ); $this->_count = $this->_query->rowCount(); } else { $this->_error = true; } } else {// non-prepared query $this->_query = $this->_pdo->prepare($sql); if ($this->_query->execute()) { $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ); $this->_count = $this->_query->rowCount(); } else { $this->_error = true; } }// code to retrieve the result from the query.... return $this; }
  7. I not fond of repetition in code, any way of using abbreviation shortening the array code. ':id',$idnr,INT ':name',$name1,STR $parms = array(); $parms[] = array(':id',$idnr,PDO::PARAM_INT); $parms[] = array(':name',$name1,PDO::PARAM_STR); Im trying to create a generic class that I can use with all my queries CRUD. Can someone please assist. $idnr = 123; $name1 = "tom"; $parms = array(); $parms[] = array(':id',$idnr,PDO::PARAM_INT); $parms[] = array(':name',$name1,PDO::PARAM_STR); $users = DB::getInstance()->query("SELECT * FROM users WHERE id = :id AND name = :name",$parms); class DB { private static $_instance = null; private $_pdo, $_query, $_error = false, $_results, $_count = 0; private function __construct() { try { $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password')); // echo "connected"; } catch (PDOException $e) { die($e->getMessage()); } } public static function getInstance() { if (!isset(self::$_instance)) { self::$_instance = new DB(); } return self::$_instance; } public function query($sql, $data_in = array()) { $this->_error = false; if ($data_in) {// prepared query $this->_query = $this->_pdo->prepare($sql); // this example extends the pdo class foreach ($data_in as $arr) { if (isset($arr[2])) {// type supplied $this->_query->bindValue($arr[0], $arr[1], $arr[2]); } else {// no type supplied $this->_query->bindValue($arr[0], $arr[1]); // defaults to string type } } if ($this->_query->execute()) { $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ); $this->_count = $this->_query->rowCount(); } else { $this->_error = true; } } else {// non-prepared query $this->_query = $this->_pdo->prepare($sql); if ($this->_query->execute()) { $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ); $this->_count = $this->_query->rowCount(); } else { $this->_error = true; } }// code to retrieve the result from the query.... return $this; } public function results() { return $this->_results; } public function first() { return $this->results()[0]; } public function error() { return $this->_error; } public function count() { return $this->_count; } }
  8. Thanks for your time and feedback. Trying to create generic classes for CRUD operations...
  9. Hi, Im using OOP and PDO prepared statement. Will it be possible to construct a class and shorten the following 3 lines of code, the array portion of the code. A lot of repetition. $parms = array(); $parms[] = array(':id',$idnr,PDO::PARAM_INT); $parms[] = array(':name',$name1,PDO::PARAM_STR); Code Sample $idnr = 123; $name1 = "tom"; $parms = array(); $parms[] = array(':id',$idnr,PDO::PARAM_INT); $parms[] = array(':name',$name1,PDO::PARAM_STR); // then use the $parms array as the second parameter in your query calling statement - $users = DB::getInstance()->query("SELECT * FROM users WHERE id = :id AND name = :name",$parms);
  10. Thanks to all, who have commented... OOP gives structure to php and code reuse ability
  11. Hi I’m looking for a standalone PHP OOP framework or code that follows best practices using - PDO prepared statements - Singleton Design Pattern Not looking for a massive library, something short and sweat straight to the point Any comments, feedback would be appreciated
  12. Ok, how would I protect my site's output from Cross-site scripting? Some example code would be appreciated.
  13. look at this online training, regarding oop http://www.youtube.com/playlist?list=PLfdtiltiRHWF5Rhuk7k4UAU1_yLAZzhWc
  14. Hi Im trying to sanitize user input, i bit corncerned about java script insertion. Using pdo and prepared queries. Thanks
  15. Hi, This escape function will only block quotes How would you stop java script insertion? function escape($string) { return htmlentities($string, ENT_QUOTES, 'UTF-8'); } Thanks
  16. The error im getting. 2002 An attempt was made to access a socket in a way forbidden by its access permissions. If i allow access to sourceforge it then works... I dont like this... :o
  17. Please help. The latest Wampserver (64 bits & PHP 5.5) 2.5 requires authentication on SourceForge before you can open phpmyadmin. Does anyone know why is this required. I blocked access to the internet and phpmyadmin would not start. Any ideas? Thanks
  18. Hi, Any other way to import the native xlsx excel file into PHPmyadmin. I know this function was supported in the older versions prior to phpMyAdmin 3.4.5. I don't want to convert the excel file to csv format. Thanks
  19. This worked like a charm... Thank you so much. Now I need to try and solve the rest. Update/ Insert and delete..
  20. I really appreciated your help. I tried, but missing something. Error... Warning: PDOStatement::bindValue() expects parameter 3 to be long $idnrr = 1; $name1 = "tom12345"; $users = DB::getInstance()->query("SELECT * FROM users WHERE id = :id AND name = :name", array(':id'=>$idnrr,PDO::PARAM_INT,':name'=>$name1,PDO::PARAM_STR)); if ($users->count()) { foreach ($users->results() as $result) { echo $result->name . "<br />"; } } class DB { private static $_instance = null; // use $_ notation for private private $_pdo, $_query, $_error = false, $_results, $_count = 0; public function query($sql, $data_in = array()) { $this->_error = false; if ($data_in) {// prepared query $this->_query = $this->_pdo->prepare($sql); // this example extends the pdo class foreach ($data_in as $arr) { if (isset($arr[2])) {// type supplied $this->_query->bindValue($arr[0], $arr[1], $arr[2]); } else {// no type supplied $this->_query->bindValue($arr[0], $arr[1]); // defaults to string type } } if ($this->_query->execute()) { $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ); $this->_count = $this->_query->rowCount(); } else { $this->_error = true; } } else {// non-prepared query $this->_query = $this->_pdo->prepare($sql); if ($this->_query->execute()) { $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ); $this->_count = $this->_query->rowCount(); } else { $this->_error = true; } }// code to retrieve the result from the query.... return $this; } public function results() { return $this->_results; } public function first() { return $this->_results[0]; } public function error() { return $this->_error; } public function count() { return $this->_count; } }
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.