$t değişkenine atadığın sql sorgusu çalışmamış başarısız olmuş demektir. bazı nedenleri
1-bağlantı olmamış olabilir
2-sql servis servis dışı olabilir kategori tablosu yok olabilir kategori tablosunda 3-kategori sütunu bulunmuyor olabilir.
--R10.NET; Flood Engellendi -->-> Yeni yazılan mesaj 12:53:39 -->-> Daha önceki mesaj 12:48:27 --
çözemezsen 10. satırın altına bunu yaz hata ekranda çıkacak onu yaz buraya
if(!$t)
{
echo mysql_error();
}
db seçilmedi diyor. PDO kullanıyorum, normal kodla olmuyor..
PDO da select;
$res = $DB->query('SELECT COUNT(*) FROM table');
$num_rows = $res->fetchColumn();
or
$res = $DB->prepare('SELECT COUNT(*) FROM table');
$res->execute();
$num_rows = $res->fetchColumn();
You can use this to ask if data exists or is selected, too:
$res = $DB->query('SELECT COUNT(*) FROM table');
$data_exists = ($res->fetchColumn() > 0) ? true : false;
Or with your variables:
$res = $DB->query('SELECT COUNT(*) FROM table');
$message = ($res->fetchColumn() > 0) ? array('status' => 'ok') : array('status' => 'error');kendi kullandığım database class'ı
<?php
class Database extends PDO
{
public function __construct($DB_TYPE, $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS)
{
parent::__construct($DB_TYPE.':host='.$DB_HOST.';dbname='.$DB_NAME, $DB_USER, $DB_PASS);
//parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTIONS);
}
/**
* select
* @param string $sql An SQL string
* @param array $array Paramters to bind
* @param constant $fetchMode A PDO Fetch mode
* @return mixed
*/
public function select($sql, $array = array(), $fetchMode = PDO::FETCH_ASSOC)
{
$sth = $this->prepare($sql);
foreach ($array as $key => $value) {
$sth->bindValue("$key", $value);
}
$sth->execute();
return $sth->fetchAll($fetchMode);
}
public function say($sql){
$res = $this->prepare("$sql ");
$res->execute();
return $num_rows = $res->fetchColumn();
}
/**
* insert
* @param string $table A name of table to insert into
* @param string $data An associative array
*/
public function insert($table, $data)
{
ksort($data);
$fieldNames = implode('`, `', array_keys($data));
$fieldValues = ':' . implode(', :', array_keys($data));
$sth = $this->prepare("INSERT INTO $table (`$fieldNames`) VALUES ($fieldValues)");
foreach ($data as $key => $value) {
$sth->bindValue(":$key", $value);
}
$sth->execute();
}
/**
* update
* @param string $table A name of table to insert into
* @param string $data An associative array
* @param string $where the WHERE query part
*/
public function update($table, $data, $where)
{
ksort($data);
$fieldDetails = NULL;
foreach($data as $key=> $value) {
$fieldDetails .= "`$key`=:$key,";
}
$fieldDetails = rtrim($fieldDetails, ',');
$sth = $this->prepare("UPDATE $table SET $fieldDetails WHERE $where");
foreach ($data as $key => $value) {
$sth->bindValue(":$key", $value);
}
$sth->execute();
}
/**
* delete
*
* @param string $table
* @param string $where
* @param integer $limit
* @return integer Affected Rows
*/
public function delete($table, $where, $limit = 1)
{
return $this->exec("DELETE FROM $table WHERE $where LIMIT $limit");
}
}//
örnek kullanımı :
<?php
class Note_Model extends Model
{
public function __construct()
{
parent::__construct();
}
public function notListesi()
{
return $this->db->select('SELECT * FROM note WHERE userid = :userid',
array('userid' => $_SESSION['userid']));
}
public function notIcerigi($noteid)
{
return $this->db->select('SELECT * FROM note WHERE userid = :userid AND noteid = :noteid',
array('userid' => $_SESSION['userid'], 'noteid' => $noteid));
}
public function NotOlustur($data)
{
$this->db->insert('note', array(
'title' => $data['title'],
'userid' => $_SESSION['userid'],
'content' => $data['content'],
'date_added' => date('Y-m-d H:i:s') // use GMT aka UTC 0:00
));
}
public function NotKaydet($data)
{
$postData = array(
'title' => $data['title'],
'content' => $data['content'],
);
$this->db->update('note', $postData,
"`noteid` = '{$data['noteid']}' AND userid = '{$_SESSION['userid']}'");
}
public function NotSil ($id)
{
$this->db->delete('note', "`noteid` = {$data['noteid']} AND userid = '{$_SESSION['userid']}'");
}
}bu pdo klasıma entegre edeceğim ama her methodumda farklı bir hata alıyorum. ne önerirsiniz ?
--R10.NET; Flood Engellendi -->-> Yeni yazılan mesaj 14:51:11 -->-> Daha önceki mesaj 14:28:09 --
Fatal error: Using $this when not in object context in E:\localhost\htdocs\models\index_model.php on line 10
10 satir $this-> ile başlayan ; $sth = $this->db->prepare("SELECT userid, role FROM user WHERE
login = :login AND password =

assword");
$sth->execute(array(
':login' => $_POST['login'],
'

assword' => Hash::create('sha256', $_POST['password'], HASH_PASSWORD_KEY)
));
$data = $sth->fetch();
$count = $sth->rowCount();