I have tried to fetch the google_id by using a select query in codeigniter, but it shows the email is null even if the email is present in the database.
public function loginUser( $data, $api = false) {
$this->db->select('*');
$this->db->from('tbl_users');
/* if(!empty($data['email']) && $api){*/
$this->db->where('email',$data['email']); //checking user email from dbase//
/*}
*/ if(!empty($data['fb_id']) && $api){
$this->db->where('fb_id', $data['fb_id']);
}
else
if(!empty($data['google_id']) && $api){
$this->db->where('google_id', $data['google_id']);
}
$this->db-> limit(1);
$query = $this->db->get();
$result = $query->row_array();
--
SELECT *
FROM (`tbl_users`)
WHERE `email` IS NULL
AND `google_id` = '5726357629529852956'
LIMIT 1
$data['email'] might be geting a null value.check $data['email'] output.Check the controller section.
Related
My target is to show an echo "No data" when the query result is empty. What happening currently is that when my query is empty, it shows an error. (Please see the picture below for the error) I provided my codes below and my screenshot. Any help will be appreciated. Thank you
Controller:
public function new1($arenaID=0) {
$data['activeNav'] = "";
$data['subnav'] = "arenas";
$this->header($data);
$this->nav();
$data['k1']=$this->arenas->meron1();
$this->load->view('new', $data);
$this->footer();
}
Model:
function meron1(){
$query = $this->db->query("SELECT fightID FROM fight_entries where position='meron' ORDER BY fightID DESC LIMIT 1");
return $query->row()->fightID+1;
}
A better way to do this is just to check the number of rows being returned.
function meron1(){
$query = $this->db->query("SELECT fightID FROM fight_entries where position='meron' ORDER BY fightID DESC LIMIT 1");
if($query->num_rows() > 0){
$query = $query->rows();
return $query->fightID+1;
}else{
$message = "No Data";
return $message;
}
After insert I want to update field with ($id.'mytext') where in db that field is empty for all rows.
table name: peca
columns:
id -autoincrement
A -varchar user insert
CB -varchar auto insert with update
MODEL WILL RETURN ALL ROWS WHERE CB=empty
function model1() {
$this->db->select('*');
$this->db->from('peca');
$this->db->where('CB', '');
//$this->db->where('id', $fileid);
$query = $this->db->get();
if ($query) {
return $query->result();
} else {
return false;
}
}
MODEL WILL update in db where CB=empty
function model2($dados = NULL) {
if ($dados !== NULL) {
// extract($dados);
$this->db->where('CB', '');
$this->db->update('peca', $dados);
return true;
} else {
return false;
}
}
CONTROLLER
$this->load->model('model_peca');
$resultadocadastropeca = $this->model_peca->model1($id);
$data = 'id' => $id;
$appointment = array('codigoean' => $data.'.PECA.');
$appointment = $this->model_peca->model2($appointment);
START TABLE
Previous values inserted from import so CB can only be generated after id exists
id|CB |
22| |
31| |
RESULTS
I'm changing CB to .PECA. in all rows where CB=empty but $id for each row is not passing
id|CB |
22|.PECA.|
31|.PECA.|
EXPECTED
id|CB |
22|22.PECA.|
31|31.PECA.|
I still don't fully understand what you want to do, but if you want to fetch something where a column is empty or null:
$this->db->where("WHERE yourColumn IS NULL OR yourColumn = ''");
As per your revised question, this should work:
$this->db->select('id');
$this->db->where('CB', '');
$q = $this->db->get('peca');
if ($q->num_rows() == 0) {
die('nothing to do here');
}
$res = $q->result();
foreach ($res as $item) {
$data[] = array('id' => $item->id, 'CB' => $item->id . '.PECA.');
}
$this->db->update_batch('peca', $data, 'id');
I have sql query in controller, this query produces number of rows.
$q1 = $this->db->query("SELECT product_code,product_quantity FROM tbl_order_details WHERE order_id='$order_no' AND location='$location'")->row();
foreach($q1 as $v_barcode){
$result = $this->settings_model->update_cancel_stock($v_barcode->product_code,$v_barcode->product_quantity);
}
then i pass this data to my model,
public function update_cancel_stock($code,$qty)
{
$this->db->set('product_quantity', $qty , FALSE);
$this->db->where('product_id', $order );
$this->db->update("tbl_inventory6");
}
but no any update. please check above code. thanx
Try this
$cond = array("order_id" => $order_no, "location" => $location);
$q1 = $this->db->select("product_code,product_quantity")->from("tbl_order_details")->where($cond)->row();
$result = $this->settings_model->update_cancel_stock($q1->product_code,$q1->product_quantity);
and then model ($code was not used)
public function update_cancel_stock($code,$qty){
$this->db->set('product_quantity', $qty , FALSE);
$this->db->where('product_id', $code);
$this->db->update('tbl_inventory6');
}
You are using row() function of Active Record. It already return only one row. So there is no requirement of foreach. Try this:
$q1 = $this->db->query("SELECT product_code,product_quantity FROM tbl_order_details WHERE order_id='$order_no' AND location='$location'")->row();
$result = $this->settings_model->update_cancel_stock($q1->product_code,$q1->product_quantity);
If you are getting multiple records and you are using foreach, you should use result().
$q1 = $this->db->query("SELECT product_code,product_quantity FROM tbl_order_details WHERE order_id='$order_no' AND location='$location'")->result();
foreach($q1 as $v_barcode){
$result = $this->settings_model->update_cancel_stock($v_barcode->product_code,$v_barcode->product_quantity);
}
I have a query for filtering products that are related to categories and subcategories and tags/attributes (color, lengths, connection etc) and i'm having problems getting exact results using $this->db->where_in("field",$array_of_tags); is there another whey to this i haven't figured out ?
This is my function
`public function get_public_products($limit_start = false, $limit_end = false, $categoria = false, $subcategoria = false , $tags = false){
if(!$subcategoria){
$this->db->select('*');
}
$this->db->from('products');
if($categoria && !$subcategoria){
$this->db->join('products_categories', 'products.familia = products_categories.codigo');
$this->db->where('products_categories.slug',$categoria);
}
if($subcategoria && !$tags){
$this->db->select('products.familia AS familia, products.subfamilia AS subfamilia, products.slug AS pslug, products_categories.slug AS cslug , products.nombre AS nombre, products_categories.name AS cname, products.codigo_producto AS codigo_producto');
$this->db->join('products_categories', 'products.subfamilia = products_categories.codigo');
$this->db->where('products_categories.slug',$subcategoria);
}
if($tags){
$this->db->select('products.familia AS familia, products.subfamilia AS subfamilia, products.slug AS pslug, products_categories.slug AS cslug , products.nombre AS nombre, products_categories.name AS cname, products.codigo_producto AS codigo_producto , products_tags_values.value AS tvalue, products_tags_values.slug AS tslug');
$this->db->join('products_categories', 'products.subfamilia = products_categories.codigo');
$this->db->where('products_categories.slug',$subcategoria);
$this->db->join('products_tags_values', 'products_tags_values.pid = products.codigo_producto');
$tags_array = explode(",", $tags);
$this->db->where_in('products_tags_values.slug',$tags_array);
}
if($limit_start){
$this->db->limit($limit_start, $limit_end);
}
$query = $this->db->get();
return $query->result_array();
}`
This works fine when i use $this->db->where('products_tags_values.slug',"blue"); but not if i chane them like this for multiple tags.
$this->db->where('products_tags_values.slug',"blue");
$this->db->where('products_tags_values.slug',"20-m");
i am getting error Call to a member function row() on a non-object in codeigniter my controller is
public function edit_survey_pro($id)
{
$id = intval($id);
$survey = $this->model->get("surveys",array("ID" => $id),100000);
if (sizeof($survey) == 0) $this->template->error(lang("error_32"));
$this->template->loadContent("user/edit_survey_pro", array(
"survey" => $survey->row()
)
);
}
my model is
function get($table,$where='',$perpage=0,$start=0,$order_by='',$arr='')
{
$this->db->from($table);
if($perpage != 0 && $perpage != NULL)
$this->db->limit($perpage,$start);
if($where){
$this->db->where($where);
}
if($order_by){
$this->db->order_by($order_by);
}
if($arr=='')
$query = $this->db->get()->result();
else
$query = $this->db->get()->result('array');
if(!empty($query))
if($perpage != 0 && $perpage != NULL)
$result = $query;
else
$result = $query[0];
else
$result = array();
return $result;
}
here loadContent() is just load the content with view path
public function loadContent($view,$data=array(),$die=0){
//something to load the content
}
in my model I am getting the result as an array of object in $query and then it is returned as $result like this -
$query = $this->db->get()->result(); but at the controller $survey stores array of object and i want to show the content of that array of object ,previously I use
$this->template->loadContent("user/edit_survey_pro", array(
"survey" => $survey->row()
)
);
to get that data but the problem is $survey->row() cannot return that data bcoz it is not an object it is array of object so it can't be returned through row() method
so instead of this I just call the first element of that data like this-
$this->template->loadContent("user/edit_survey_pro", array(
"survey" => $survey[0]
)
);
Somehow its works for me bcoz I want to show the first row of the data
if sembody wants to show all data then I think he shuld try logic to increment the key value of that array of object for me it is $survey[] you can use foreach loop for increment the of value of the key element
The problems i see are your model, I will dissect it and add comments to your original code to point out the issues:
function get($table,$where='',$perpage=0,$start=0,$order_by='',$arr='')
//above there are problems, you are setting some of your parameters to
//equal blank, but below, in your conditionals, you are checking if they
// exist. They will always exist if they are set to blank. Fix them by
// setting them = NULL like this:
// get($table,$where=null,$perpage=0,$start=0,$order_by=null,$arr=null)
{
$this->db->select();// <-- you forgot this
$this->db->from($table);
if($perpage != 0 && $perpage != NULL)
//when will $perpage = null? , if never, then you dont need it.
$this->db->limit($perpage,$start);
if($where){
//change this to if(isset($where)). Also why do you use
//curly braces here, but not in the above if statement if only
//one line is affected in your if. I would remove the
//curly braces here.
$this->db->where($where);
}
if($order_by){
// change this to if(isset($order_by)). Same thing as
//above about the curly braces here
$this->db->order_by($order_by);
}
if($arr=='')
// change this to if(isset($arr)).
$query = $this->db->get()->result();
else
$query = $this->db->get()->result('array');
//change this to: $query = $this->db->get()->result_array();
if(!empty($query))
//change the above to if($query->num_rows > 0). Also, here since
//your code body is longer then one line, you will need curly braces
//around your if statement
if($perpage != 0 && $perpage != NULL)
//again, will $perpage ever be NULL? However, why do you need
//this conditional at all, if the limit above is already
//doing this job?
$result = $query;
else
$result = $query[0];
else
$result = array();
return $result;
}
after applying all the changes:
MODEL:
function get($table, $where=null, $perpage=0, $start=0, $order_by=null, $arr=null)
{
$this->db->select();
$this->db->from($table);
if($perpage != 0)
$this->db->limit($perpage, $start);
if(isset($where))
$this->db->where($where);
if(isset($order_by))
$this->db->order_by($order_by);
if(isset($arr)) {
$result = $this->db->get()->result_array();
} else {
$result = $this->db->get()->result();
}
return $result;
}
CONTROLLER
public function edit_survey_pro($id) {
$id = intval($id);
$survey = $this->model->get("surveys",array("ID" => $id),100000);
if (!$survey) {
$this->template->error(lang("error_32"));
} else {
$data["survey"] = $survey;
$this->template->loadContent("user/edit_survey_pro", $data);
}
}
I think when you use $this->db->get(), you need to pass the table name as param like this:
$this->db->get('table_name')->result();