- Timestamp:
- Aug 8, 2019 10:36:39 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/PDO.inc.php
r693 r698 192 192 $this->dbh = new \PDO($dsn, $this->getParam('db_user'), $this->getParam('db_pass'), $options); 193 193 } catch (\PDOException $e) { 194 $mysql_error_msg = sprintf('PDOException: (%s) %s', $e->getCode(), $e->getMessage()); 195 // sprintf('PDO error: Could not connect to server (db_server=%s, db_name=%s, db_user=%s, db_pass=%s)', $this->getParam('db_server'), $this->getParam('db_name'), $this->getParam('db_user'), ('' == $this->getParam('db_pass') ? 'NO' : 'YES')); 194 $mysql_error_msg = sprintf('PDO connect %s: %s (db_server=%s, db_name=%s, db_user=%s, db_pass=%s)', 195 get_class($e), 196 $e->getMessage(), 197 $this->getParam('db_server'), 198 $this->getParam('db_name'), 199 $this->getParam('db_user'), 200 ('' == $this->getParam('db_pass') ? 'NO' : 'YES') 201 ); 196 202 $app->logMsg($mysql_error_msg, LOG_EMERG, __FILE__, __LINE__); 197 203 198 204 // Print helpful or pretty error? 199 if ($this->getParam('db_debug')) { 200 echo $mysql_error_msg . "\n"; 205 if ($this->getParam('db_debug') && $app->getParam('display_errors')) { 206 if (!$app->isCLI()) { 207 printf('<pre style="padding:1em;background:#ddd;font:0.9rem monospace;">%s</pre>', $mysql_error_msg); 208 } 201 209 } 202 210 203 211 // Die if db_die_on_failure = true, or just continue without connection. 204 212 return $this->_fail(); 205 206 // throw new \PDOException($e->getMessage(), (int)$e->getCode());207 213 } 208 214 … … 233 239 public function close() 234 240 { 241 $app =& App::getInstance(); 242 235 243 if (!$this->_connected) { 244 $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__); 236 245 return false; 237 246 } … … 268 277 public function ping() 269 278 { 279 $app =& App::getInstance(); 280 270 281 if (!$this->_connected) { 271 return false; 282 $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__); 283 return false; 272 284 } 273 285 … … 362 374 363 375 if (!$this->_connected) { 364 return false; 376 $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__); 377 return false; 365 378 } 366 379 … … 372 385 dump($debugqry, true, SC_DUMP_PRINT_R, __FILE__, __LINE__); 373 386 } else { 374 echo "<!-- ----------------- Query $this->_query_count ---------------------\n$debugqry\n-->\n";387 echo "<!-- ----------------- PDO query $this->_query_count ---------------------\n$debugqry\n-->\n"; 375 388 } 376 389 } … … 387 400 try { 388 401 $stmt = $this->dbh->query($query); 389 } catch (\PDOException $e) { 390 $mysql_error_msg = sprintf('PDOException: (%s) %s', $e->getCode(), $e->getMessage()); 391 $app->logMsg(sprintf('PDOException (%s): %s in query: %s', $e->getCode(), $e->getMessage(), $debugqry), LOG_EMERG, __FILE__, __LINE__); 392 if ($this->getParam('db_debug')) { 402 if (!$stmt) { 403 throw new Exception('PDO::query returned false'); 404 } 405 } catch (\Exception $e) { 406 $app->logMsg(sprintf('PDO query %s (%s): %s in query: %s', get_class($e), $e->getCode(), $e->getMessage(), $debugqry), LOG_EMERG, __FILE__, __LINE__); 407 if ($this->getParam('db_debug') && $app->getParam('display_errors')) { 393 408 if (!$app->isCLI()) { 394 printf('<pre style="padding: 2em; background:#ddd; font:9pxmonospace;">%s<hr>%s</pre>', wordwrap($e->getMessage()), htmlspecialchars($debugqry));409 printf('<pre style="padding:1em;background:#ddd;font:0.9rem monospace;">%s<hr>%s</pre>', wordwrap($e->getMessage()), htmlspecialchars($debugqry)); 395 410 } 396 411 } … … 416 431 417 432 if (!$this->_connected) { 418 return false; 433 $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__); 434 return false; 419 435 } 420 436 … … 423 439 $debugqry = preg_replace("/\n[\t ]+/u", "\n", $query); 424 440 if ($this->getParam('db_always_debug')) { 425 echo "<!-- ----------------- P repare $this->_query_count ---------------------\n$debugqry\n-->\n";441 echo "<!-- ----------------- PDO prepare $this->_query_count ---------------------\n$debugqry\n-->\n"; 426 442 } 427 443 … … 437 453 try { 438 454 $stmt = $this->dbh->prepare($query, ...$params); 455 if (!$stmt) { 456 throw new Exception('PDO::query returned false'); 457 } 439 458 } catch (\PDOException $e) { 440 $mysql_error_msg = sprintf('PDOException: (%s) %s', $e->getCode(), $e->getMessage()); 441 $app->logMsg(sprintf('PDOException (%s): %s in prepare: %s', $e->getCode(), $e->getMessage(), $debugqry), LOG_EMERG, __FILE__, __LINE__); 442 if ($this->getParam('db_debug')) { 459 $app->logMsg(sprintf('PDO prepare %s (%s): %s in query: %s', get_class($e), $e->getCode(), $e->getMessage(), $debugqry), LOG_EMERG, __FILE__, __LINE__); 460 if ($this->getParam('db_debug') && $app->getParam('display_errors')) { 443 461 if (!$app->isCLI()) { 444 printf('<pre style="padding: 2em; background:#ddd; font:9pxmonospace;">%s<hr>%s</pre>', wordwrap($e->getMessage()), htmlspecialchars($debugqry));462 printf('<pre style="padding:1em;background:#ddd;font:0.9rem monospace;">%s<hr>%s</pre>', wordwrap($e->getMessage()), htmlspecialchars($debugqry)); 445 463 } 446 464 } … … 509 527 510 528 if (!$this->_connected) { 529 $app->logMsg(sprintf('No DB connection to run %s', __METHOD__), LOG_NOTICE, __FILE__, __LINE__); 511 530 return false; 512 531 }
Note: See TracChangeset
for help on using the changeset viewer.