PDA

View Full Version : مشكل در PDO



bikran
سه شنبه 22 اسفند 1391, 20:06 عصر
با سلام

وقتي كد زير رو اجرا ميكنم اين خطا رو ميده


Fatal error: Call to undefined function setAttribute() in C:\wamp\www\Example\pdo\db1.php on line 20

اينم كدم:


<?php
class db
{
private $connection;
private $host="localhost";
private $user="root";
private $password="";
private $dbname="users";
public function __construct()
{
$this->connect();
}
private function connect()
{
if(!$this->connection)
{
$this->connection= new PDO("mysql:dbname=$this->dbname;host=$this->host",$this->user,$this->password);
$this->connection=setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->connection=prepare('SET NAMES \'utf8\'');
$this->connection=prepare('select * from users');
}
}
}
$mydb=new db();
?>

desatir7316
سه شنبه 22 اسفند 1391, 20:39 عصر
مشکلت توی ایناست:

$this->connection=setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->connection=prepare('SET NAMES \'utf8\'');
$this->connection=prepare('select * from users')

یاید اینجوری بنویسی:

$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->connection->prepare('SET NAMES \'utf8\'');
$this->connection->prepare('select * from users');

گرفتی؟ یعنی به جای = باید از <- استفاده کنی

bikran
چهارشنبه 23 اسفند 1391, 10:19 صبح
با کمک شما دوستان دارم کلاس PDO رو تکمیل میکنم . یک تابع نوشتم برای escape که وقتی رشته ی SQL رو ازش عبور میدم خطا میده ولی اگه عبور ندم خطا نمیده!


<?php
class db
{
private $connection;
private $host="localhost";
private $user="root";
private $password="";
private $dbname="mydb";
public function __construct()
{
$this->connect();
}
private function connect()
{
if(!$this->connection)
{
try
{
$this->connection= new PDO("mysql:dbname=$this->dbname;host=$this->host",$this->user,$this->password);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->connection->exec('SET NAMES \'utf8\'');
}
catch(PDOException $e)
{
echo "I'm sorry, Dave. I'm afraid I can't do that.";
file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}
}
}
public function query($query){
$this->connect();
$sth=$this->connection->query($query);
return $sth;
}
public function escape($value)
{
$this->connect();
if(get_magic_quotes_gpc())
{
$value=stripslashes($value);
}
return $this->connection->quote($value);
}
}
$mydb=new db();
$query=$mydb->escape("select * from tell");
$row=$mydb->query($query);
print_r ($row->FETCH(PDO::FETCH_ASSOC));
?>


اینم خطا

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''select * from tell'' at line 1' in C:\Program Files\EasyPHP-12.1\www\Example\pdo.php:33 Stack trace: #0 C:\Program Files\EasyPHP-12.1\www\Example\pdo.php(33): PDO->query(''select * from ...') #1 C:\Program Files\EasyPHP-12.1\www\Example\pdo.php(48): db->query(''select * from ...') #2 {main} thrown in C:\Program Files\EasyPHP-12.1\www\Example\pdo.php on line 33

MMSHFE
پنج شنبه 24 اسفند 1391, 08:50 صبح
دوست عزیز، شما نباید کل Query رو برای Escape بفرستین بلکه یکی یکی باید مقادیر رو براش بفرستین و نتیجه Escape شده رو توی Query بگذارین. طبیعیه که با فرستادن کل Query برای Escape پیغام خطا بگیرین چون ' و... که جزو خود Query هستن رو هم Escape میکنه. این کارکترها نباید توی مقادیر باشن نه اینکه کلاً توی Query نباشن.