Reputation: 363
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.
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!
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.
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:
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()) {
Where am I going wrong?
Upvotes: 1
Views: 1765
Reputation: 45500
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);
}
Upvotes: 1
Reputation: 8659
$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. 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:
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=;");
Upvotes: 0