Jump to content

Review my first programmed class


makeshift_theory

Recommended Posts

Hello everyone, well I have taken the next step in my php programming world and attempted to do some OOP.  Below is a Database Driver that I am using to get variables out that I need for my game I am working on.  I wanted to see how the code structure and maybe some improvements so I can make it even better....THANKS!

[code]<?php
class db_conn
{
// Class to do all database queries for a mysql database
// Database Driver Created By Chris Willard
// Finished on 11/10/2006
/*
Functions Included:
db_connect() : Connects a Database
db_select() : Selects a Table
db_query() : Runs a query on whatever table is currently selected
db_read() : Echo's out a list of everything found in query
db_readcol() : Echo's out the table's columns
db_close() : Closes Active Database
*/

var $db_host; // Host Address
var $db_db; // Database to connect to
var $db_uid; // DB Username
var $db_pwd; // DB Password
var $db_link; // Connection Link
var $db_queryr; // DB Result
var $db_result; // Result Array

function error($error)
{ // Handles errors
echo "<font color=red><b>".$error."</b></font>";
}

function resultmsg($result)
{ // Handles Result messages
foreach($result as $msg)
echo "<font color=navy><b>".$msg."</b></font>";
}


function db_connect($db_host,$db_uid,$db_pwd)
{//Connects Database
$this->db_host = $db_host; // Set host
$this->db_uid = $db_uid; // Set UID
$this->db_pwd = $db_pwd; // SET PWD

$this->link = mysql_connect($db_host,$db_uid,$db_pwd) or
$this->error(mysql_error());

if($this->link) // If Connection is Accepted
echo "Connection Accepted.<br>";

}

function db_select($db_db)
{ // Selects a Database
$this->db_db = $db_db; // Set Database Globally
if(mysql_select_db($this->db_db))
echo "Database Found and Connected.";
else
$this->error(mysql_error());

}

function db_query($query)
{ // Does a query
$this->queryr = mysql_query($query);
$numresults = mysql_num_rows($this->queryr);
$resultmsg[] = "<br>Found " . $numresults . " results<br>";
$this->resultmsg($resultmsg); // Return messages
}

function db_read()
{ // Outputs all of the database variables
while($row = mysql_fetch_array($this->queryr, MYSQL_NUM))
for($i;$i<=count($row);$i++)
echo $row[$i]."\n";
mysql_free_result($this->queryr);

}

function db_readcol()
{ // Outputs all of the table's columns
  while ($row = mysql_fetch_array($this->queryr)) {
      echo $row[0]."\n";
  }
mysql_free_result($this->queryr);

}

function db_close()
{ // Closes active Database
$close = mysql_close($this->link) or
$this->error(mysql_error());
if($close)
{
$this->db_result[] = "<br>Connection Successfully Closed.";
$this->resultmsg($this->db_result);
}
}






}[/code]
Link to comment
https://forums.phpfreaks.com/topic/26878-review-my-first-programmed-class/
Share on other sites

agreed. if you're new to OOP, then download a copy of phpBB (www.phpbb.com). the classes for each DB tech (MySQL, MSSQL, etc) are quite similar to yours so should be easy enough to understand, only it allows (via a change in its settings) to switch seamlessly between each one if required. helped me loads when i first moved away from Dreamweaver's built in stuff.
Awesome I'll check that out, I don't use MSSQL or Access much, I use Oracle at work but that's about it.  I wish I would have gotten into OOP a while ago, it has made programming much easier for me.  I have just finished programming a dynamic site management tool that has a dynamic editor, publisher, and versioning in it.  I don't think I would have been able to finish it on time if it wasn't for OOP =).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.