未定义的变量PDO

时间:2013-11-17 21:55:46

标签: php pdo phpstorm

我使用PDO进行数据库连接。所有PDO内容都已在文件application_top.php中初始化。

if(!isset($db)) {
  $db = new Morrow\Db(array(
    'driver'   => 'mysql',
    'host'     => DB_SERVER,
    'db'       => DB_DATABASE,
    'user'     => DB_SERVER_USERNAME,
    'pass'     => DB_SERVER_PASSWORD,
    'encoding' => '"utf8"',
  ));
}

这很好用。但PHPStorm给了我一个警告

  

未定义变量'db'

代码看起来像

require_once 'includes/application_top.php';
...
$pass = $db->result_one("
  SELECT customers_password
  FROM   " . TABLE_CUSTOMERS . "
  WHERE  customers_id = " . (int)$_SESSION['customer_id']
);

但为什么$ db未定义?

1 个答案:

答案 0 :(得分:1)

我喜欢这个:

require_once 'includes/application_top.php';
...
if (!isset($db)) exit; // or die, return false, thrown exception

$pass = $db->result_one("
  SELECT customers_password
  FROM   " . TABLE_CUSTOMERS . "
  WHERE  customers_id = " . (int)$_SESSION['customer_id']
);

它让PHPStorm感到高兴,也许会给我一些安全保障。