Page 1 of 1

OOP/Classes and Databases

Posted: Thu Mar 27, 2008 8:13 pm
by caighil
First off, this is my first post here. I am a student in a university on Vancouver Island.

Hi.

I am having a dickens of a time with my code. I have watered it down, to just the concept now. Can anyone out there please help. Thanks in advance. Kyle Corey

This first Code is my Class page. it is saved as con_test.php

Code: Select all

<?php
class Test {
    function __construct(){
        $this->dbConnect = @new mysqli("localhost", "root", "******"); //not the actual password
        if (mysqli_connect_errno()) {
            die("<p>Unable to connect to the database server!!</p>");
        } else echo "<p>Connected to database server</p>";
    }
        function __destruct() {
    }
        public function setDatabase($database) { 
        $this->database = $database;
        $this->dbConnect->query($database) or die("<p>Unable to select the database!!");
        }
}
?>
This is the page that will access the function setDatabase, and in my full program it is suppose to select the database and enter a new user into a table. Again this is just the nuts and bolts.

Code: Select all

<?php
require_once("con_test.php");
session_start();
$test = new Test();
$test->setDatabase('coreyk_bankdb') or die("doh");
 
?>
Any help, suggestions or links would be very helpful....

Cheers Kyle Corey

http://www.kylecorey.ca

Re: OOP/Classes and Databases

Posted: Thu Mar 27, 2008 9:58 pm
by Christopher
caighil wrote:I am having a dickens of a time with my code.
Well ... that's mighty strong language, but I guess if that's how they talk up there on Vancouver Island it's OK then! ;)

It looks like you are headed in the right direction. Do you have a specific problem or just looking for input? I might recommend passing the configuration into the class rather than hard coding it. You could also create a Singleton by using a static to hold the connection id. It depends on how your code is structured.

Re: OOP/Classes and Databases

Posted: Thu Mar 27, 2008 10:57 pm
by caighil
You have never heard of getting the dickens scared out of you??

Hehe.

Anyways, the code is failing to use the "coreyk_bankdb"

this is my output in firefox after i run the program,

Connected to database server

Unable to select the database!!



So it is connecting to my MySQL database server, just not the database itself. HELP :)

Thanks again.

Kyle Corey

http://www.kylecorey.ca

Re: OOP/Classes and Databases

Posted: Fri Mar 28, 2008 12:46 am
by Chris Corbyn
Do you have privileges to access that database and/or does the database exist? Someone needs to GRANT <privileges> ON databaseName.* before it will work.

Re: OOP/Classes and Databases

Posted: Fri Mar 28, 2008 12:49 am
by caighil
I will check right now... It is my box, as I am a student and this is part of our project.

BRB

I am logged in as root, so I must right??

Kyle Corey

http://www.kylecorey.ca

Re: OOP/Classes and Databases

Posted: Fri Mar 28, 2008 1:46 am
by Christopher
It should be:

Code: Select all

<?php
class Test {
    public function setDatabase($database) { 
        $this->database = $database;
        $this->dbConnect->select_db($database) or die("<p>Unable to select the database!!");
    }
}
?>
I would also recommend not dying all over the place. Add an isError() method or throw exceptions.