Jump to content

Recommended Posts

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;
    }

 

 

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 by Supervan

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.

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.

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.