OOP/Classes and Databases

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
caighil
Forum Newbie
Posts: 3
Joined: Thu Mar 27, 2008 8:07 pm

OOP/Classes and Databases

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: OOP/Classes and Databases

Post 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.
(#10850)
caighil
Forum Newbie
Posts: 3
Joined: Thu Mar 27, 2008 8:07 pm

Re: OOP/Classes and Databases

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: OOP/Classes and Databases

Post 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.
caighil
Forum Newbie
Posts: 3
Joined: Thu Mar 27, 2008 8:07 pm

Re: OOP/Classes and Databases

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: OOP/Classes and Databases

Post 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.
(#10850)
Post Reply