i want to fetch that from mysql database using codeigniter framework php?
I want to fetch that through model
SELECT `id`, `name`, `visits`, `shortdes`, `photo` FROM `products`
WHERE `name` LIKE '$word'
OR `shortdes` LIKE '$word' ANd `valid` = 1 ORDER BY `id` DESC
I used that, but it returned false
$this->db->order_by('id开发者_StackOverflow社区', 'desc');
$this->db->like('name', $word);
$this->db->or_like('shortdes', $word);
$this->db->where('valid', 1);
$this->db->select('id, name, visits, shortdes, photo');
$query = $this->db->get('products');
What can i use?
For debugging codeigniter's query builder query's, I usually reccommend doing something like this to debug them and see exactly what query the statements are producing.
$this->db->order_by('id', 'desc');
$this->db->like('name', $word);
$this->db->or_like('shortdes', $word);
$this->db->where('valid', 1);
$this->db->select('id, name, visits, shortdes, photo');
$query = $this->db->get('products');
echo "<pre>"
die(print_r($this->db->last_query(), TRUE));
When ran, it will output the actual raw SQL query being produced, and you can then debug it from there.
You should order your active record queries the same way you order a regular SQL query...
SELECT
WHERE
order_by
$this->db->get(x); -- which is the equivalent of FROM.
IE:
<?php
$this->db->select('id, name, visits, shortdes, photo');
$this->db->where('valid', 1);
$this->db->like('name', $word);
$this->db->or_like('shortdes', $word);
$this->db->order_by('id', 'DESC');
$query = $this->db->get('products');
if ($query->num_rows() > 0)
{
$row = $query->row();
echo $row->id;
echo $row->name;
echo $row->visits;
etcetc...
}
Or instead of using active records you can just use a full query.
$query = $this->db->simple_query('YOUR QUERY HERE');
Use the following user guide pages for more documentation:
active records: http://codeigniter.com/user_guide/database/active_record.html
query results: http://codeigniter.com/user_guide/database/results.html
精彩评论