knowj Posted April 23, 2008 Share Posted April 23, 2008 I am currently getting this error within my wishlist class Warning: mysqli::query() [function.mysqli-query]: Couldn't fetch database in /url/class_wishlist.php on line 34 <?php public function __destruct() { $this->items = implode(',', $this->items); //line 34 $this->database->query("UPDATE `members` SET wishlist='".$this->items."' WHERE id='".$_SESSION['uid']."'"); } ?> Can you normally run SQL queries within a destruct? wishlist class: <?php class wishlist extends basket { private $wishlist; private static $instance; private function __construct() { //connect to the database $this->db = database::getinstance(); $this->items = array(); //retrive the users wishlist if ($results = $this->database->query("SELECT `wishlist` FROM `members` WHERE id='".$_SESSION['uid']."'")) { //return the result into $row $row = $results->fetch_array(MYSQLI_ASSOC); //if the wishlist is populated if (!empty($row['wishlist'])) { //explode the wishlist into an array $this->wishlist = explode(',', $row['wishlist']); //loop the exploded wishlist out into the items format foreach ($this->wishlist as $name => $value) { $this->items[$value] = '1'; } } } } public function getinstance() { if(self::$instance === null) { $c = __CLASS__; self::$instance = new $c; } return self::$instance; } //prevent clone public function __clone() { throw new Exception("Cannot clone ".__CLASS__." class"); } public function __destruct() { if ($this->items) { //set the wishlist to an array to ensure it is empty $this->wishlist = array(); //loop through the items object to reformat the array into wishlist foreach ($this->items as $name => $value) { $this->wishlist[] = $name; } //implode the wishlist $this->wishlist = implode(',', $this->wishlist); //save the wishlist to the users wishlist field $query = "UPDATE `members` SET wishlist='".$this->wishlist."' WHERE id='".$_SESSION['uid']."'"; $this->database->query($query); } } } ?> relevant sections of basket class <?php session_start(); class basket { protected $items; private $quantity; protected $database; private static $instance = NULL; private function __construct() { //create an instance of the database class and connect to the database $this->database = database::getinstance(); //load the session items into the property $this->items $this->items = $_SESSION['items']; } public function getinstance() { if(self::$instance === null) { $c = __CLASS__; self::$instance = new $c; } return self::$instance; } //prevent clone public function __clone() { throw new Exception("Cannot clone ".__CLASS__." class"); } /*//// //Add the item to the $basket->items property after checking is valid *///// public function add($id) { //check the value isn't an invalid format if (!preg_match("/^[A-Z0-9]+$/", $id)) { return 0; } //SELECT * FROM `products` WHERE code='$id' if ($results = $this->database->query("SELECT * FROM `products` WHERE id='$id'")); { //if the product is in the database if ($results->num_rows == 1) { //clear the results $results->close(); if ($this->items[$id] < 1) { $this->items[$id] = 1; } return 1; } else { return 0; } } } function remove($id) { if ($this->items[$id]) { unset($this->items[$id]); return 1; } } function getitems() { if ($this->items) { foreach ($this->items as $name => $value) { if (empty($where)) { $where = "'$name'"; } else { $where .= ", '$name'"; } } return $this->database->query("SELECT * FROM `products` WHERE `id` IN ($where)"); } return 0; } public function countitems() { return count($this->items); } function __destruct() { $_SESSION['items'] = $this->items; } } ?> product page calling the wishlist->add() function <?php if ($_POST['wishlist']) { $wishlist = wishlist::getinstance(); if ($wishlist->add($_POST['item'])) { //header("Location: ".$_SERVER['HTTP_REFERER']); exit(); } else { //header("Location: /error/3/"); exit(); } } ?> I have been looking everywhere with no joy Cheers J Link to comment https://forums.phpfreaks.com/topic/102612-mysqliquery-functionmysqli-query-couldnt-fetch-database-on-destructor/ Share on other sites More sharing options...
DarkWater Posted April 23, 2008 Share Posted April 23, 2008 You apparently can't use mysqli_query in a destructor, so call a function in the destructor that does your mysqli_query. =) Link to comment https://forums.phpfreaks.com/topic/102612-mysqliquery-functionmysqli-query-couldnt-fetch-database-on-destructor/#findComment-525514 Share on other sites More sharing options...
knowj Posted April 23, 2008 Author Share Posted April 23, 2008 Tried that and to no avail. I did consider that it was destructing the $this->database instance before it was calling the query but then it would just return a non object error. ??? Link to comment https://forums.phpfreaks.com/topic/102612-mysqliquery-functionmysqli-query-couldnt-fetch-database-on-destructor/#findComment-525526 Share on other sites More sharing options...
knowj Posted April 24, 2008 Author Share Posted April 24, 2008 *bump* any PHP gurus around know whats going on? There seems to be no documentation anywhere and I have spoken to numerous PHP developers and they have never come across it. Link to comment https://forums.phpfreaks.com/topic/102612-mysqliquery-functionmysqli-query-couldnt-fetch-database-on-destructor/#findComment-525871 Share on other sites More sharing options...
knowj Posted April 24, 2008 Author Share Posted April 24, 2008 *bump* :-\ Link to comment https://forums.phpfreaks.com/topic/102612-mysqliquery-functionmysqli-query-couldnt-fetch-database-on-destructor/#findComment-526441 Share on other sites More sharing options...
knowj Posted April 24, 2008 Author Share Posted April 24, 2008 *bump* Surely there must be some form of documentation somewhere of this error :S I will not be beaten by it Link to comment https://forums.phpfreaks.com/topic/102612-mysqliquery-functionmysqli-query-couldnt-fetch-database-on-destructor/#findComment-526631 Share on other sites More sharing options...
DarkWater Posted April 24, 2008 Share Posted April 24, 2008 What does it say when you try to call a class function inside the destructor that does a mysqli query? Is it the same error as when you have it directly in the destructor? Link to comment https://forums.phpfreaks.com/topic/102612-mysqliquery-functionmysqli-query-couldnt-fetch-database-on-destructor/#findComment-526634 Share on other sites More sharing options...
knowj Posted April 25, 2008 Author Share Posted April 25, 2008 ye but it errors on the line the query is called rather than the function call within the destructor Link to comment https://forums.phpfreaks.com/topic/102612-mysqliquery-functionmysqli-query-couldnt-fetch-database-on-destructor/#findComment-526691 Share on other sites More sharing options...
DarkWater Posted April 25, 2008 Share Posted April 25, 2008 That's so strange. Do this: if (is_resource($this->db)) { echo "Connection object remains active."; } else { echo "Connection destroyed."; } Put that in the destructor. I would honestly just make a function to delete everything and call it right before you destroy the object, outside of the destructor. Link to comment https://forums.phpfreaks.com/topic/102612-mysqliquery-functionmysqli-query-couldnt-fetch-database-on-destructor/#findComment-526694 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.