Hi I'm new to CodeIgniter and I just want to know How will I query from my MySql Db, a Select Statement with a where clause, I know it can be searched from the net but whenever I try something I get errors, It's really frustrating. The string in the Where clause will be coming from a User Input. Thanks guys!
You can do as Mehedi-PSTU stated, however it seems as though you're a little new to this, so here's some extra information:
I'll copy Mehedi-PSTU for the most part here.
$this->get->where('column_name', $equals_this_variable);
$query = $this->db->get('table_name');
This will store the query object in the variable $query.
if you wanted to convert that to a usable array, you just perform to following.
$results = $query->result_array();
Or you can loop through it like this:
foreach($query->result_array() as $result){
// Perform some task here.
}
A better or even full understanding can probably come from:
http://ellislab.com/codeigniter/user-guide/database/active_record.html
Try something like this
$this->db->where('db_attr', $var);
return $this->db->get('table');
Try this one.
$id = 'your id';
$this->db->select("*");
$this->db->from("table_name");
$this->db->where('id','$id');
$query = $this->db->get();
return $query->result_array();
In Codeigniter with Method Chaining Style :-
$data['getData'] = $this->db->get_where('table_name',array('column_name'=>$var))->result_array();
Related
is there a way to order codeigniter select query by a specific value?
I know that it can be done in mysql from this answer, but I'm wondering if there is a "codeigniter" way of doing it, here is what I've tried:
$this->db->select('...');
$this->db->from('table_one');
$this->db->join('table_two', 'table_one.some_id = table_two.id', 'inner');
$this->db->where('city',$city);
// this gives me error
$this->db->order_by("table_two.id=$id", "desc");
$query = $this->db->get();
return $query->result_array();
This gives me the unknown column error.
yes this is possible, but you need quotes:
$this->db->order_by("table_two.id='$id'", "desc");
hint: you can always doublecheck your query like this:
echo $this->db->last_query();die;
I am looking for query in Codeigniter works like where OR, AND both works.
This is working Perfect!
$srcArr2['emp_id']=8;
$srcArr2['status']='2';
$this->db->where($srcArr2);
$query = $this->db->get('tickets');
$res = $query->result_array(); // as array
print_r ($res);
query is:
Need to add OR in where with $srcArr1['ass_id']=12;
I try but not working.
$srcArr1['ass_id']=12;
$srcArr2['emp_id']=8;
$srcArr2['status']='2';
$this->db->where($srcArr2);
$this->db->where_in($srcArr1);
$query = $this->db->get('tickets');
$res = $query->result_array(); // as array
print_r ($res);
Thank You, in Advance for help
You can use or_where_in to achieve the same,
$this->db->or_where_in($srcArr1);
Official doc.
$this->db->where($srcArr2);
$this->db->or_where($srcArr1);
https://www.codeigniter.com/user_guide/database/query_builder.html#looking-for-specific-dataenter link description here
where can I put the like statement on this code? I would want to allow the user to search any documents that are similar to the entered text it would be document like '%$search%;
public function getDoc($param=''){
if($this->User->PrincipalType!="All")$where['tbl_document.PrincipalType']=$this->User->PrincipalType;
if(!isset($param['RecordID']) || $param['RecordID']==''){
$where['tbl_document.Status !=']='Removed';
$this->db->select('tbl_document.*,cr.FirstName,cr.MiddleName,cr.LastName,up.FirstName as UFName,up.MiddleName as UMName,up.LastName as ULName');
$this->db->from('tbl_document');
$this->db->join('tbl_user `cr`', 'tbl_document.CreatedBy = cr.ID','left');
$this->db->join('tbl_user `up`', 'tbl_document.UpdatedBy = up.ID','left');
if(isset($param['DocTrack']) && $param['DocTrack']!='') $where['tbl_document.ID']=$param['DocTrack'];
$this->db->where($where);
if(isset($param['DocTrack']) && $param['DocTrack']!='')$this->db->or_where('tbl_document.Title',$param['DocTrack']);
$this->db->order_by('DateCreated', 'desc');
$query = $this->db->get();
$return=$query->result();
}
Assuming you want to search for $param['DocTrack'] then you can use below two approaches for like query execution as below:
1. $this->db->like('tbl_document.Title','%'.$param['DocTrack'].'%')
2. $this->db->where("tbl_document.Title LIKE '%".$param['DocTrack']."%'");
Hope it helps you :)
Sir,
I am facing a problem and it is shown below:
I want to perform where opertion on the output of $this->db->select('*')->from('table'); only when cond1 satisfies
$result = $this->db->select('*')->from('table');
if(cond1)
{
I want to perform $result->where(); operation
}
Is it possible and i so what is the exact syntax to do it
try something like this:
$this->db->select('*');
$this->db->from('table');
if ($cond1)
$this->db->where(...)
$query = $this->db->get();
$con1 is should be a variable and it should has a value or Null. $con1 like a Boolean.1 or 0 .if 1(true) happening it'll execute the if block if not it'll execute the else block.
Looks like you want to run two different queries. Assuming that $cond1 is not dependent upon $result (in which case all bets are off), you can do something like
if(cond1)
$result = $this->db->select('*')->from('table')->where(...);
else
$result = $this->db->select('*')->from('table');
I have the following function that does not work and I'm having the hardest time trying to figure it out. I'm 12 and just learning, so forgive me:
function get_answer() {
$answer = $this->db->query("SELECT COUNT(questions) FROM possible_quest WHERE questions='something'");
return $answer;
}
When I run the following SQL query in phpmyadmin, it returns the expected result
SELECT COUNT(questions) FROM possible_quest WHERE questions='something'
How do I get this working in CodeIgniter using my function above?
The PHP error I get is
A PHP Error was encountered
Severity: 4096
Message: Object of class CI_DB_mysql_result could not be converted to string
Could be:
function get_answer()
{
$query = $this->db->query("SELECT COUNT(questions) AS count FROM possible_quest WHERE questions='something'");
$count = $query->row(); // returns an object of the first row
return $count->count;
// OR
$count = $query->row_array(); // returns an asociative array of the result
return $count['count'];
}
Another thing: if you want to pass 'something' as a variable, you can use parametrized query, like
$sql = "SELECT COUNT(questions) AS count FROM possible_quest WHERE questions = ?";
$query = $this->db->query($sql, array($something));
which has the benefit of escaping automatically your variable, so you don't worry about sql injections.
You need to setup to the count.
Heres what you need to do is
$answer = $this->db->query("SELECT COUNT(questions) as count FROM possible_quest WHERE questions='something'")->first_row()->count;
//$answer is now setup to be count
One line. Thats the beauty of CI
You're getting that error because
return $answer;
should be
return $answer->result();
The error you are getting is related to the fact that $this->db->query returns a result object, so you cannot use $answer directly as a string.
I suggest that you use print_r($answer) to see what could be going wrong with your conversion of objects to strings, if you have such a function in your model.
CodeIgniter has functions for building queries and returning the count:
function get_answer() {
$this->db->from("possible_quest");
$this->db->where("questions", "something");
return $this->db->count_all_results();
}
NOTE: The name of the function 'get_answer' doesn't match what you're actually doing. It looks like you're getting a count of questions, not an answer, so you should name it to something that makes more sense, like 'get_question_count'.
I recommend you to use an Active Record with method chaining when possible:
public function getAnswer() {
return
$this->db->
select('id')->
where('questions', 'something')->
get('possible_quest')->row()->count
;
}
or
public function getAnswer() {
return
$this->db->
select('id')->
from('possible_quest')->
where('questions', 'something')->
get()->row()->count
;
}
It's secure, easy to use, easy to understand and read. Don't listen to people saying that a single-line code is something good because a good code should be readable.