<?php
#--------------------------------------------------------------#
# develop by Abo mohammad yusif al-lehyani
# e-mail :yusifpc@hotmail.com
#--------------------------------------------------------------#
##################### GetSQLValueString ############################
// the code Only from Macromedia Dremweaver
function GetSQLValueString($theValue, $theType)
{
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "password":
$theValue = ($theValue != "") ? "'" . md5($theValue) . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "timestamp":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NOW( ) ";
break;
case "datetime":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NOW( ) ";
break;
}
return $theValue;
}
#--------------------------------------------------------------#
#--------------------------------------------------------------#
class DataSet
{
var $link;
var $database;
var $table;
var $active = false;
var $RecNo;
var $link;
var $fields;
var $result;
var $row;
var $errormsg;
var $num_rows;
var $num_fields;
var $sql;
#--------------------------------------------------------------#
function DataSet($dataserver, $username, $password ,$dataname='', $persistent = false)
{
if ($persistent == true) {
$this->link = mysql_pconnect($dataserver, $username, $password);
}
else {
$this->link = mysql_connect ($dataserver, $username, $password);
}
if (!$this->link) die ('ERROR: Can not Connected to Server database');
if (!empty($dataname)) {
$this->select_db($dataname);
}
$this->active = true;
}
#--------------------------------------------------------------#
function select_db($dataname) {
$this->database = $dataname;
if ( ! @mysql_select_db($this->database, $this->link) )
{
echo ("ERROR: Can not to find database ".$this->database);
}
}
#--------------------------------------------------------------#
function close() {
return mysql_close($this->link);
}
#--------------------------------------------------------------#
function Select ($select_sql, $order_by='',$sc='ASC', $startRow='0', $maxRows='20')
{
if (!empty($select_sql)) {
$Strlimit='';
if ( !empty ($startRow) && !empty($maxRows) && $startRow < $maxRows ) {
$Strlimit =sprintf('LIMIT %d, %d', $startRow, $startRow);
} else { $Strlimit = ''; }
if (!empty($order_by) && !empty($sc) ) {
$StrOrder = sprintf('ORDER BY %s %s',$order_by,$sc);
} else $StrOrder = '';
$sql = sprintf('%s %s %s', $select_sql, $StrOrder, $Strlimit);
$this->execute ($sql);
// $this->num_fields = @mysql_num_fields($this->result);
// $this->fields = @mysql_list_fields($this->database,$this->table,$this->link);
}
}
#--------------------------------------------------------------#
function Delete($table,$id)
{
if (!empty($id)) {
$sql = sprintf('DELETE FROM %s WHERE id=%s', $table, $id);
$this->execute ($sql);
}
}
#--------------------------------------------------------------#
function Insert($tablename, $fields)
{
if (!is_array ($fields)) return false;
global $type_from_db;
for(reset($fields); $key = key($fields); next($fields) ) {
$fields_values[] = GetSQLValueString ($fields[$key],$type_from_db[$key]);
}
$values = implode (',', $fields_values);
$fields = implode (',', array_keys($fields) );
$sql = sprintf('INSERT INTO %s (%s) VALUES (%s)',
$tablename, $fields, $values);
$this->execute ($sql);
if ($this->num_rows = 1 ) return $this->insert_id();
else return false;
}
#--------------------------------------------------------------#
function Update ($tablename, $fields, $id='')
{
if (!is_array ($fields) ) return false;
global $type_from_db;
while(list($key,$value) = each($fields)){
$fields_values[] = sprintf('%s=%s',$key, GetSQLValueString ($value,$type_from_db[$key]));
}
$fields_values = implode (',',$fields_values);
if ( $fields_values != '' )
{
if (!empty ( $id )) { $StrWhere = "WHERE id=$id"; } else { $StrWhere = ''; }
$sql = sprintf('UPDATE %s SET %s %s',
$tablename, $fields_values, $StrWhere );
$this->execute ($sql);
if ($this->num_rows > 0 ) return true;
else return false;
}
}
#--------------------------------------------------------------#
function execute ($SQL)
{
$this->result = false;
$this->mun_rows = 0;
$this->sql = !empty ($SQL) ? $SQL : exit;
$this->result = mysql_query($this->sql,$this->link)
or $this->errormsg(mysql_error());
if ($this->result) {
$this->get_num_rows ();
return $this->result;
}
}
#--------------------------------------------------------------#
function empty_table ($tablename)
{
if (!empty($tablename)) $this->execute (sprintf('DELETE FROM %s', $tablename));
}
#--------------------------------------------------------------#
function fetch_row($mode='assoc') {
// $mode = 'array', 'object' , 'assoc' ,
if ($this->num_rows > 0) {
switch ($mode) {
case "array" : $this->row = mysql_fetch_array($this->result); break;
case "object" : $this->row = mysql_fetch_object($this->result); break;
case "assoc": $this->row = mysql_fetch_assoc($this->result); break;
}
return $this->row;
}
}
#--------------------------------------------------------------#
function reset_row ()
{
@mysql_data_seek($this->result,0);
}
#--------------------------------------------------------------#
function seek_row ($int)
{
@mysql_data_seek($this->result,$int);
}
#--------------------------------------------------------------#
function free_result()
{
if ($this->result)
{
@mysql_free_result($this->result);
return true;
} else return false;
}
#--------------------------------------------------------------#
function get_fields_table ($table) {
return mysql_list_fields($this->database, $table, $this->link);
}
#--------------------------------------------------------------#
function get_field ($int, $field ) {
if ( ($this->num_rows > 0) && is_int($int) && !empty($field) ) {
return @mysql_result ($this->result,$int , $field );
} else return null;
}
#--------------------------------------------------------------#
function get_num_rows() {
$this->num_rows = 0;
if ($this->result) {
if (@mysql_num_rows($this->result) >0 ) {
$this->num_rows = @mysql_num_rows($this->result);
} else
if (@mysql_affected_rows($this->link)>0) {
$this->num_rows = @mysql_affected_rows($this->link);
}
}
return $this->num_rows;
}
#--------------------------------------------------------------#
function insert_id() {
if (mysql_affected_rows()>0)
{
return mysql_insert_id($this->link);
}
}
#--------------------------------------------------------------#
function gettable ()
{
$table = '';
if ( $this->num_rows > 0) {
$table = "<table>\n";
for ($i = 0; $i < $this->num_fields; $i++) {
$table .= "<th>".mysql_field_name($this->result, $i)."</th>\n";
}
while ($this->fetch_row () ) {
$table .= "<tr>\n";
for ($i = 0; $i < $this->num_fields; $i++) {
$table .= "<td>".$this->row[$i]."</td>\n";
}
$table .= "</tr>\n";
}
$table .= "</table>\n";
}
return $table;
}
#--------------------------------------------------------------#
function errormsg ($error)
{
global $INFO;
$msg = "<html>
<head>
<title>Database Error</title>
<style>P,BODY{ font-family:arial,sans-serif; font-size:11px; }</style>
</head>
<body>
<br><br><blockquote><b>There appears to be an error with the {$INFO['company']} database.</b><br>
You can try to refresh the page by clicking <a href=\"java script:window.location=window.location;\">here</a>, if this
<a href='mailto:{$INFO['email']}?subject=#'>here</a>
<p>{$this->sql}</p>
<br><br><b>Error Returned</b><br>
<form name='mysql'>
<textarea rows=\"10\" cols=\"70\">".htmlspecialchars($error)."</textarea>
</form></blockquote>
</body>
</html>";
echo($msg);
die("");
}
#--------------------------------------------------------------#
} //end class DataSet
?>بالنسبة لاستخدام الكلاس
$data = new DataSet('localhost', 'root', '','mydb');
//////////
//$label=array(); is my array
$res = "";
$sql = sprintf ("SELECT * FROM %s WHERE (id=%s)",$table, $id);
$this->data->Select ($sql);
if ($this->data->num_rows > 0) {
$this->data->fetch_row();
$res .= "<table>\n";
for( reset( $this->data->row); $field = key( $this->data->row); next($this->data->row) ) {
$res .= sprintf("<tr>\n<td>%s</td><td>%s</td>\n<tr>\n",
$label[$field] , $this->data->row[$field]);
$this->count_field++;
}
$res .= "</table>\n";
}