clasa php

Ai o întrebare legată de PHP? Incercăm să îi găsim soluţie. Sau poate doar vrei să publici un cod interesant.

Moderatori: Zamolxe, Moderatori

aPParT3
PHPRomania Supporter
Mesaje: 25
Membru din: Vin Apr 08, 2005 5:41 pm

clasa php

Mesajde aPParT3 » Dum Mai 14, 2006 10:23 am

- 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 ?



Avatar utilizator
raul_
Senior Member
Mesaje: 711
Membru din: Joi Sep 15, 2005 8:00 am

Mesajde raul_ » Dum Mai 14, 2006 11:35 am

Ultima oară modificat Mar Mai 30, 2006 11:52 pm de către raul_, modificat 1 dată în total.
I have learned that you shouldn't compare yourself to others - they are more screwed up than you think!

Avatar utilizator
assault
Senior Member
Mesaje: 237
Membru din: Sâm Dec 03, 2005 8:00 am

Mesajde assault » Dum Mai 14, 2006 5:24 pm

uite aici o clasa faina :P
[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]

Avatar utilizator
raul_
Senior Member
Mesaje: 711
Membru din: Joi Sep 15, 2005 8:00 am

Mesajde raul_ » Dum Mai 14, 2006 6:18 pm

---
Ultima oară modificat Mar Mai 30, 2006 11:52 pm de către raul_, modificat 1 dată în total.
I have learned that you shouldn't compare yourself to others - they are more screwed up than you think!

Avatar utilizator
assault
Senior Member
Mesaje: 237
Membru din: Sâm Dec 03, 2005 8:00 am

Mesajde assault » Dum Mai 14, 2006 6:28 pm

1.aPParT3 a cerut o clasa n-a zis nik de PHP 5 8O
2.8O
3.functioneaza perfect
4.nu ti-am cerut pararea :roll:
5.citeste pe mysql.com
6.pana la urma urmelor majoritatea serverelor au php 4 si nu php 5

Avatar utilizator
raul_
Senior Member
Mesaje: 711
Membru din: Joi Sep 15, 2005 8:00 am

Mesajde raul_ » Dum Mai 14, 2006 6:35 pm

---
Ultima oară modificat Mar Mai 30, 2006 11:52 pm de către raul_, modificat 1 dată în total.
I have learned that you shouldn't compare yourself to others - they are more screwed up than you think!

Avatar utilizator
assault
Senior Member
Mesaje: 237
Membru din: Sâm Dec 03, 2005 8:00 am

Mesajde assault » Dum Mai 14, 2006 6:52 pm

8O ... whateva`

uite aici un articol despre "eficienta" oop http://www.webmasterstop.com/56.html


Înapoi la “Cod PHP”

Cine este conectat

Utilizatori ce ce navighează pe acest forum: Niciun utilizator înregistrat și 7 vizitatori