![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Apr 2005
Posts: 126
Rep Power: 4
![]() |
Am I on the right track with OOP?
I havent programmed with PHP in over a year and Im starting again, by jumping into OOP, something I never really used before... kinda confused me at first, working on a mysql class. Anyways, heres what I have for the connection, is this complete sloppyness? or acceptable?
testmysql.php <?php
require_once "config.php";
require_once "classes/mysql.php";
$db = new mysqldb;
$db->dbhost = $dbhost;
$db->dbusername = $dbusername;
$db->dbpassword = $dbpassword;
$db->dbname = $dbname;
$conn = $db->connect();
if (!$conn) {
echo 'Error Connecting to the database.';
}
else {
echo 'Successful Connection.';
}
?>mysql.php <?php
class mysqldb
{
var $dbhost;
var $dbusername;
var $dbpassword;
var $dbname;
function connect() {
$dblink = mysql_connect($this->dbhost, $this->dbusername, $this->dbpassword);
if (!$dblink) {
}
elseif (!mysql_select_db($this->dbname, $dblink)) {
$dblink = false;
}
return $dblink;
}
}
?>I wrote the connect to select the database too because my scripts only using 1 database and it would be quite an annoyance to have to have an extra line in every file to select the database. How does it look? |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
What is this? (rearranged for logical clarity)
if (!$dblink)
{
// Empty blocks are acceptable, but why?
}
elseif (!mysql_select_db($this->dbname, $dblink))
{
$dblink = false;
}if ($dblink && !mysql_select_db ($this->dbname, $dblink)) $dblink = false;
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Apr 2005
Posts: 126
Rep Power: 4
![]() |
Alrighty, thanks, I see what your saying.
I have another question... I have the mysqldb class, and now I have a validateuser class. The valideuserclass looks like this currently: <?php
class validateduser extends mysqldb
{
var $vusername;
var $vpassword;
function setvariables() {
$this->vusername = $_SESSION['pkmnt_user'];
$this->vpassword = $_SESSION['pkmnt_pass'];
}
function checkvalid() {
$sel = "SELECT `id` FROM `members` WHERE username='$this->vusername' AND password='$this->vpassword' LIMIT 1";
$sel = $this->query($sel);
$num = $this->num($sel);
if ($num != 1) {
return false;
}
else {
return true;
}
}
}
?>Thats just a simple function to start off to see if the username and password's are valid. This class is right after the mysqldb class. The problem is when it extends the mysql class, all the vars in that class are dropped, as is the connection var, so all querys fail unless I fill out the connection again, and its just too sloppy to do that over and over again and start another connection, that is beyond out of the question. Is there any other way I can use the mysql classes functions within that, without extending it? |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Oct 2006
Posts: 14
Rep Power: 0
![]() |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to keep track of bridge mappings. | hoffmandirt | Software Design and Algorithms | 2 | Sep 8th, 2006 11:19 AM |
| New Computer | Lord Barlak | Coder's Corner Lounge | 73 | Aug 7th, 2006 4:47 PM |
| Older members: How did you learn to code | Mjordan2nd | Coder's Corner Lounge | 55 | Mar 22nd, 2006 8:35 AM |
| YEY!! My First GUI To share with the world!! | SaturN | Python | 8 | Jul 16th, 2005 4:55 PM |
| Linked List Part 2 | Nish | C++ | 0 | Mar 4th, 2005 3:56 PM |