尝试使用 PDO 创建 PHP CRUD 应用程序,但在配置文件中获取未定义类型 'AppPDO'



整个项目-https://github.com/steve-davey/phpsqliteconnect(配置文件已过期(

这是配置文件:

<?php
namespace App;
class Config {
/**
* path to the sqlite file
*/
const PATH_TO_SQLITE_FILE = 'db/DeviceAssetRegister.db';
}
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'DeviceAssetRegister');

/* Attempt to connect to MySQL database */
try{
$pdo = new PDO("mysql:host=" . DB_SERVER . ";dbname=" . DB_NAME, DB_USERNAME, DB_PASSWORD);
// Set the PDO error mode to exception
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
die("ERROR: Could not connect. " . $e->getMessage());
}
?>

我不明白为什么PDO出现错误,而上面目录中的index.php文件却没有?

$pdo = new PDO('sqlite:./db/DeviceAssetRegister.db');

这真是太好了!我甚至在VSC中得到了一个链接到PHP文档的弹出式描述。那么,为什么它会在另一个文件中抛出一个错误呢??TIA!

PDO是根命名空间中的一个类。除非您正确使用该根命名空间(通过导入PDO或使用new PDO(,否则PHP将从当前命名空间(即App(中搜索该类

最新更新