Jump to content

My MySQL class...simple


cmgmyr

Recommended Posts

So I'm starting out in OOP and I decided to make my own little MySQL class. Can you please review and let me know if there is anything else i NEED either to make it better or for security. Will this be ok to use for a large site with lots of queries going on at the same time?

 

Thanks

 

mysql.php

<?php

$host = "localhost";
$username = "root";
$password = "password";
$database = "database";

// Connect to MySQL
$connection = mysql_connect($host, $username, $password);

// Select desired database
$link = mysql_select_db($database, $connection);

if (!$link) {
   die('Fatal Error: Could not connect to MySQL Database' . mysql_error());
}


class mysql{

//Query MySQL Database
function query($sql){
$result = mysql_query($sql);
return $result;
}

//Fetch array Query MySQL Database
function fetch($result) {
$rows = mysql_fetch_array($result);
return $rows;
}

//Count the number of rows in query
function count_rows($result) {
$count = mysql_num_rows($result);
return $count;
}


}

?>

 

test_mysql.php

<?php
// Include MySQL class
require_once 'mysql.php';

// Instantiate MySQL class, connect to MySQL and select database
$db = new mysql;

$sql = "SELECT * FROM users WHERE type = 'f' ORDER BY name ASC";

// Perform a query selecting five articles
$result = $db->query($sql); 

// Perform a count
$count = $db->count_rows($result);
echo "$count<br /><br />";

// Display the results
while ($row = $db->fetch($result)) {
  $name = stripslashes($row['name']); 
  echo "$name<br />";
}

?>

Link to comment
Share on other sites

when you are creating a database abstraction class, make sure it will supports more then just one type of database (eg: mysql, mysqli, postgresql, ...)

 


class DB {
     var $container;
     var $conn;
     
     function DB($container = 'MySQL', $options = '') {
         $this->container = new $container($options);
     }
     function connect($server, $username, $password) {
         $this->container->connect($server, $username, $password);
     }
}

// container MySQL
class MySQL extends DB {
     function connect($server, $username, $password) {
         $this->conn = mysql_connect($server, $username, $password);
     }
}

// container PostGreSQL
class PostGreSQL extends DB {
     function connect($server, $username, $password) {
         $this->conn = pg_connect(sprintf("host=%s user=%s password=%s", $server, $username, $password));
     }
}

Link to comment
Share on other sites

when you are creating a database abstraction class, make sure it will supports more then just one type of database (eg: mysql, mysqli, postgresql, ...)

 

Yes, that is a crucial aspect of a database abstraction library, but if you'll notice, cmgmyr was not out to create a database abstraction library. He says "MySQL class", which leads me to believe that he's not going to be using it with a postgres/sqlite/etc. database.

Link to comment
Share on other sites

when you are creating a database abstraction class, make sure it will supports more then just one type of database (eg: mysql, mysqli, postgresql, ...)

 

Yes, that is a crucial aspect of a database abstraction library, but if you'll notice, cmgmyr was not out to create a database abstraction library. He says "MySQL class", which leads me to believe that he's not going to be using it with a postgres/sqlite/etc. database.

 

yes, ofcourse but when they want to play with fire, you might aswell give them a lighter.. ;D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.