Hi. I've been working on this for a few hours, mostly to make some stuff easier for myself, make work faster and get some experience/knowledge on PHP.
I am not aware if there is a MySQL class with PHP and most of you are laughing at me, but I think I did a good job here, and just wanted to share with you, for critic, tips and if you feel like using it or take it and develop on it, be my guest.
Since the class is not that big (yet) I think I can permit myself to post it and not a hyperlink or a download.
PHP Code:class MySQL {
/*
* Author: Mindblaster7
* Version: 1.0
* Last update: 06-06-2010
*/
private static $connected = false;
private static $failed = false;
private static $connection = NULL;
public function connectTo($host, $user, $pass, $db) {
if (!self::isLoggedIn()) { //This check does not seem to work..
$conn = mysql_connect($host, $user, $pass) or (die(mysql_error() && self::$failed = true));
$db = mysql_select_db($db) or (die(mysql_error() && self::$failed = true));
if (self::$failed === false) {
self::$connected = true;
self::$connection = $conn;
} else {
self::alert('You failed to connect to the database!');
}
} else {
self::alert("You can't log in twice, please make another object of the MySQL class.");
}
}
public function getData($sql, $rowNames, &$returnTo = NULL) {
if (self::isLoggedIn()) {
$data = mysql_query($sql);
if (is_array($rowNames)) {
$x = 0;
while ($row = mysql_fetch_assoc($data)) {
for($var = 0; $var <= count($rowNames); $var++) {
$array[$rowNames[$var]][$x] = $row[$rowNames[$var]];
}
$x++;
}
} else {
$row = mysql_fetch_assoc($data);
}
if (isset($array)) {
if (!$returnTo === NULL)
$returnTo = $array;
else
return $array;
}
else {
if (!$returnTo === NULL)
$returnTo = $row;
else
return $row[$rowNames];
}
}
else {
self::alert("Could not get the data you requested, you were not connected to a MySQL Database.");
}
}
public function insertData() {
$args = func_get_args();
if (count($args) >= 2) {
if (self::isLoggedIn()) {
$sql = "INSERT INTO $args[0] VALUES(";
for($var = 1; $var <= count($args) - 1; $var++) {
$sql .= "'{$args[$var]}'";
if ($var != count($args)- 1) //Afslut SQL koden
$sql .= ",";
else {
$sql .= ");";
}
}
mysql_query($sql) or self::alert(mysql_error());
} else
self::alert("You were not logged into a MySQL database, therefor you were unable to insert your desired data.");
}
}
public function deleteData() {
$args = func_get_args();
if (count($args) >= 2) {
$sql = "DELETE FROM $args[0] WHERE ";
for($var = 1; $var <= count($args) - 1; $var++) {
$sql .= $args[$var];
if ($var != count($args) - 1)
$sql .= " AND ";
else {
$sql .= ";";
}
}
mysql_query($sql) or self::alert(mysql_error());
}
}
public function executeSQL($sql) {
if (self::isLoggedIn()) {
$query = mysql_query($sql);
return $query;
} else
self::alert("You were not logged into a MySQL database, therefor you were unable to execute the SQL command.");
}
public function closeCon() {
if (self::isLoggedIn()) {
mysql_close(self::$connection);
self::$connected = false;
}
}
private function isLoggedIn() {
return (self::$connected === true && self::$failed === false) ? true : false;
}
private function alert($message) {
print "<script type=\"text/javascript\">$message</script>";
}
} // end of MySQL Class
Output:PHP Code:$mysql->connectTo("localhost", "root", "", "skema");
$mysql->deleteData("tabel", "navn = 'Bob'", "alder = '35'");
$mysql->insertData("tabel", "", "Bob", "McGee", "35");
$var = $mysql->getData("SELECT * FROM tabel ORDER BY id", array("navn", "alder"));
$length = count($var['navn']) - 1;
for($i = 0; $i <= $length; $i++) {
print "First name: {$var['navn'][$i]}<br />";
print "Age: {$var['alder'][$i]}<br /><br />";
}
$mysql->closeCon();
Code:First name: Mathias Age: 16 First name: Bob Age: 35


Reply With Quote![[PHP]MySQL Class](http://ragezone.com/hyper728.png)

