Det är helt enkelt en class som du bara kör en funktion i för att sätta in/ta ut/tabort/ändra information i en mysqldatabas med... jag hade tänkt att använda den till en kod databas jag pillar på... men iaf, här är den:
Code: Select all
<?php
// Mysql Class
// Version Beta 0.1
// By: thr
// Started: 2003-03-19
// Filename: Mysql.php
// Conatins: Class & Functions for doing various mysql requests
$host = "databasehost";
$username = "username";
$password = "password";
$database = "databasename";
// Start MySQL Class -->
class MySQL{
// Function for connecting to the database
function Connect(){
// Get global vars
global $host,$username,$password,$database;
// Connect to MySQL and select database, If there were errors, give error msgs and exit
if(!mysql_connect($host,$username,$password) || !mysql_select_db($database)){
echo mysql_error(); return false; exit;}
// Retrun true if all is succsessfull
return true;}
// Function for disconnecting to the database
function Disconnect(){
// Get global vars
global $host,$username,$password;
// Disconnect from MySQL and select database, If there were errors, give error msgs and exit
if(!mysql_close(mysql_connect($host,$username,$password))){
echo mysql_error(); return false; exit;}
// Retrun true if all is succsessfull
return true;}
// Function for inserting values into a table and cells
function Insert($table,$cols,$values){ // tablename,cols,values
// Check so that no arguments were empty
if($table == "" || $cols == "" || $values == ""){
echo "One or more argument(s) was empty for function '<b>Insert</b>' in the class '<b>MySQL</b>'"; return false; exit;}
// Connect to the database
$this->Connect();
// Do insert request, check for failure
if(!mysql_query("INSERT INTO $table ($cols) VALUES($values)")){
echo mysql_error(); return false; exit;}
// Disconnect from the database
$this->Disconnect();
// Retrun true if all is succsessfull
return true;}
function Update($table,$cols,$values,$where){ // tablename,columns,values,where
// Check so that no arguments were empty
if($table == "" || $cols == "" || $values == "" || $where == ""){
echo "One or more argument(s) was empty for function '<b>Update</b>' in the class '<b>MySQL</b>'"; return false; exit;}
// Split values
$values = explode(",",$values);
// Split cells
$cols = explode(",",$cols);
// Check so that $values and $cells have the same amount of values
if(count($values) != count($cols)){
echo "Array $colls ans array $values did not have the same amount of values in function '<b>Update</b>' in the class '<b>MySQL</b>'"; return false; exit;}
// Get the correct SET MySQL syntax
for($i=0;$i < count($values);$i++){
$set .= $cols[$i] . "='" .$values[$i] . "'";
if($i < count($values)-1){
$set .= ",";}}
// Connect to the database
$this->Connect();
// Do update request
if(!mysql_query("UPDATE $table SET $set WHERE $where")){
echo mysql_error(); return false; exit;}
// Disconnect from the database
$this->Disconnect();
// Retrun true if all is succsessfull
return true;}
function Delete($table,$where,$limit){ // tablename,where,limit
// Check so that no arguments were empty
if($table == "" || $where == "" || $limit == ""){
echo "One or more argument(s) was empty for function '<b>Delete</b>' in the class '<b>MySQL</b>'"; return false; exit;}
// Connect to the database
$this->Connect();
// Do Delete request
if(!mysql_query("DELETE FROM $table WHERE $where LIMIT $limit")){
echo mysql_error(); return false; exit;}
// Disconnect from the database
$this->Disconnect();
// Retrun true if all is succsessfull
return true;}
function Select($table,$cols,$where="",$limit="",$orderby=""){ // table,columns,where,limit,orderby returns an array.
// Check so that no required arguments were empty
if($table == "" || $cols == ""){
echo "One or more argument(s) was empty for function '<b>Select</b>' in the class '<b>MySQL</b>'"; return false; exit;}
// Check for WHERE statement
if($where != ""){
$set .= " WHERE $where";}
// Check for ORDER BY statement
if($orderby != ""){
$set .= " ORDER BY $orderby";}
// Check for LIMIT statement
if($limit != ""){
$set .= " LIMIT $limit";}
// Connect to the database
$this->Connect();
// Do Select request
$query = mysql_query("SELECT $cols FROM $table$set");
while($sql_result = mysql_fetch_array($query,MYSQL_NUM)){ $array[] = $sql_result;}
// Disconnect from the database
$this->Disconnect();
// Return asociative array
return $array;}
// <-- End Class
}
?>
Exempel:
Code: Select all
$mysql = new MySQL;
// Insert exempel:
$mysql->Insert("tabell","cell1,cell2,cell3","värde1,värde2,värde3);
// Update exempel:
$mysql->Update("tabell","cell1,cell2","nyttvärde1,nyttvärde2","id=3");
// Delete exempel:
$mysql->Delete("tabell","id=1","1");
// Select exempel(Returnerar en asociative array):
$mysql->Select("tabell","*","usertype=2","5","id");