简体   繁体   English

设置Microsoft Access数据库的PDO连接类

[英]Setting up PDO connection class for Microsoft Access Database

Good day! 美好的一天!

Basically, I'm trying to set up a PDO connection class with a Microsoft Access database with PHP so I can call the class when I'm creating new pages. 基本上,我试图通过PHP用Microsoft Access数据库设置PDO连接类,以便在创建新页面时可以调用该类。

So far I have this, i've tried to adapt it from other articles on Stack Overflow: NB: I'm completely new to setting up PHP with a Microsoft Access database and I know my $dbName is probably in the wrong place and I'm not sure where it goes to be honest! 到目前为止,我已经做到了这一点,我尝试将其与其他有关Stack Overflow的文章相提并论:NB:我对使用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);
        }
    }   
}

So what I'm trying to find out is where the $dbName variable goes and also how I would I could call the connection class in an index page along with an SQL statement. 因此,我想找出的是$ dbName变量的位置以及如何在索引页中调用连接类以及SQL语句。

I have a basic idea, something along these lines: 我有一个基本思路,遵循以下原则:

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;  
}

I run the script and I just get this message: 我运行该脚本,然后收到以下消息:
'We're sorry but there was an error while trying to connect to the database' “很抱歉,尝试连接数据库时出错”

Can anyone please point me in the right direction, any help would be absolutely awesome! 任何人都可以向我指出正确的方向,任何帮助都将非常棒! Thanks in advance! 提前致谢!

EDIT: Ok, now I'm getting really lost - I've been looking at my code and can't figure out why i'm getting the error: Fatal error: Call to a member function fetch() on a non-object in D:....... on line 23. Here's the code now: 编辑:好的,现在我真的迷路了-我一直在查看我的代码,无法弄清楚为什么我得到错误:致命错误:在非对象上调用成员函数fetch()在第23行的D:.......中,这是现在的代码:

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);
    }

The error is relating to this line: while ($row = $result->fetch()) { 错误与此行有关:while($ row = $ result-> fetch()){

Where am I going wrong? 我要去哪里错了?

connection class 连接类别

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;
    }   
}

and the code : 代码

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);
}

$dbname either needs to be a local variable of the function that actually uses it, or needs to be defined as a member of the class. $ dbname要么需要是实际使用它的函数的局部变量,要么需要定义为该类的成员。 As it stands, you have it as a local variable of a function that doesn't even use it. 就目前而言,您可以将其作为甚至不使用它的函数的局部变量。 And you should use a naming convention that distinguishes between members and local variables, possibly by using m_ in front of class members: 并且您应该使用一种区分成员和局部变量的命名约定,可能通过在类成员前面使用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=;");

Or 要么

 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=;");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM