I get following error while trying to get data from my database:
Error Number: 1066
Not unique table/alias: 'faq'
SELECT *FROM (faq, faq)WHERE faq_title = 'title 1'
Please help me to find my mistake. Here is my model:
public function did_get_faq_data($title){
$this->db->select('*');
$this->db->from('faq');
$this->db->where('faq_title', $title);
$query = $this->db->get('faq');
if ($query->num_rows() > 0){
return $query->result();
}
else {
return false;
}
}
In your query table name is called two times. This is unnecessary.
Just replace $query = $this->db->get('faq'); to $query = $this->db->get();
the bold one is correct.
public function did_get_faq_data($title){
$this->db->select('*');
$this->db->from('faq');
$this->db->where('faq_title', $title);
$query = $this->db->get();
if ($query->num_rows() > 0){
return $query->result();
}
else {
return false;
}
}
Related
I have a problem in the model, how to get all the columns in the following select query:
public function getAllProvince($where = array())
{
$this->db->select("id, name");
$this->db->from('target_statis');
$this->db->where($where);
$this->db->group_by("id, name");
$this->db->order_by("id");
$query = $this->db->get();
if ($query->num_rows() > 0){
return $query->result();
}
return false;
}
Try This,
public function getAllProvince($where = array()){
$this->db->select("*");//changed line
$this->db->from('target_statis');
$this->db->where($where);
$this->db->group_by("id,name");
$this->db->order_by("id");
$query = $this->db->get();
if ($query->num_rows() >0){
return $query->result();
}else{
return array();//changed line
}
}
I'm deleting $this->db->select(column) because you want to get all columns
If you not defining $this->db->select(), codeigniter will default select all the columns
also, I'm deleting $this->db->from() and move the table name to $this->db->get('target_statis');
public function getAllProvince($where = array()){
$this->db->where($where);
$this->db->group_by("id,name");
$this->db->order_by("id");
$query = $this->db->get('target_statis');
if ($query->num_rows() >0){
return $query->result();
}
return FALSE;
}
Try this code...
public function getAllProvince($where = array())
{
$this->db->select("*");
$this->db->from('target_statis');
$this->db->where($where);
$this->db->group_by("id, name");
$this->db->order_by("id");
$query = $this->db->get();
if ($query->num_rows() > 0){
return $query->result();
}
return false;
}
I just want to do this if(status==0) then only where condition is run and print only related to status==0 record.And when status==1 come then it bypass the where clause and print both data for status 0 and 1. I don't know how to build a query for this. Please help me in this.
my model code
function listing(){
$admin_id = $this->session->userdata('admin_id');
$query = $this->db->select('student_info.*,admin.status')
->from('student_info')
->where('student_info.admin_id',$admin_id)
->join('admin','admin.admin_id = student_info.admin_id')
->get();
return $query->result();
}
This code work properly and print separate data whose status 0 or 1.
Only admin_id = 1 contain Status = 1, other id's contain 0 status.
Replace your function with the following code...
<?php
function listing($status)
{
$this->db->select('student_info.*,admin.status');
$this->db->from('student_info');
if($status==0)
{
$this->db->where('student_info.admin_id',$admin_id);
}
$this->db->join('admin','admin.admin_id = student_info.admin_id');
$query = $this->db->get();
return $query->result();
}
?>
Try this:
function listing(){
$admin_id = $this->session->userdata('admin_id');
$query = $this->db->select('student_info.*,admin.status')
->from('student_info')
->join('admin','admin.admin_id = student_info.admin_id')
->where("student_info.admin_id = IF (admin.status = 0, $admin_id, '>-1'")
->get();
return $query->result();
}
It solves my problem..
function listing()
{
$admin_id = $this->session->userdata('admin_id');
$this->db->select('student_info.*,admin.status');
$this->db->from('student_info');
$this->db->join('admin','admin.admin_id = student_info.admin_id');
$this->db->where('student_info.admin_id',$admin_id);
$this->db->or_where('admin.status < 1');
$query = $this->db->get();
return $query->result();
}
<?php
function listing($status)
{
if($status == 0){
$this->db->select('student_info.*,admin.status');
$this->db->from('student_info');
$this->db->where('student_info.admin_id',$admin_id);
$this->db->join('admin','admin.admin_id = student_info.admin_id');
$query = $this->db->get();
return $query->result();
}else{
$this->db->select('student_info.*,admin.status');
$this->db->from('student_info');
$this->db->join('admin','admin.admin_id = student_info.admin_id');
$query = $this->db->get();
return $query->result();
}
}
?>
I have to return only one row from my model to controller, before passing it to controller i have to add some more parameters to it such as given below:
$condition = "id =" . "'" . $id . "'";
$this->db->select('*');
$this->db->from('subject');
$this->db->where($condition);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
i have want to do something like this while returning it to controller
if ($query->num_rows() == 1) {
$rows[]=$query->result();
$rows['main1']='aaaa';
return $query->result();
} else {
return false;
}
Just grab the row_array and append a value to it like such:
if($query->num_rows() == 1){
$resultArray = $query->row_array();
$resultArray['main1']='aaaa';
return $resultArray;
} else {
return array();
}
Use $query->row() to fetch single rows from database and you can direction write your condition as $this->db->where('id', $id);
$this->db->select('*');
$this->db->from('subject');
$this->db->where('id', $id);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
$res= $query->row();
return $res->column_name;
} else {
return false;
}
In controller
function your_function() {
$this->load->model('your_model');
$res['name'] = $this->your_model->your_function($id);
$this->load->view('your_view',$res);
}
The below query is only returning one result, even though there are multiple entries in the table with user_is 2. I m not sure what I m doing wrong.
function get_payments($user_id)
{
$sql = "SELECT txn_id, payer_email FROM orders WHERE user_id = ?";
$query = $this->db->query($sql, $user_id);
//echo $this->db->last_query();
if($query->num_rows() != 0) {
foreach($query->result() as $payment)
{
$data[] = $payment;
return $data;
}
}else{
return false;
}
}
This is what $this->db->last_query(); returns. SELECT txn_id, payer_email FROM orders WHERE user_id = '2'
Controller:
$profile_data['user_payments'] = $this->profile_model->get_payments($user_id);
View:
<?php
//var_dump($user_payments);
foreach($user_payments as $payment)
{
echo '<p>';
echo $payment->txn_id;
echo $payment->payer_email;
echo '</p>';
}
?>
try this:
<?php
function get_payments($user_id){
$this->db->select('txn_id, payer_email')
->from('orders')
->where('user_id', $user_id);
$qry = $this->db->get();
$res = $qry->result();
if($res){
return $res;
}else{
return false;
}
}
?>
there is a slight mistake here
foreach($query->result() as $payment)
{
$data[] = $payment;
return $data; // this line returns the data and it stops execution in first loop itself
}
it should be like
if($query->num_rows() != 0) {
foreach($query->result() as $payment)
{
$data[] = $payment;
}
return $data;
}
You should return $data from model after foreach loop completes.
if($query->num_rows() != 0) {
foreach($query->result() as $payment)
{
$data[] = $payment;
}
return $data;
}
else
{
return false;
}
Simple CI way:
...
$query = $this->db->query($sql, $user_id);
return $query->result_array();
No looping needed, nothing.
And I see you're using the object form, in that case do this:
return $query->result();
Then just loop it in your view like you're already doing.
So this is the whole model method:
function get_payments($user_id) {
$sql = "SELECT txn_id, payer_email FROM orders WHERE user_id = ?";
$query = $this->db->query($sql, $user_id);
return $query->result();
}
See the relevant docs here: http://ellislab.com/codeigniter/user-guide/database/results.html
And using Active Records (the most "natural" to CI):
function get_payments($user_id) {
return $this->db
->select('txn_id, payer_email')
->where('user_id', $user_id)
->get('orders')
->result();
}
I would like to have an where in statement before the select_sum statement.
Because i would like to have different sums based on the $project. Now it is just counting all the array variables as a sum.
Im doing this in codeigniter.
model:
function rapport_detail_opbrengsten($idKlant){
$this->db->from('Project');
$this->db->join('Opbrengsten', 'Opbrengsten.idProject = Project.idProject');
if ($idKlant > 0){
$this->db->where('idKlant', $idKlant);}
$query = $this->db->get();
$project = array();
foreach($query->result() as $row){
$project[] = $row->idProject;
}
$this->db->select_sum('Prijs');
$this->db->from('Opbrengsten');
$this->db->where_in('idProject', $project);
$query = $this->db->get();
if($query->num_rows()>0){
return $query->result();
}
else{
return false;
}
}
select_sum groups all your rows .. so there will be no effect if you manually group the rows by other criteria. Try something like this
$this->db->select('idProject, SUM(Prijs) as total');
$this->db->from('Opbrengsten');
$this->db->where_in('idProject', $project);
$this->db->group_by('idProject');
$query = $this->db->get();