PDA

View Full Version : تبدیل این کلاس به pdo چجوریه؟



saeed-71
پنج شنبه 19 تیر 1393, 22:37 عصر
<?php
class DB {
/**
* @property Resource The connection field
*/
protected $con;

/**
* Database constructor
* Initializes the object and connects to MySQL
*/
public function __construct() {
$this->con = mysql_connect(HOST, USER, PASS) or die('Connection error');
mysql_select_db(NAME, $this->con) or die('Database error');
$this->Query('SET NAMES \'utf8\'');
mysql_set_charset('utf8');
}

/**
* How many rows affected?
* @return The number of affected rows by the last excuted query
*/
public function AffectedRows() {
if($this->con) {
return mysql_affected_rows($this->con);
}
return 0;
}

/**
* Executes a select query and return the result as standard PHP array
* @param string $query The select query to execute
* @return array The result array
*/
public function ArrayQuery($query) {
$result = array();
if($this->con) {
$rows = $this->Query($query);
if($rows && mysql_num_rows($rows) > 0) {
while($row = mysql_fetch_assoc($rows)) {
$result[] = $row;
}
}
}
return $result;
}

/**
* Escape a value to use safely in queries
* @param string $value The value to escape
* @return string|boolean The escaped value if connection exists, false otherwise
*/
public function Escape($value) {
if($this->con) {
return mysql_real_escape_string($value, $this->con);
}
return false;
}

/**
* Execute a query and return the result as a MySQL resource
* @param string $query The query to execute
* @return resource|boolean The result resource if connection exists, false otherwise
*/
public function Query($query) {
if($this->con) {
return mysql_query($query, $this->con);
}
return false;
}
}


نتونستم تبدیل کنم
اینجوری شد


<?php
class DB {
/**
* @property Resource The connection field
*/
protected $con;
private $dsn;

/**
* Database constructor
* Initializes the object and connects to MySQL
*/
public function __construct() {

/*$this->con = mysql_connect(HOST, USER, PASS) or die('Connection error');
mysql_select_db(NAME, $this->con) or die('Database error');
$this->Query('SET NAMES \'utf8\'');
mysql_set_charset('utf8');*/

$this->dsn = "mysql:host=HOST;dbname=NAME";
$this->con =new PDO($this->dsn,USER,PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES utf8'));
}

/**
* How many rows affected?
* @return The number of affected rows by the last excuted query
*/
public function AffectedRows() {
if($this->con) {
return mysql_affected_rows($this->con);
//return $this->query("SELECT FOUND_ROWS()")->fetchColumn();
}
return 0;
}



}

sh.n.n786
پنج شنبه 19 تیر 1393, 23:04 عصر
درود و ...
نیازی به تبدیل نیست من این شکلی نوشتم.



class DB extends PDO {

/**
* Database Informations
* @var type
*/
public $connection;
public $host;
public $username;
public $password;
public $db;
public $dsn;

/**
* PDO Database Connection
* @param type $username
* @param type $password
* @param type $host
* @param type $db
*/
public function __construct() {
if ($this->connection == NULL && !is_resource($this->connection)) {
$this->host = DB_HOSTNAME;
$this->username = DB_USERNAME;
$this->password = DB_PASSWORD;
$this->db = DB_TRUENAME;
$this->dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->db . ';';
try {
$this->connection = parent::__construct($this->dsn, $this->username, $this->password);
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->prepare("SET NAMES 'utf8'");
} catch (PDOException $err) {
$this->connection = NULL;
echo '<pre>';
print_r($err);
echo '</pre>';
die;
}
}
}

public function __destruct() {
$this->PDOClose();
}

/**
* PDO Close Connaction
*/
public function PDOClose() {
$this->connection = NULL;
return;
}

}

saeed-71
پنج شنبه 19 تیر 1393, 23:50 عصر
اون سه تابع اخرو کی بکار میبرین؟
چرا تو خط 31 گرفتید utf8 رو prepare کردین؟

saeed-71
جمعه 20 تیر 1393, 00:18 صبح
نحوه استفاده از اون کلاس بالا تو این کلاس چجوریه؟
یدونشو مثلا بزنید بقیشو انجام میدم



<?php
class Product extends DB implements CRUD {

public function Create($params = array()) {
if(isset($params['name'], $params['cat_id'])) {
$name = $this->Escape($params['name']);
$cat_id = $this->Escape($params['cat_id']);
$description = (isset($params['description']) ? '\'' . $this->Escape($params['description']) : 'NULL');
$price = (isset($params['price']) ? $this->Escape($params['price']) : 0);
$hidden = (isset($params['hidden']) ? $this->Escape($params['hidden']) : 0);
$this->Query("INSERT INTO `products` VALUES (NULL,'{$cat_id}','{$name}',{$description},'{$pric e}','{$hidden}')");
}
return $this->AffectedRows();
}

public function Read($params = array()) {
if(count($params) == 0) {
return $this->ArrayQuery('SELECT * FROM `product` WHERE (`hidden`=\'0\') ORDER BY `name`');
}
if(isset($params['id'])) {
$id = $this->Escape($params['id']);
$product = $this->ArrayQuery("SELECT * FROM `product` WHERE (`id`='{$id}' AND `hidden`='0') ORDER BY `name`");
return (count($product) > 0 ? $product[0] : $product);
}
$query = 'SELECT * FROM `product` WHERE (1=1';
if(isset($params['cat_id'])) {
$cat_id = $this->Escape($params['cat_id']);
$query .= " AND `cat_id`='{$cat_id}'";
}
if(isset($params['name'])) {
$name = $this->Escape($params['name']);
$query .= " AND `name` LIKE '%{$name}%'";
}
if(isset($params['description'])) {
$description = $this->Escape($params['description']);
$query .= " AND `description` LIKE '%{$description}%'";
}
if(isset($params['price'])) {
$price = $this->Escape($params['price']);
$query .= " AND `price`='{$price}'";
}
$query .= ' AND `hidden`=\'0\') ORDER BY `name`';
return $this->ArrayQuery($query);
}

public function Update($params = array()) {
if(isset($params['id'])) {
$id = $this->Escape($params['id']);
$query = 'UPDATE `product` SET `id`=`id`';
if(isset($params['name'])) {
$name = $this->Escape($params['name']);
$query .= ",`name`='{$name}'";
}
if(isset($params['description'])) {
$description = $this->Escape($params['description']);
$query .= ",`description`='{$description}'";
}
if(isset($params['price'])) {
$price = $this->Escape($params['price']);
$query .= ",`price`='{$price}'";
}
if(isset($params['hidden'])) {
$hidden = $this->Escape($params['hidden']);
$query .= ",`hidden`='{$hidden}'";
}
$query .= " WHERE (`id`='{$id}')";
$this->Query($query);
}
return $this->AffectedRows();
}

public function Delete($id) {
$id = $this->Escape($id);
$this->Query("DELETE FROM `product` WHERE (`id`='{$id}')");
return $this->AffectedRows();
}
}
?>

saeed-71
جمعه 20 تیر 1393, 15:43 عصر
کسی نیست برای قسمت read توضیح بده؟
بقیشو میتونم انجام اگه یکیو بفهمم

bagherok
جمعه 20 تیر 1393, 18:43 عصر
<?php

class db extends PDO {

public function __construct() {
$dsn = 'mysql:dbname=pdo;host=localhost';
$user='root';
$pass='';

parent::__construct($dsn,$user,$pass);
}

public function Create($params = array()) {

if(isset($params['name'], $params['cat_id'])) {
$cat_id = $params['cat_id'];
$name = $params['name'];
$description = (isset($params['description']) ? $params['description'] : 'NULL');
$price = (isset($params['price']) ? $params['price'] : 0);
$hidden = (isset($params['hidden']) ? $params['hidden'] : 0);

$stmt=$this->prepare("INSERT INTO `product` VALUES (NULL,:cat_id,:name,:description,:price,:hidden)");
$stmt->execute(array(
':cat_id' =>$cat_id,
':name' =>$name,
':description' =>$description,
':price' =>$price,
':hidden' =>$hidden,
));
}
return $stmt->rowCount();
}


public function Read($params = array()) {
if(count($params) == 0) {
$stmt=$this->query('SELECT * FROM `product` WHERE (`hidden`=\'0\') ORDER BY `name`');
return $stmt->fetchall(PDO::FETCH_ASSOC);
}
if(isset($params['id'])) {
$id = $params['id'];
$stmt = $this->prepare("SELECT * FROM `product` WHERE (`id`=:id AND `hidden`='0') ORDER BY `name`");
$stmt->execut(array(
":id="=>$id
));
$product=$stmt->fetchall(PDO::FETCH_ASSOC);
return (count($product) > 0 ? $product[0] : $product);
}

$query = 'SELECT * FROM `product` WHERE (1=1 ';
$param = array();
if(isset($params['cat_id'])) {
$query .= " AND `cat_id`= ? ";
$param[]=$params['cat_id'];
}
if(isset($params['name'])) {
$query .= " AND `name` LIKE ? ";
$param[]='%'.$params['name'].'%';
}
if(isset($params['description'])) {
$query .= " AND `description` LIKE ? ";
$param[]='%'.$params['description'].'%';

}
if(isset($params['price'])) {
$query .= " AND `price`= ? ";
$param[]=$params['price'];
}

$query .= ' AND `hidden`=\'0\') ORDER BY `name` ';
$stmt = $this->prepare($query);
$stmt->execute($param);
return $stmt->fetchall(PDO::FETCH_ASSOC);
}


public function Update($params = array()) {
if(isset($params['id'])) {
$param[] = $params['id'];
$query = 'UPDATE `product` SET `id`= ?';


if(isset($params['cat_id'])) {
$param[] = $params['cat_id'];
$query .= ",`cat_id`= ?";
}

if(isset($params['name'])) {
$param[] = $params['name'];
$query .= ",`name`= ?";
}
if(isset($params['description'])) {
$param[]= $params['description'];
$query .= ",`description`= ?";
}
if(isset($params['price'])) {
$param[]= $params['price'];
$query .= ",`price`= ?";
}
if(isset($params['hidden'])) {
$param[]=$params['hidden'];
$query .= ",`hidden`= ?";
}
$param[] = $params['id'];
$query .= " WHERE (`id`= ?)";
$stmt = $this->prepare($query);
$stmt->execute($param);
}
return $stmt->rowCount();
}

public function Delete($id) {
$stmt = $this->prepare("DELETE FROM `product` WHERE `id`=:id ");
$stmt->execute(array(
":id"=>$id,
));

return $stmt->rowCount();
}



}


///////////////


$db=new db();

$data=array();
$result = $db->Read($data);
echo '<pre>' . print_r($result,true) .'</pre>';

$data=array(
'name' =>'name1',
'description'=>'des1',
);
$result = $db->Read($data);
echo '<pre>' . print_r($result,true) .'</pre>';


$data=array(
'cat_id' =>3,
'name' =>'name3',
'description'=>'des3',
'price' =>3000,
'hidden' =>0,
);
if($db->create($data)>0){
echo 'ok';
}


$data=array(
'id' =>1,
'cat_id' =>10,
);

if($db->update($data)>0){
echo 'ok';
}


if($db->Delete(3)>0){
echo 'ok';
}

bagherok
جمعه 20 تیر 1393, 19:09 عصر
مشکلی که نیست؟؟

saeed-71
یک شنبه 22 تیر 1393, 10:46 صبح
حل شد ......

Mohammadsgh
یک شنبه 22 تیر 1393, 12:07 عصر
میتونید تو پروژه هاتون از این کلاس استفاده کنید خیلی سریع و امنه.اینم لینکش (http://medoo.in/)