php - PDO Update Statement Not Working -


i've browsed web quite bit before asking here; noticed people have same problem me, none of answers given others didn't solve problem, so....

i have basic pdo update statement inside public function:

 public function editrank($name, $rank){ $query = "update `chat_mod` set `chat_mod_rank` = :rank `chat_mod_ign` = :username"; $prepare = $this->_db->prepare($query); $array = array(     ':rank'     => $rank,     ':username' => $name );  try {     $prepare->execute($array); } catch (pdoexception $e){     echo 'error: ' . $e->getmessage();     return false; } return true; // if no pdo exception thrown.. 

}

no exception thrown, function returns true; rows not being updated. yes, i've checked rows named , values not nulls.

thanks, tom.

p.s other queries select, add & delete work fine.

you catching pdo exceptions did tell pdo throw them?

to make pdo throw exceptions have configure pdo errmode. note setting mode connection option let pdo throw exceptions on connection errors too, important.
so, here example creating pdo connection right way:

$dsn = "mysql:host=$host;dbname=$db;charset=utf8"; $opt = array(     pdo::attr_errmode            => pdo::errmode_exception,     // other options  ); $pdo = new pdo($dsn, $user, $pass, $opt); 

connecting way, notified of database errors, occurred during query execution. note have able see php errors in general. on live site have peek error logs, so, settings have be

error_reporting(e_all); ini_set('display_errors',0); ini_set('log_errors',1); 

while on local development server it's ok make errors on screen:

error_reporting(e_all); ini_set('display_errors',1); 

and of course should never ever use error suppression operator (@) in front of pdo statements.

also, due many bad examples telling wrap every pdo statement try..catch block, have make distinct note:

do not use try..catch operator echo error message. uncaught exception excellent purpose, act same way other php errors - so, can define behavior using site-wide settings - so, you have error message without useless code. while unconditionally echoed error message may reveal sensitive information potential attacker, yet confuse honest visitor.

  • a custom exception handler added later, not required. new users, recommended use unhandled exceptions, extremely informative, helpful , secure.
  • use try..catch if going handle error - say, rollback transaction.

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -