سلام دوستان بهترین کلاسی که میتونم تو پروژه برای کوئری به دیتابیس استفاده کنم رو لطفا لینک دانلود بزارید.
تشکر.
Printable View
سلام دوستان بهترین کلاسی که میتونم تو پروژه برای کوئری به دیتابیس استفاده کنم رو لطفا لینک دانلود بزارید.
تشکر.
الان که mysql extension کاملا depricate شده و همه دارند از mysqli و pdo استفاده میکنند که خودشون کلاس های کاملی توی php به صورت built in هستند ! هیچ نیازی به کلاس خاصی ندارید. در ضمن سرعت mysqli از pdo بیشتره ولی pdo انعطافپذیری بیشتری داره و خیلی راحت میشه بعدا به DBMS های دیگه port کردش.
اساتید لطفا یه نگاه به این کلاس بندازید اگه خوب بود بگید.ممنون میشم.
کد HTML:http://www.phpclasses.org/package/6620-PHP-Compose-MySQL-queries-chaining-functions.html
من باز هم متوجه نمیشم وقتی mysqli به این خوبی پیاده سازی شده چه نیازی به کلاس دارین ؟
میشه مثال بزنید؟
آموزش کار با کلاس mysqli :
http://www.pontikis.net/blog/how-to-...why-you-should
http://codular.com/php-mysqli
یه نمونه کلاس کار با دیتابیس با pdo نوشتم امیدوارم به کارتون بیاد
class.php
<?php
class content
{
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
private $stmt;
private $dbh;
private $error;
public function __construct(){
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
);
// Create a new PDO instanace
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
/*** close the database connection ***/
//$this->dbh = null;
}
// Catch any errors
catch(PDOException $e){
$this->error = $e->getMessage();
}
}
// query
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function rowCount(){
return $this->stmt->rowCount();
}
public function lastInsertId(){
return $this->dbh->lastInsertId();
}
public function beginTransaction(){
return $this->dbh->beginTransaction();
}
public function endTransaction(){
return $this->dbh->commit();
}
public function cancelTransaction(){
return $this->dbh->rollBack();
}
public function debugDumpParams(){
return $this->stmt->debugDumpParams();
}
}
?>
مثال نحوه ی فراخوانی در حلقه
function slider()
{
$class=new content;
$qr=$class->query('select * from `tbl_slider` order by `id` desc ');
$row=$class->resultset();
foreach($row as $key => $value)
{
echo'
<div class="slide">
<img src="'.inc.'imgsize.php?w=370&h=280&img='.upload.' '.$row[$key]['pic'].'" alt="'.$row[$key]['title'].'"/>
</div>
';
}
}
مثال بدون حلقه
function slider()
{
$class=new content;
$qr=$class->query('select * from `tbl_slider` ');
$row=$class->single();
echo'
<div class="slide">
<img src="'.inc.'imgsize.php?w=370&h=280&img='.upload.' '.$row['pic'].'" alt="'.$row['title'].'"/>
</div>
';
}
با استفاده از شرط
function showproduct()
{
$class=new content;
$qr=$class->query('select * from `tbl_activity` where `id`=:id ');
$class->bind(':id',$_GET['id']);
$row=$class->single();
echo'
<h2 class="titleah">'.$row['title'].'</h2>
';
}
مثال update
$class=new content;
$class->query("UPDATE `tbl_admin` SET
`fullname` = :fullname,
`user` = :user,
`pass` = :pass,
`superadmin` = :superadmin,
`pic` = :dir,
where `aid`=:edit ");
$class->bind(':fullname',$fullname);
$class->bind(':user',$user);
$class->bind(':pass',$pass);
$class->bind(':superadmin',$superadmin);
$class->bind(':dir',$dir);
$class->bind(':edit',$edit);
$class->execute();
مثال insert
$class=new content;
$class->query("INSERT INTO `tbl_admin`
(`aid` ,`user`,`pass`,`fullname`,`superadmin` ,`pic`)
VALUES (NULL , ?,?,?,?,?)");
$class->bind(1,!is_null($user) ? $user :'');
$class->bind(2,!is_null($pass) ? $pass :'');
$class->bind(3,!is_null($fullname) ? $fullname :'');
$class->bind(4,!is_null($superadmin) ? $superadmin :'');
$class->bind(5,!is_null($dir) ? $dir :'');
$class->execute();
مثال delete
$class=new content;
$class->query("delete from `tbl_admin` where `aid`=:aid");
$class->bind(':aid',$aid);
$class->execute();
اگه هم مشکلی داره می شه کاملتر و بهترش کرد بگید ممنون
اقا سروش به نظرم این یک کاره اضافیه که یک کلاس رو دوباره درون یک کلاس به کار ببرید !
public function execute(){ return $this->stmt->execute(); }
public function rowCount(){ return $this->stmt->rowCount();
}
خب چه کاریه ؟
اگر قصدتون راحتتر شدن کارها باشه،خب با این کار،به نتیجه ایی نمرسین،چون شما درهرحال باید متد رو صدا بزنید
ولی میتونید یک کلاس بنویسید که موقع ساخت به دیتابیس وصل بشه
یک متد برای کوئری هایی که مقدار بازگشتی ندارند،مثل insert,update,delete
یک متد برای Select ها و...
یک متد برای بستن کانکشن
حالا میتونید برای این متد ها ارگومان هایی بزارید که کار رو راحتتر بکنه !
مقلا برای متد select یک ارگومان برای خوده کوئری،یکی برای متغیر های کوئری
با توجه به اینکه این پست برای چند سال پیش است، میخواستم ببینم در حال حاضر پیشنهاد دوستان در همین مورد چی هست ؟
من یه همچین ساختاری برای هر جدولم دارم..
شاید به دردت بخوره
namespace entities{
class ticket {
private $mysqli;
public $id;
public $suggested_time;
public $error;
public $flage = true;
private $tbl_name ;
public function __construct($commit = FALSE) {
$this->tbl_name = TBL_PREFIX.'tickets';
$this->mysqli = new \MySQLi(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$this->mysqli->set_charset("utf8");
if ($commit)
$this->mysqli->query('start transaction;');
}
public function commit($var) {
if ($var)
$this->mysqli->query('commit;');
elseif (!$var)
$this->mysqli->query('rollback;');
}
public function save(
$id=NULL
,$title=NULL
,$body=NULL
,$register_user_id=NULL
,$register_date=NULL
,$ticket_tags=NULL
,$state_id=NULL
,$is_emergency=NULL
,$role_id=NULL
,$close_date=NULL
,$resource_id=NULL
,$deadline=NULL
,$suggested_time=NULL
,$parent_id=NULL
,$duration=NULL
,$worth=NULL
,$link=NULL
,$color=NULL
,$upload=NULL
,$concat=TRUE
,$old_ticket_id=NULL){
if($id==NULL):
$sql="INSERT INTO $this->tbl_name(
title ";
if($body != NULL) $sql .= " ,body";
if($register_user_id != NULL) $sql .= " ,register_user_id" ;
if($register_date != NULL) $sql .= " ,register_date" ;
if($ticket_tags != NULL) $sql .= " ,ticket_tags" ;
if($state_id != NULL) $sql .= " ,state_id" ;
if($is_emergency != NULL && $is_emergency != 'FALSE') $sql .= " ,is_emergency" ;
elseif($is_emergency == 'FALSE') $sql .= " ,is_emergency" ;
if($role_id != NULL) $sql .= " ,role_id" ;
if($close_date != NULL) $sql .= " ,close_date" ;
if($resource_id != NULL) $sql .= " ,resource_id" ;
if($deadline != NULL) $sql .= " ,deadline" ;
if($suggested_time != NULL) $sql .= " ,suggested_time" ;
if($parent_id != NULL ) $sql .= " ,parent_id" ;
if($duration != NULL) $sql .= " ,duration" ;
if($worth != NULL) $sql .= " ,worth" ;
if($link != NULL) $sql .= " ,link" ;
if($color != NULL) $sql .= " ,color" ;
if($old_ticket_id != NULL) $sql .= " ,old_ticket_id" ;
$sql .= " ) VALUES (
'$title' ";
if($body != NULL) $sql .= " ,'$body'" ;
if($register_user_id != NULL) $sql .= " , $register_user_id" ;
if($register_date != NULL) $sql .= " ,'$register_date'" ;
if($ticket_tags != NULL) $sql .= " ,'$ticket_tags'" ;
if($state_id != NULL) $sql .= " , $state_id" ;
if($is_emergency != NULL && $is_emergency != 'FALSE' ) $sql .= " , $is_emergency" ;
elseif($is_emergency == 'FALSE') $sql .= " , 0 " ;
if($role_id != NULL) $sql .= " , $role_id" ;
if($close_date != NULL) $sql .= " , '$close_date' " ;
if($resource_id != NULL) $sql .= " , $resource_id" ;
if($deadline != NULL) $sql .= " , '$deadline' " ;
if($suggested_time != NULL) $sql .= " ,'$suggested_time' " ;
if($parent_id != NULL ) $sql .= " , $parent_id" ;
if($duration != NULL) $sql .= " , $duration " ;
if($worth != NULL) $sql .= " , $worth" ;
if($link != NULL) $sql .= " , '$link'" ;
if($color != NULL ) $sql .= " , '$color'" ;
if($old_ticket_id != NULL ) $sql .= " , $old_ticket_id" ;
$sql .= " )";
elseif($id != NULL): //upduration
$sql="UPDATE $this->tbl_name SET id = $id ";
if($title != NULL) $sql .= " , title = '$title'";
if($body != NULL && $body != 'NULL')$sql .= " , body = '$body'";
if($body == 'NULL') $sql .= " , body = NULL ";
if($register_user_id != NULL) $sql .= " , register_user_id = $register_user_id";
if($register_date != NULL) $sql .= " , register_date = '$register_date'";
if($ticket_tags != NULL) $sql .= " , ticket_tags = '$ticket_tags'";
if($state_id != NULL) $sql .= " , state_id = $state_id";
if($is_emergency != NULL) $sql .= " , is_emergency = $is_emergency";
if($role_id != NULL) $sql .= " , role_id = $role_id";
if($close_date != NULL) $sql .= " , close_date = '$close_date'";
if($resource_id != NULL) $sql .= " , resource_id = $resource_id";
if($deadline != NULL && $deadline != 'FALSE') $sql .= " , deadline = '$deadline'";
elseif($deadline == 'FALSE') $sql .= " , deadline = NULL";
if($suggested_time != NULL) $sql .= " , suggested_time = $suggested_time";
if($parent_id != NULL ) $sql .= " , parent_id = $parent_id";
if($duration != NULL) $sql .= " , duration = $duration";
if($worth != NULL) $sql .= " , worth = $worth";
if($link != NULL && $link != 'FALSE') $sql .= " , link = '$link'";
elseif($link == 'FALSE') $sql .= " , link = NULL ";
if($color != NULL && $color != 'FALSE') $sql .= " , color = '$color'";
elseif($color == 'FALSE') $sql .= " , color = '0' ";
if($upload != NULL && $concat) $sql .= " , upload = CONCAT(upload ,',$upload') ";
elseif($upload != NULL && !$concat)$sql .= " , upload = '$upload' ";
if($old_ticket_id != NULL) $sql .= " , old_ticket_id = $old_ticket_id";
$sql .= " WHERE id = $id ";
endif;
$this->mysqli->query($sql);
$this->error = $this->mysqli->error;
if (!empty($this->mysqli->error)) {
$this->flage = FALSE;
}
if ($id == NULL)
return $this->mysqli->insert_id;
else
return $this->mysqli->error;
}
public function delete($ids=NULL,$role_id=NULL,$state_id=NULL)
{
$sql = "DELETE FROM $this->tbl_name WHERE 1=1 ";
if($ids != NULL) $sql .= " AND $this->tbl_name.id in($ids)";
if($role_id != NULL) $sql .= " AND $this->tbl_name.role_id = $role_id";
if($state_id != NULL) $sql .= " AND ($this->tbl_name.state_id = $state_id)";
$this->mysqli->query($sql);
}
public function get(
$id=NULL
,$title=NULL
,$body=NULL
,$register_user_id=NULL
,$register_date=NULL
,$ticket_tags=NULL
,$state_id=NULL
,$is_emergency=NULL
,$role_id=NULL
,$close_date=NULL
,$resource_id=NULL
,$deadline=NULL
,$suggested_time=NULL
,$parent_id=NULL
,$duration=NULL
,$worth=NULL
,$show = FALSE
,$plus = NULL
,$link = NULL
,$color=NULL
,$old_ticket_id=NULL)
{
$sql="SELECT * FROM $this->tbl_name WHERE 1=1";
$custom = '';
if($show==TRUE){
$custom .= " INNER JOIN ".TBL_PREFIX."roles ON ".TBL_PREFIX."tickets.role_id = ".TBL_PREFIX."roles.id ";
$sql="SELECT $this->tbl_name.id , $this->tbl_name.title , body , register_user_id ,
register_date , role_id , ".TBL_PREFIX."roles.title as role_title , suggested_time ,
close_date , resource_id ,is_emergency , ticket_tags , worth , state_id , color , deadline ,
duration
FROM $this->tbl_name $custom WHERE 1=1 ";
}
if($id != NULL) $sql .= " AND $this->tbl_name.id = $id";
if($title != NULL) $sql .= " AND $this->tbl_name.title = '$title'";
if($body != NULL) $sql .= " AND $this->tbl_name.body = '$body'";
if($register_user_id != NULL) $sql .= " AND $this->tbl_name.register_user_id = $register_user_id";
if($register_date != NULL) $sql .= " AND $this->tbl_name.register_date = '$register_date'";
if($role_id != NULL) $sql .= " AND $this->tbl_name.role_id = $role_id";
if($close_date != NULL) $sql .= " AND $this->tbl_name.close_date = $close_date";
if($resource_id != NULL) $sql .= " AND $this->tbl_name.resource_id = '$resource_id'";
if($ticket_tags != NULL) $sql .= " AND $this->tbl_name.ticket_tags = '$ticket_tags'";
if($worth != NULL) $sql .= " AND $this->tbl_name.worth = $worth";
if($state_id != NULL) $sql .= " AND ($this->tbl_name.state_id = $state_id)";
if($is_emergency != NULL) $sql .= " AND $this->tbl_name.is_emergency = $is_emergency";
if($deadline != NULL) $sql .= " AND $this->tbl_name.deadline = '$deadline'";
if($suggested_time != NULL) $sql .= " AND $this->tbl_name.suggested_time = $suggested_time";
if($parent_id != NULL && $parent_id != -1 ) $sql .= " AND $this->tbl_name.parent_id = $parent_id ";
elseif($parent_id == -1 ) $sql .= " AND ($this->tbl_name.parent_id IS NULL OR $this->tbl_name.parent_id = 0 )";
if($duration != NULL) $sql .= " AND $this->tbl_name.duration = $duration";
if($link != NULL) $sql .= " AND $this->tbl_name.link = '$link'";
if($color != NULL) $sql .= " AND $this->tbl_name.color = '$color'";
if($old_ticket_id != NULL) $sql .= " AND $this->tbl_name.old_ticket_id = '$old_ticket_id'";
return $this->mysqli->query($sql.$plus." order by is_emergency DESC ");
}
}
}
یه framework یاد بگیر دیگه این همه داستان نداری خیلی راحت پیشنهاد اول CI هست و وقتی که خوب مسلط شدی yii2
البته همون زمانی که این پست مطرح شده بود کلاس های زیادی بودن که به صورت یک Wrapper عمل میکردن و کار را کمی راحت تر میکردن الان هم هستن :
https://meekro.com/
https://github.com/ThingEngineer/PHP...Database-Class
ولی با زهم میشه از همون Native ها استفاده کرد.
اگه قصد از یادگیری Framework ها استفاده از Database Wrapper های اون ها هست به نظرم اشتباهه ! همچنان Pure PHP بهترین و سریعترینه !نقل قول:
یه framework یاد بگیر دیگه این همه داستان نداری خیلی راحت پیشنهاد اول CI هست و وقتی که خوب مسلط شدی yii2