Supervan Posted May 17, 2015 Share Posted May 17, 2015 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; } Quote Link to comment https://forums.phpfreaks.com/topic/296367-can%E2%80%99t-get-the-exception-to-display-an-error-message/ Share on other sites More sharing options...
ginerjm Posted May 17, 2015 Share Posted May 17, 2015 You're not getting YOUR exception message or you are not getting the exception error message? Quote Link to comment https://forums.phpfreaks.com/topic/296367-can%E2%80%99t-get-the-exception-to-display-an-error-message/#findComment-1512111 Share on other sites More sharing options...
Supervan Posted May 18, 2015 Author Share Posted May 18, 2015 (edited) You're not getting YOUR exception message or you are not getting the exception error message? Thanks for responding. Im not getting "throw new Exception('There was a problem updating.');" Edited May 18, 2015 by Supervan Quote Link to comment https://forums.phpfreaks.com/topic/296367-can%E2%80%99t-get-the-exception-to-display-an-error-message/#findComment-1512118 Share on other sites More sharing options...
boompa Posted May 18, 2015 Share Posted May 18, 2015 Assuming the query function being called is the one you posted (which doesn't necessarily make sense because it looks like both of these functions are in the same class, but you're invoking the query function on $this->_db rather than $this), you are returning $this from your query function, which shouldn't be falsy. Quote Link to comment https://forums.phpfreaks.com/topic/296367-can%E2%80%99t-get-the-exception-to-display-an-error-message/#findComment-1512127 Share on other sites More sharing options...
ginerjm Posted May 18, 2015 Share Posted May 18, 2015 Your call to a 'query' function is not going to return an exception, as boompa tried to say. The 'query' function of yours returns the object (??). Try adding your exception check to the actual query execution statement if you want to see the exception. Kind of a lot of code to perform a simple pdo query. Quote Link to comment https://forums.phpfreaks.com/topic/296367-can%E2%80%99t-get-the-exception-to-display-an-error-message/#findComment-1512133 Share on other sites More sharing options...
CroNiX Posted May 18, 2015 Share Posted May 18, 2015 Also, the PDOExceptions would probably be more useful info than your custom ones. Quote Link to comment https://forums.phpfreaks.com/topic/296367-can%E2%80%99t-get-the-exception-to-display-an-error-message/#findComment-1512143 Share on other sites More sharing options...
Supervan Posted May 18, 2015 Author Share Posted May 18, 2015 Assuming the query function being called is the one you posted (which doesn't necessarily make sense because it looks like both of these functions are in the same class, but you're invoking the query function on $this->_db rather than $this), you are returning $this from your query function, which shouldn't be falsy. Thanks...I should have mentioned... Using 2 classes 1. Db Class...........public function query 2. User class ...............public function updatename Quote Link to comment https://forums.phpfreaks.com/topic/296367-can%E2%80%99t-get-the-exception-to-display-an-error-message/#findComment-1512169 Share on other sites More sharing options...
Supervan Posted May 18, 2015 Author Share Posted May 18, 2015 Your call to a 'query' function is not going to return an exception, as boompa tried to say. The 'query' function of yours returns the object (??). Try adding your exception check to the actual query execution statement if you want to see the exception. Kind of a lot of code to perform a simple pdo query. 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 Quote Link to comment https://forums.phpfreaks.com/topic/296367-can%E2%80%99t-get-the-exception-to-display-an-error-message/#findComment-1512171 Share on other sites More sharing options...
Supervan Posted May 18, 2015 Author Share Posted May 18, 2015 Also, the PDOExceptions would probably be more useful info than your custom ones. I think i must use a combination of messages. End-user messages and system PDOExceptions thats get directed to the admins Quote Link to comment https://forums.phpfreaks.com/topic/296367-can%E2%80%99t-get-the-exception-to-display-an-error-message/#findComment-1512174 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.