מצאתי לך מחלקה של DATEBASE תסתכל איך הם עשו
PHP קוד:
class mysql {
function Connect($host, $name, $pass, $db){
$connection = mysql_connect("$host",
"$name",
"$pass");
mysql_select_db("$db", $connection);
}//ends the connection function
function Close(){
mysql_close($this->connection);
}//ends the close function
function FetchRow($query){
$rows = mysql_fetch_row($query);
return $rows;
}
function FetchArray($query){
$array = mysql_fetch_array($query);
return $array;
}
function FetchNum($query){
$num = mysql_num_rows($query);
return $num;
}
function Query($sql){
$query = mysql_query($sql) or die(mysql_error());
return $query;
}//ends the query function
}//ends the class
דוגמא לשימוש במחלקה
PHP קוד:
include("mysql_class.php");
$DB = new mysql();
$host = "localhost";
$name = "username";
$pass = "password";
$db = "dbname";
$connection = $DB->Connect($host, $name, $pass, $db);
//define an SQL statement and execute it
$sql = "SELECT title,author FROM news";
$query = $DB->Query($sql);
//fetch a single row and output it
$newsrow = $DB->FetchRow($query);
$title = $newsrow[0];
$author = $newsrow[1];
echo "<b>Single Row</b><br /><br />Title: $title<br>
Author: $author<br><br>";
//output all rows from the statement
while($array = $DB->FetchArray($query)){
extract($array);
echo "<b>All rows</b><br /><br />Title: $title<b>
Author: $author<br />";
}
//find the number of rows
$num = $DB->FetchNum($query);
echo "Number of rows: $num";
//close the connection
$DB->Close();