- in primul rand vreau sa fac o clasa in care sa implementez operatiile de baza care se fac cu baza de date;
- connect, select, insert, delete, (affected rows, error, insert_id,
disconnect)
Ma ajuta cineva ? cu ceva exemple ?
clasa php
Moderatori: Zamolxe, Moderatori
uite aici o clasa faina
[php]
<?php
class DB
{
var $db = array();
var $link_id, $query_id, $query=FALSE, $sqlr;
var $failed = FALSE;
var $debug = FALSE;
var $debugtext = array();
var $link_time;
var $emsg;
function DB($db_name,$user,$pass,$host="localhost"){
$this->db=array('name'=>$db_name,'user'=>$user,'pass'=>$pass,'host'=>$host);
}
function record_error(){
$this->failed=TRUE;
if($this->debug)
{
$this->debugtext[]="ERROR: ".mysql_error();
if($this->query)
$this->query="FAILED: ".$query;
}
}
function connect($persistent=false){
$this->failed=FALSE;
$this->db['pers']=$persistent;
if($persistent)
$this->link_id=mysql_pconnect($this->db['host'],$this->db['user'],$this->db['pass']);
else
$this->link_id=mysql_connect($this->db['host'],$this->db['user'],$this->db['pass']);
mysql_select_db($this->db['name']) or $this->record_error();
//if($this->debug)
list($usec, $sec) = explode(" ", microtime());
$this->link_time=(float)$usec+(float)$sec;
return !$this->failed;
}
function disconnect(){
$this->freeResult();
if($this->db['pers'])
mysql_close($this->link_id);
if($this->debug)
{
list($usec, $sec) = explode(" ", microtime());
$this->link_time=(float)$usec+(float)$sec-$this->link_time;
$this->debugtext[]="END Connection: ".(string)$this->link_time." sec";
}
}
function query($query){
$this->freeResult();
$this->sqlr=mysql_query($query) or $this->record_error();
//if($this->debug)
$this->debugtext[]="$query";
return $this->sqlr;
}
function getRow(){
if(is_resource($this->sqlr))
return mysql_fetch_assoc($this->sqlr);
return FALSE;
}
function freeResult(){
if(isset($this->sqlr)&&is_resource($this->sqlr))
{
mysql_free_result($this->sqlr);
unset($this->sqlr);
}
}
function getLine($query,$id=FALSE){
if($id===FALSE)
$this->query($query);
else
$this->query($query." WHERE `id`='$id'");
if(is_resource($this->sqlr))
{
$line=mysql_fetch_assoc($this->sqlr);
$this->freeResult();
return $line;
}
return FALSE;
}
function getTable($query,$useid=FALSE){
$table=array();
if($this->query($query)==FALSE) return FALSE;
while($line=mysql_fetch_assoc($this->sqlr))
{
if($useid)
$table[$line['id']]=$line;
else
$table[]=$line;
}
return $table;
}
function insertRow($table,$array){
if(count($array)==0) return;
$comma='';$listofvalues=''; $listofelements='';
foreach($array as $elem=>$value)
{
if($value===NULL)
$listofvalues.="{$comma}NULL";
else
{
$value=mysql_escape_string($value);
$listofvalues.="$comma'$value'";
}
$listofelements.="$comma`$elem`";
$comma=',';
};
$query = "INSERT HIGH_PRIORITY INTO $table (".$listofelements.") VALUES(".$listofvalues.")";
$this->query("$query");
}
function updateRow($table,$array,$condition){
if(count($array)==0) return;
$q="UPDATE $table SET ";
foreach($array as $index=>$value)
{
if($value==NULL)
$q.="`$index`=NULL, ";
else
{
$value=mysql_escape_string($value);
$q.="`$index`='$value', ";
}
}
$q=substr($q,0,-2)." WHERE $condition LIMIT 1";
$this->query($q);
}
function deleteRow($table,$condition,$single=true){
$q="DELETE FROM $table WHERE $condition".($single?" LIMIT 1":"");
$this->query($q);
}
function getInfo_tableStatus($tablename,$what_to_get='all'){
$line=$this->getLine("SHOW TABLE STATUS LIKE '$tablename'");
switch($what_to_get){
case 'all': return array('rows'=>$line['Rows'],'autoinc'=>$line['Auto_increment']);
case 'rows': return $line['Rows'];
case 'autoinc': return $line['Auto_increment'];
}
}
function getInfo_countRows($tablename,$condition=false){
if($condition) $line=$this->getLine("SELECT COUNT(*) FROM `$tablename` WHERE $condition");
else $line=$this->getLine("SELECT COUNT(*) FROM `$tablename`");
return $line['COUNT(*)'];
}
}
?>
[/php]
[php]
<?php
class DB
{
var $db = array();
var $link_id, $query_id, $query=FALSE, $sqlr;
var $failed = FALSE;
var $debug = FALSE;
var $debugtext = array();
var $link_time;
var $emsg;
function DB($db_name,$user,$pass,$host="localhost"){
$this->db=array('name'=>$db_name,'user'=>$user,'pass'=>$pass,'host'=>$host);
}
function record_error(){
$this->failed=TRUE;
if($this->debug)
{
$this->debugtext[]="ERROR: ".mysql_error();
if($this->query)
$this->query="FAILED: ".$query;
}
}
function connect($persistent=false){
$this->failed=FALSE;
$this->db['pers']=$persistent;
if($persistent)
$this->link_id=mysql_pconnect($this->db['host'],$this->db['user'],$this->db['pass']);
else
$this->link_id=mysql_connect($this->db['host'],$this->db['user'],$this->db['pass']);
mysql_select_db($this->db['name']) or $this->record_error();
//if($this->debug)
list($usec, $sec) = explode(" ", microtime());
$this->link_time=(float)$usec+(float)$sec;
return !$this->failed;
}
function disconnect(){
$this->freeResult();
if($this->db['pers'])
mysql_close($this->link_id);
if($this->debug)
{
list($usec, $sec) = explode(" ", microtime());
$this->link_time=(float)$usec+(float)$sec-$this->link_time;
$this->debugtext[]="END Connection: ".(string)$this->link_time." sec";
}
}
function query($query){
$this->freeResult();
$this->sqlr=mysql_query($query) or $this->record_error();
//if($this->debug)
$this->debugtext[]="$query";
return $this->sqlr;
}
function getRow(){
if(is_resource($this->sqlr))
return mysql_fetch_assoc($this->sqlr);
return FALSE;
}
function freeResult(){
if(isset($this->sqlr)&&is_resource($this->sqlr))
{
mysql_free_result($this->sqlr);
unset($this->sqlr);
}
}
function getLine($query,$id=FALSE){
if($id===FALSE)
$this->query($query);
else
$this->query($query." WHERE `id`='$id'");
if(is_resource($this->sqlr))
{
$line=mysql_fetch_assoc($this->sqlr);
$this->freeResult();
return $line;
}
return FALSE;
}
function getTable($query,$useid=FALSE){
$table=array();
if($this->query($query)==FALSE) return FALSE;
while($line=mysql_fetch_assoc($this->sqlr))
{
if($useid)
$table[$line['id']]=$line;
else
$table[]=$line;
}
return $table;
}
function insertRow($table,$array){
if(count($array)==0) return;
$comma='';$listofvalues=''; $listofelements='';
foreach($array as $elem=>$value)
{
if($value===NULL)
$listofvalues.="{$comma}NULL";
else
{
$value=mysql_escape_string($value);
$listofvalues.="$comma'$value'";
}
$listofelements.="$comma`$elem`";
$comma=',';
};
$query = "INSERT HIGH_PRIORITY INTO $table (".$listofelements.") VALUES(".$listofvalues.")";
$this->query("$query");
}
function updateRow($table,$array,$condition){
if(count($array)==0) return;
$q="UPDATE $table SET ";
foreach($array as $index=>$value)
{
if($value==NULL)
$q.="`$index`=NULL, ";
else
{
$value=mysql_escape_string($value);
$q.="`$index`='$value', ";
}
}
$q=substr($q,0,-2)." WHERE $condition LIMIT 1";
$this->query($q);
}
function deleteRow($table,$condition,$single=true){
$q="DELETE FROM $table WHERE $condition".($single?" LIMIT 1":"");
$this->query($q);
}
function getInfo_tableStatus($tablename,$what_to_get='all'){
$line=$this->getLine("SHOW TABLE STATUS LIKE '$tablename'");
switch($what_to_get){
case 'all': return array('rows'=>$line['Rows'],'autoinc'=>$line['Auto_increment']);
case 'rows': return $line['Rows'];
case 'autoinc': return $line['Auto_increment'];
}
}
function getInfo_countRows($tablename,$condition=false){
if($condition) $line=$this->getLine("SELECT COUNT(*) FROM `$tablename` WHERE $condition");
else $line=$this->getLine("SELECT COUNT(*) FROM `$tablename`");
return $line['COUNT(*)'];
}
}
?>
[/php]
Cine este conectat
Utilizatori ce ce navighează pe acest forum: Niciun utilizator înregistrat și 7 vizitatori
