Returning all rows from database in CodeIgniter - php

I am trying to fetch the data from database with compare of some fields using get_where but every time it's returning data even where condition does not satisfied...
My Code is:
function check_sec_token()
{
$sec_token=$this->session->userdata('sec_token');
$email_id=$this->session->userdata('email_id');
$query=$this->db->get_where($this->_registration,array('sec_token'=>$sec_token,'email_id'=>$email_id));
$rows=$query->num_rows();
return $query->result();
if($rows<1)
{
return false;
}
else
{
return true;
}
}
if $sec_token and $email_id is null then it returns all rows from the database..i also tried
function check_sec_token()
{
$sec_token=$this->session->userdata('sec_token');
$email_id=$this->session->userdata('email_id');
$query=$this->db->get_where($this->_registration,array('sec_token'=>$sec_token,'email_id'=>$email_id));
$rows=$query->num_rows();
return $this->db->last_query();
if($rows<1)
{
return false;
}
else
{
return true;
}
}
it's showing query
Select * from user_reg where sec_token=0 and email_id=0

Try this:
...
if ($sec_token == NULL && $email_id == NULL)
{
$query=$this->db->get($this->_registration);
}
else
{
$query=$this->db->get_where($this->_registration,array('sec_token'=>$sec_token,'email_id'=>$email_id));
}
...

Notice:
return $query->result();
This piece of code stops running the rest of your code! and your condition on row count is not considered!
you can do this instead:
$query=$this->db->get_where($this->_registration,array('sec_token'=>$sec_token,'email_id'=>$email_id));
$rows=$query->num_rows();
if($rows<1)
{
return false;
}
else
{
return $query->result();
}

Here if $sec_token and $email_id are null or "0" then query you will fetch all records.It will give all rows because when "where condition not satisfied then it fetch all records" try it.Also check your session is start or not and your values is set or not in it properly.

Related

Check Function without execute it

i'm training SOLID/Architectures and trying to make an INSERT on my code, but its insert four times on DB. There's any error on my logic? I'm following Repositories/Service Pattern.
i think my service is executing two times, but i cant find the reason.
Repositorie Code
public function inserirEstoque($dadosPost)
{
if (empty($dadosPost)) {
return false;
}
$pdo = $this->dbConnection->conectar();
$sql = $pdo->prepare('INSERT INTO estoque (nomeProduto, descriptions, price, quantity)
VALUES (:nome, :descriptions, :price, :quantity)');
$sql->bindValue(':nome', $dadosPost['nomeProduto']);
$sql->bindValue(':descriptions', $dadosPost['descriptions']);
$sql->bindValue(':price', $dadosPost['price']);
$sql->bindValue(':quantity', $dadosPost['quantity']);
$res = $sql->execute();
if($res == false)
{
return false;
}
return $res;
}
Service
public function insertEstoque()
{
$db = new MySQL();
$insert = new EstoqueRepositories($db);
if(!empty($insert->inserirEstoque($_POST))){
return $insert->inserirEstoque($_POST);
} else {
return false;
}
}
Controller
public function insert()
{
$insert = new EstoqueService();
$insert->insertEstoque();
header('Location: ../../index.php');
}
It's executing twice because of this
if(!empty($insert->inserirEstoque($_POST))){
return $insert->inserirEstoque($_POST);
} else {
return false;
}
if you wanna check if the POST data is empty just remove where it inserts the data then it should just insert it 1 time
if(!empty($_POST["whatevername"])){
return $insert->inserirEstoque($_POST);
} else {
return false;
}
As an addition to Reed's answer, if you just want to check the result of a function call before carrying on, assign the result to a variable and use that variable.
$res = $insert->inserirEstoque($_POST)
if(!empty($res)){
return $res;
} else {
return false;
}

CI affected_rows always return 0 even after Updated

I use Codeigniter 3.0.6 to build a system.
Usually, I use affected_rows() to check whether my database is updated or not.
function update($id=0,$data=array())
{
$this->db->where($this->key, $id);
$this->db->update($this->table,$data);
if($this->db->affected_rows() > 0)return TRUE;
else return FALSE;
}
Problem is if no update found, it will return 0 as well. As I want to differentiate between Updated and No Update in my code, so i use this solution.
function update($id=0,$data=array())
{
$this->db->trans_start();
$this->db->where($this->key, $id);
$this->db->update($this->table,$data);
$this->db->trans_complete();
if ($this->db->affected_rows() > 0) {
return TRUE;
} else {
if ($this->db->trans_status() == FALSE) {
return FALSE;
}
return 'No Update';
}
}
But even after updated affected_rows always return int(0).
What's the reason? Thank you.
I'm also using CI 3.0.6 and affected_rows() gives back the correct amount of updated rows.
Can you do a test without the transactions. Something like this ?
function update($id)
{
$this->db->set('field_name', "new_value");
$this->db->where('id', $id);
$this->db->update('table_name');
$rows = $this->db->affected_rows();
return $rows;
}
Verify if a record is actually been updated in the database. You can check $rows for the update status.
if ($rows < 1) {
// no update
} else {
// updated
}
If you move $this->db->affected_rows() before the $this->db->trans_complete(); you will get expected count. Because affected_rows() check only the last statement which is $this->db->trans_complete(); in your problem it will be return 0 .
You can fix this by assign $this->db->affected_rows() to another variable before the $this->db->trans_complete(); and validate later.
function update($id=0,$data=array())
{
$this->db->trans_start();
$this->db->where($this->key, $id);
$this->db->update($this->table,$data);
$affected_rows = $this->db->affected_rows(); // moved to here from if condition
$this->db->trans_complete();
if ($affected_rows > 0) {
return TRUE;
} else {
if ($this->db->trans_status() == FALSE) {
return FALSE;
}
return 'No Update';
}
}

Codeigniter not getting data though available in mysql database

I have a model which fetch the data from database is below
public function counselor() {
$inst_id = $this->session->userdata('user_id');
$submission_key=$this->session->userdata('submission_key');
$query = $this->db->query("SELECT * FROM counselor where USER_ID = $inst_id AND submission_key= $submission_key");
$data = $query->num_rows();
if ($data > 0) {
return $data;
} else {
return false;
}
}
I have tested the $inst_id and $submission_key by printing it and its set.
$inst_id=2 and $submission_key=2016-8 .BUT though I have one record in database with those two field set its not returning the data. What is the case. I have tried with codeigniter get() and where() method too. Still not giving me the result.
Just write your query using active record function. It will help you in escaping string
$this->db->select('*',FALSE);
$this->db->where('USER_ID',$inst_id);
$this->db->where('submission_key',$submission_key);
$query=$this->db->get('counselor');
$data = $query->num_rows();
if ($data > 0) {
return $data;
} else {
return false;
}

Last Insert ID on codeigniter with SQL Server returns false

Good Evening,
I got a problem with the last insert id function with active record codeigniter, it works fine in mysql. But when i try it on SQL Server, the result always return false. Please kindly take a look on my code:
function transactions_start($data) {
$this->db->insert($this->table, $data);
$id = $this->db->insert_id();
$q = $this->db->affected_rows();
if ($q > 0) {
return $id;
} else {
return FALSE;
}
}
this is the result of $this->db->last_query()
select ##IDENTITY as id
the var_dump() result always returning bool(false).
I'm developing this apps using MySQL and SQL Server, so i need to keep using the active record from codeigniter.
Any help would be great, thanks in advance!
The insert_id() function returns the id of last inserted row so you just check whether it is greater than 0 or not. Use it like
function transactions_start($data) {
$this->db->insert($this->table, $data);
$id = $this->db->insert_id();
if ($id > 0) {
return $id;
} else {
return FALSE;
}
}
Reference : https://ellislab.com/codeigniter/user-guide/database/helpers.html

Redirecting not working in codeigniter

So here's what I want to do. I want to check if the userid in segment(3) exist or else it will redirect somewhere instead of still loading the view with an error.
Here's the example url
http://localhost/ems/edit_user/edit_user_main/1001
Now if I try to edit the userid in segment(3) and intentionally put an invalid userid, it still loads the view and i don't know why
Here's my function
public function edit_user_main(){
$id = $this->uri->segment(3);
$check = $this->get_data->check_if_exist($id);
if($check) {
$data['title'] = 'Edit User';
$data['id'] = $this->session->userdata('usertoedit');
$this->load->model('accounts/get_data');
$item = $this->get_data->get_user($id);
$data['user'] = $item[0];
$data['main_content'] = 'edit_user/edit_user_main';
$this->load->view('includes/template', $data);
} else {
redirect('admin/adminuser');
}
}
Here's the model
public function check_if_exist($id){
$query = $this->db->get_where('accounts',array('user_id'=>$id));
if($query) {
return TRUE;
} else {
return FALSE;
}
}
There is no problem with the fetching of data.
The problem is even if the userid doesn't exist, the view is still loading but with an error coz there's no data for that userID. It's not redirecting,
I tried using print_r and it working fine, the value of the $check is 1 when there's a valid userID.
Hope someone can help me with this. Thank you
With your function it will always return true because the statement
$this->db->get_where('accounts',array('user_id'=>$id));
will always execute,So you need to check query is returning any result row or not with the statement
$query->num_rows().
public function check_if_exist($id){
$query = $this->db->get_where('accounts',array('user_id'=>$id));
if($query->num_rows() > 0){ //change made here
return TRUE;
}
else{
return FALSE;
}
}
Try this..
With the function it will always return true because the following statement
$this->db->get_where('accounts',array('user_id'=>$id));
will always be execute, So need to check query is returning any result row or not
$query->num_rows().
public function check_if_exist($id){
$query = $this->db->get_where('accounts',array('user_id'=>$id));
if($query->num_rows() > 0){ //change made here
return TRUE;
}
else{
return FALSE;
}
}
And load heper as:-
$this->load->helper('url');
before the redirection

Categories