Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #15253

Re: PDO problem

From Jerry Stuckle <jstucklex@attglobal.net>
Newsgroups comp.lang.php
Subject Re: PDO problem
Date 2015-04-12 12:40 -0400
Organization A noiseless patient Spider
Message-ID <mge74h$ohd$1@dont-email.me> (permalink)
References <865189d4-1ef8-4aca-a619-13b59009061a@googlegroups.com> <ced25579-ca73-4434-9af3-8d9431790398@googlegroups.com>

Show all headers | View raw


On 4/12/2015 9:01 AM, Hiago Vitor wrote:
<snip unrelated code>
> 
> -------connect-------
> 
> public function connect (){
>            try{
>                  $db = new PDO( 'mysql:host='.$this->getHost().
>                                       ';dbname='.$this->getDbname().
>                                       ';port='.$this->getPort(),$this->getUser(),$this->getPassword());
>                  return $db;
>            }
>            catch(EXCEPTION $e){
>                 echo 'Error: ' . $e->getMessage();
>            }
>      }
> ----------------update--------
> 
> public function update($email,$pwd,$id){
>            try {
>                 $sql = "UPDATE conta SET email= ? , pass= ? WHERE id(pk)= ? ";
>                 $query = $this->connect()->prepare($sql);
>                 $query->execute(array($email, $pwd, $id));
>            }
>            catch(PDOException $e) {
>                echo 'Error: ' . $e->getMessage();
>           }
>      }
> ----------new problem---------
> I hava another problem now update dont work, but no erros appear
> 

You're not checking the return value from your $query->execute()
statement.  I suspect you have an error in your SQL statement, and it
doesn't look like PDOStatement::execute() throws an exception (I don't
use PDO, so all I can go from is the doc).

I suspect your problem is in your WHERE clause - what is 'id(pk)'
supposed to be?  Are you trying to identify the column 'id' as a primary
key?  That would be unnecessary (and illegal here).

Check the return value from your $query->execute() call, and if it's
false, call $query->errorInfo() to get the error information.

P.S. You have a couple of basic logic faults in your code here, also.
When you catch an exception, the correct way to handle it is to either
correct the error or skip any processing which depends on the call
completing.

In your connect() function, you catch the exception but only display an
error message.  Then you just return from the function, with no return
value.  This is your first problem - the result of trying to use the
return value from connect() when it fails is undefined.

The other problem is in your update() function, you're just assuming the
connect() function works.  Since you caught any possible exception in
connect(), you won't see another exception from a connection failure.

The easiest way to correct this is to just not catch the exception in
connect().  Then it will get caught in update(), and processing will
stop (you'll get the error message from the catch block here).

Other possibilities include rethrowing the error (not good because
you're really not doing anything with the error in connect()) or
returning false from connect() and testing the return value in update()
before trying to use it.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

PDO problem Hiago Vitor <hiagoiuri@gmail.com> - 2015-04-11 16:18 -0700
  Re: PDO problem Jerry Stuckle <jstucklex@attglobal.net> - 2015-04-11 19:58 -0400
    Re: PDO problem Hiago Vitor <hiagoiuri@gmail.com> - 2015-04-11 17:12 -0700
      Re: PDO problem Jerry Stuckle <jstucklex@attglobal.net> - 2015-04-11 20:17 -0400
        Re: PDO problem Hiago Vitor <hiagoiuri@gmail.com> - 2015-04-11 17:24 -0700
        Re: PDO problem Hiago Vitor <hiagoiuri@gmail.com> - 2015-04-11 17:26 -0700
  Re: PDO problem Hiago Vitor <hiagoiuri@gmail.com> - 2015-04-12 06:01 -0700
    Re: PDO problem Jerry Stuckle <jstucklex@attglobal.net> - 2015-04-12 12:40 -0400
      Re: PDO problem Hiago Vitor <hiagoiuri@gmail.com> - 2015-04-12 10:03 -0700
        Re: PDO problem Jerry Stuckle <jstucklex@attglobal.net> - 2015-04-12 16:34 -0400
          Re: PDO problem Hiago Vitor <hiagoiuri@gmail.com> - 2015-04-13 12:48 -0700

csiph-web