美好的一天!
基本上,我正在尝试使用PHP建立一个带有Microsoft Access数据库的PDO连接类,这样我就可以在创建新页面时调用该类。
到目前为止,我有这个,我试图从Stack Overflow上的其他文章中进行调整: 注意:我对使用Microsoft Access数据库设置PHP完全不熟悉,我知道我的$ dbName可能位于错误的位置,我不确定它在哪里说实话!
class connection{
public $con = '';
function __construct(){
$this->connect();
/*probably in the wrong place but... */
$dbName = $_SERVER["DOCUMENT_ROOT"] . "\database/yakety1new.mdb";
}
function connect(){
try{
$this->con = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$dbName; Uid=Admin; Pwd=;");
$this->con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e){
echo 'We\'re sorry but there was an error while trying to connect to the database';
file_put_contents('connection.errors.txt', $e->getMessage().PHP_EOL,FILE_APPEND);
}
}
}
所以我想要找出的是$ dbName变量的位置,以及我如何在索引页面中调用连接类以及SQL语句。
我有一个基本的想法,就是这样:
include_once '/pages/classes/connectionClass.php';
$con = new connection();
$sql = $this->con->prepare("SELECT * FROM celebs");
$result = $con->query($sql);
while ($row = $result->fetch()) {
$firstname = $row['firstname'];
$surname = $row['surname'];
echo $firstname;
echo $surname;
}
我运行脚本,我收到此消息:
“我们很抱歉,但在尝试连接数据库时发生错误”
任何人都可以指出我正确的方向,任何帮助都绝对棒! 提前谢谢!
编辑:好的,现在我真的迷路了 - 我一直在查看我的代码,无法弄清楚为什么我收到错误:致命错误:调用成员函数fetch() D中的非对象:......第23行。这是现在的代码:try{
include_once '\classes\connectionClass.php';
//get the DB connection
$con = new connection();
$pdoConnection = $con->connect();
//query the DB
$sql = $pdoConnection->prepare("SELECT * FROM celebs");
$result = $pdoConnection->query($sql);
while ($row = $result->fetch()) {
echo $row['firstname'];
echo $row['surname'];
}
} catch (Exception $e){
echo 'ERROR:'.$e->getMessage();
file_put_contents('connection.errors.txt', $e->getMessage().PHP_EOL,FILE_APPEND);
}
错误与此行有关: while($ row = $ result-> fetch()){
我哪里错了?
答案 0 :(得分:1)
连接类
class connection{
private $con;
private $dbName;
function __construct(){
$this->dbName = $_SERVER["DOCUMENT_ROOT"] . "\database/yakety1new.mdb";
}
function connect(){
$this->con = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$this->dbName; Uid=Admin; Pwd=;");
$this->con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
return $this->con;
}
}
和代码:
try{
include_once '/pages/classes/connectionClass.php';
//get the DB connection
$con = new connection();
$pdoConnection = $conn->connect();
//query the DB
$sql = $pdoConnection->prepare("SELECT * FROM celebs");
$result = $pdoConnection->execute();
while ($row = $result->fetchAll(PDO::FETCH_ASSOC)) {
echo $row['firstname'];
echo $row['surname'];
}
} catch (Exception $e){
echo 'ERROR:'.$e->getMessage();
file_put_contents('connection.errors.txt', $e->getMessage().PHP_EOL,FILE_APPEND);
}
答案 1 :(得分:0)
$ dbname要么是实际使用它的函数的局部变量,要么需要定义为类的成员。就目前而言,你将它作为一个功能的局部变量,甚至不使用它。您应该使用区分成员和局部变量的命名约定,可能在类成员面前使用m_:
class connection
{
public $m_con = '';
private $m_dbName = '';
function __construct()
{
...
$m_dbName = $_SERVER["DOCUMENT_ROOT"] . "\database/yakety1new.mdb";
}
function connect()
{
try
{
$this->m_con = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$m_dbName; Uid=Admin; Pwd=;");
或者
function connect()
{
try
{
$dbName = $_SERVER["DOCUMENT_ROOT"] . "\database/yakety1new.mdb";
$this->con = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$dbName; Uid=Admin; Pwd=;");