Basically, this is what I wanted to do, how does it work?
public function get_product_category(){
$id = $this->uri->segment(3);
$query = $this->db->query('SELECT * FROM products where category_id = $id ');
return $query->row();
}
You could use Query Builder Class
public function get_product_category(){
$this->db->where('category_id', $this->uri->segment(3));
$query = $this->db->get('products');
return $query->row();
// return $query->row_array();
}
Or
public function get_product_category(){
$this->db->select('*');
$this->db->from('products');
$this->db->where('category_id', $this->uri->segment(3));
$query = $this->db->get();
return $query->row();
// return $query->row_array();
}
I would autoload the database library.
Related
I'm making a social network's login page but when I login a get the error above. My db.php is (i use pdo):
<?php
class DB {
private static function connect() {
$pdo = new PDO('mysql:host=127.0.0.1;dbname=social;charset=utf8', 'danny', 'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
}
public static function query($query, $params = array()) {
$statement = self::connect()->prepare($query);
$statement->execute($params);
if (explode(' ', $query)[0] == 'SELECT') {
$data = $statement->fetchAll();
return $data;
}
}
}
?>
Called from
$user_id = DB::query('SELECT id
FROM users
WHERE email=:email')[0]['id'];
DB::query('INSERT INTO login_tokens
VALUES(\'\', :token, :user_id)',
array(':token'=>sha1($token), ':user_id'=>$user_id));
Your class has many issues. to fix them:
class DB {
protected static $pdo;
public static function connect() {
static::$pdo = new PDO('mysql:host=127.0.0.1;dbname=social;charset=utf8', 'danny', 'Dani2034');
static::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public static function query($query, $params = array()) {
$statement = self::$pdo->prepare($query);
$statement->execute($params);
return $statement;
}
}
DB::connect(); // called only once
$user_id = DB::query('SELECT id FROM users WHERE email=?', array($email))->fetchColumn();
DB::query('INSERT INTO login_tokens VALUES(null, ?, ?)', array(sha1($token), $user_id));
I am using this code in my Grid.php to display records from a single tabel 'paypal_payment_transaction':
protected function _prepareCollection()
{
$db = Mage::getSingleton('core/resource')->getConnection('core_write');
$query = "SELECT transaction_id,txn_id,additional_information,created_at,user_id,reference_txn
FROM `paypal_payment_transaction`
LIMIT 0 , 30";
$result = $db->query($query);
// Get count of affected rows
$affected_rows = $result->rowCount();
$orders = $result->fetchAll($sql);
foreach($orders as $order)
{
echo "<pre>"; print_r($order);
}
}
I need this query in magento way:
may be like this
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('sales/order_collection')
SOME QUERY TO SELECT RECORD FROM TABEL 'paypal_payment_transaction'
;
$this->setCollection($collection);
parent::_prepareCollection();
return $this;
}
So that I can display it in grid accordingly i.e. in :
protected function _prepareColumns()
{
-------------------
------------------
}
Try this -
protected function _prepareCollection()
{
$collection = Mage::getModel('paypal/payment_transaction')->getCollection();
$this->setCollection($collection);
parent::_prepareCollection();
return $this;
}
you can try the following code
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('payment/transaction')
$Collection->addFieldToSelect('transaction_id');
$this->setCollection($collection);
parent::_prepareCollection();
return $this;
}
Thanks
I have very simple users database: user_id, user_name, user_email
My model this:
class Users extends CI_Model {
private $table;
private $table_fields;
private $table_fields_join;
function __construct() {
parent::__construct();
$this->table = 'users';
$this->table_fields = array(
$this->table.'.user_id',
$this->table.'.user_name',
$this->table.'.user_email'
);
$this->table_fields_join = array();
}
function select(){
$this->db->select(implode(', ', array_merge($this->table_fields, $this->table_fields_join)));
$this->db->from($this->table);
$query = $this->db->get();
if($query->num_rows() > 0){
return $query->result();
} else {
return false;
}
}
function insert($data) {
$data = array(
'user_name' => $data['user_name'],
'user_email' => $data['user_email']
);
$this->db->insert($this->table, $data);
}
My controller this:
class Users extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('users');
}
public function select(){
$data['query'] = $this->users->select();
$data = json_encode($data['query']);
echo $data;
}
public function insert($json){
$data = json_decode($json);
$this->users->insert($data);
}
}
And this is my routing.php:
$route['default_controller'] = 'Welcome';
$route['users'] = 'users/select';
$route['users/insert/:(any)'] = 'users/insert';
I would like that 127.0.0.1/users/select give json.
Example: [{"user_name":"user1","user_email":"user#user.de"}]
This JSON insert my table: 127.0.0.1/users/insert/[{"user_name":"user1","user_email":"user#user.de"}]
But my code is not working. :-(
You want to return json object in response, so it's required to set json type in response header. As given here
public function select(){
$data['query'] = $this->users->select();
$this->output
->set_content_type('application/json')
->set_output(json_encode($data['query']));
}
It is required to encode part as below for insert part. so you can use this generated url to call your insert.
site_url('usres/insert/'.urlencode('[{"user_name":"user1","user_email":"user#user.de"}]'));
your insert route should be as
$route['users/insert/:(any)'] = 'users/insert/$1';
your insert method should be updated as
public function insert($json){
$json = urldecode($json);
$data = json_decode($json);
$this->users->insert($data);
}
}
I've been having a problem with a table model recently and I can't really explain why this happens. I want to select everything from a table and return it in a descending order. But when I try to display it nothing shows up:
Code:
<?php
//Will be used at a later date
namespace Blog\Model\Table;
use Zend\Db\TableGateway\TableGateway;
class Blog extends TableGateway
{
public function __construct($adapter)
{
parent::__construct('posts', $adapter);
}
public function displayPosts()
{
$adapter = $this->getAdapter();
$result = $adapter->query('SELECT * FROM `posts` ORDER BY `date_added` DESC');
return $result;
}
}
Result:
But when I add an $id argument it shows the data:
Code:
<?php
//Will be used at a later date
namespace Blog\Model\Table;
use Zend\Db\TableGateway\TableGateway;
class Blog extends TableGateway
{
public function __construct($adapter)
{
parent::__construct('posts', $adapter);
}
public function displayPosts($id = 6)
{
$adapter = $this->getAdapter();
$result = $adapter->query('SELECT * FROM `posts` WHERE `post_id` = ?', array($id));
return $result;
}
}
Result:
It's weird that it works with an argument and not without, any ideas?
You needed to order it by post_id
public function displayPosts()
{
$adapter = $this->getAdapter();
$result = $adapter->query('SELECT * FROM `posts`')->order(array('post_id' => 'DESC'));
return $result;
}
OK, I am new to AMFPHP. I can do simple sql statements with no problem.
Here are my classes of Schools and Locations:
class Schools {
public $id;
public $district_id;
public $school_name;
// explicit actionscript class
var $_explicitType = "components.Schools";
}
class Locations {
public $id;
public $school_id;
public $school_address;
public $icon_id;
// explicit actionscript class
var $_explicitType = "components.Locations";
}
This simple sql statement works with no issues:
/**
* Retrieves schools data
* #returns id, district_id, school_name
*/
public function getSchools() {
//connect to the database.
$mysql = mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD);
mysql_select_db(DATABASE_NAME);
//retrieve all rows
$query = "SELECT * FROM schools ORDER BY school_name";
$result = mysql_query($query);
$ret = array();
while ($row = mysql_fetch_object($result)) {
$tmp = new Schools();
$tmp->id = $row->id;
$tmp->district_id = $row->district_id;
$tmp->school_name = $row->school_name;
$ret[] = $tmp;
}
mysql_free_result($result);
return $ret;
}
However, I am having a hard time making a "somewhat" complex sql statement work properly.
/**
* Retrieves schools with locations data
* #returns school_name, school_address
*/
public function getSchoolsLocations() {
//connect to the database.
$mysql = mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD);
mysql_select_db(DATABASE_NAME);
//retrieve all schools with their locations
$query = "SELECT schools.school_name AS SNAME, school_address AS SLOC FROM schools, locations WHERE schools.id = locations.school_id";
$result = mysql_query($query);
$ret = array();
while ($row = mysql_fetch_object($result)) {
$tmp = new Schools();
$tmp->id = $row->id;
$tmp->school_name = $row->SNAME;
$ret[] = $tmp;
$tmp2 = new Locations();
$tmp2->school_id = $row->school_id;
$tmp2->school_address = $row->SLOC;
$ret2[] = $tmp2;
array_splice($ret, count($ret), 0, $ret2);
}
mysql_free_result($result);
return $ret;
}
BTW, SELECT schools.school_name AS SNAME, school_address AS SLOC FROM schools, locations WHERE schools.id = locations.school_id works perfectly in mysql.
It doesn't look like you are declaring $ret2.
FWIW: you might get better performance with your query if you format it as a join. Just a thought.