This is my controller:
$key['conversation_id']=1;
$update_data['is_read']=0;
$getupdate = $this->Common_model->update_msg($key,$update_data);
This is my model
function update_msg($updatekey, $updatevalue) {
$result = $this->mongo_db->db->messages->update($updatekey,array('$set'=> $updatevalue));
return $result;
}
if I try to print the response then I will get the following result
Array ( [updatedExisting] => 1 [n] => 1 [connectionId] => 10 [err] => [ok] => 1 )
$converstion_id=1;
$update_data=0;
$getupdate = $this->Common_model->update_msg($converstion_id,$update_data);
//In Model
function update_msg($converstion_id, $update_data)
{
$result = $this->mongo_db->db->messages->update(array("converstion_id"=>$converstion_id),array('$set'=>array("is_read"=>$update_data)));
return $result;
}
try this code..
$key['conversation_id']=1;
$update_data['is_read']=0;
$data_array['$set'] = $update_data;
$result = this->mongo_db->db->messages->update($key, $data_array);
return $result;
Related
I would like to find a way to check if my query result doesn't return a value. Example:
If in my tableExample on database there isn' t the id that I'm passing , the method should return an exception or a simple echo that indicate me the not presence in the table
My code below:
try{
DB::table('tableExample')
->where('id', "2")
->update(['update' => "1"]);
return $result= array("result" => "true" );
}catch(QueryException $e){
return $result= array("result" => "false" );
echo " - ".$e;
}
}
update method return integer value (affected rows) if success, try like this
try{
$update = DB::table('tableExample')
->where('id', "2")
->update(['update' => "1"]);
if($update){
$result = array("result" => true );
}else{
$result = array("result" => false,"message"=>"Not Found" );
}
}catch(QueryException $e){
$result = array("result" => false,"message"=>$e->getMessage() );
}
return $result;
Use findOrFail() helper method. That way you do not need to wrap the action in a try catch since if findOrFail does not find the row then it will throw an exception.
$resultData = DB::table('tableExample')->findOrFail(2);
$update = $resultData->update(['update' => "1"]);
if(!$update){
return response(['results'=>false]);
}
return response(['results'=> true]);
}
update() method returns a boolean true for success on update and versa.
You can do this by using the whereExists clause:
https://laravel.com/docs/5.5/queries#where-exists-clauses
https://laravel.com/api/5.5/Illuminate/Database/Query/Builder.html#method_exists
I'm using Codeigniter active records for some time now and this is the most ridiculous error I've got.
My query is
$this->db->select('*');
$this->db->from('my_table');
$this->db->where('is_active', 1);
$query = $this->db->get();
if ($query->num_rows() > 0) {
//return $query->result();
return $query;
} else {
return FALSE;
}
when I use$query->result(); It is empty. When I use return $query;
the result is like below,
CI_DB_pdo_result Object
(
[num_rows] => 21
[conn_id] => PDO Object
(
)
[result_id] => PDOStatement Object
(
[queryString] => SELECT * FROM my_table WHERE is_active = 1
)
[result_array] => Array
(
)
[result_object] => Array
(
)
[custom_result_object] => Array
(
)
[current_row] => 0
[row_data] =>
)
Count is
[num_rows] => 21
what is missing/problem here?
I have found the issue. This thing happens because my application use PDO DB Driver. Not MySQL Driver.
$db['default']['dbdriver'] = 'pdo';
So I changed my model to support that.
$sql = "SELECT * FROM my_table WHERE is_active = 1";
$stmt1 = $this->db->conn_id->prepare($sql);
$stmt1->execute();
return $stmt1->fetchAll(PDO::FETCH_ASSOC);
Now it's working fine.
I am really sorry for your time people. And thank you very much for
trying to help.
Try this
$this->db->select('*');
$this->db->from('my_table');
$this->db->where('is_active', 1);
$query = $this->db->get();
$result = $query->result_array();
if (count($result) > 0) {
return $result;
}
else{
return FALSE;
}
You're not formatting the query array. So format it.
simple add
$query = $this->db->get()->result();
in your code
$this->db->select('*');
$this->db->from('my_table');
$this->db->where('is_active', 1);
$query = $this->db->get()->result();
if ($query->num_rows() > 0) {
//return $query->result();
return $query;
} else {
return FALSE;
}
if ($query->num_rows() > 0) then return $query->result() which returns results in object format.Like this..
if ($query->num_rows() > 0) {
return $query->result();
} else {
return FALSE;
}
Then use arrow(->) operator to retrieved required values.
For more see here Codeigniter Result Formats
If u want to get result you must use row() or result() function.
$this->db->select('*');
$this->db->from('my_table');
$this->db->where('is_active', 1);
$query = $this->db->get();
if ($query->num_rows() > 0) {
//return $query->result();
return $this->db->get()->result();
//return $query;
} else {
return FALSE;
}
$this->db->select('*');
$this->db->from('my_table');
$this->db->where('is_active', 1);
$query = $this->db->get();
$i = 0;
foreach ($query->result() as $row) {
$i++;
$data['id'] = $row->id;
$data['username'] = $row->username;
}
if ($i > 0) {
return $data;
}
I am working in codeigniter.I have created one function in model.The function is like this :
function pr($id)
{
//$query5 = $this->db->query("select max(to_date) as last_date from agent_print_details where agent_id = ".$id);
//$result5 = $query5->result();
$this->db->select('max(to_date) as last_date');
$this->db->from('agent_print_details');
$this->db->where('agent_id',$id);
$result = $this->db->get();
if($result->num_rows()>0)
return $result->result();
else
return "empty";
}
my controller is :
$pr_detail = $this->cashier1_model->pr($role['id']);
if($pr_detail != 0)
{
echo "nisarg";
}
else
{
echo "123";
}
when I print pr_detail then it will display output like this:
Array ( [0] => stdClass Object ( [last_date] => ) )
it will give blank data so it has to print 123 but it is display nisarg.
So What should i have to do to print 123?
If result not found just return FALSE in model file. Also use CI select_max to get max value
MODEL
$this->db->select_max('to_date','last_date');
$this->db->from('agent_print_details');
$this->db->where('agent_id', $id);
$result = $this->db->get();
if ($result->num_rows() > 0) {
return $result->result();
} else {
return FALSE;
}
CONTROLLER
$pr_detail = $this->cashier1_model->pr($role['id']);
if($pr_detail)
{
echo "nisarg";
}
else
{
echo "123";
}
you could use count function, also, do you have defined a table name in get function?
...
$result = $this->db->get('agent_print_details');
//if($result->num_rows() > 0)
if(count($result) > 0)
...
When you use $result->num_rows() > 0 you CANT use $result->result(). If u want to return these results, you need to check if its not empty first, and then query again to get results.
$this->db->select_max('to_date','last_date');
$this->db->from('agent_print_details');
$this->db->where('agent_id', $id);
$result = $this->db->get();
if ($result->num_rows() > 0) {
$this->db->select_max('to_date','last_date');
$this->db->from('agent_print_details');
$this->db->where('agent_id', $id);
$result = $this->db->get();
return $result->result();
} else {
return FALSE;
}
I'm trying to get the data from array in my controller php on Cakephp.
I have this function:
public function updateUserStatus() {
if(isset($this->params['url']["pcs"])) {
$uus = array( "pcs" =>$this->params['url']["pcs"] );
$trans = $this->Transaction->updateUserStatus($uus);
} else {
$trans = "failed";
}
$this->set('trans', $trans);
$this->layout = 'ajax';
}
And I want to get the data from status_id who have this response:
Array (
[0] => Array
(
[status_id] => 2
)
[1] => Array
(
[rem_time] => 66
)
)
How can I do it?
My question is how can i get the data for status_id ?
public function updateUserStatus() {
if (isset($this->params['url']["pcs"])) {
$uus = array("pcs" =>$this->params['url']["pcs"]);
$trans = $this->Transaction->updateUserStatus($uus);
} else {
$trans = "failed";
}
$currentStatus = 0;
if (is_array($trans) && isset($trans['status_id'])) {
$currentStatus = $trans['status_id'];
}
$this->set('trans', $trans);
$this->layout = 'ajax';
}
public function updateUserStatus($uus){
if(isset($uus["pcs"])) {
$sql = "SELECT status_id as status_id , rem_time as rem_time FROM phones WHERE pcs = '".$uus["pcs"]."' LIMIT 1";
$query = $this->query($sql);
return $query[0]['phones'];
} else {
return "failed";
}
}
Notice, we are returning $query[0]['phones'].
Let me know if it works.
The code could also use some refactoring.
For example, why is the function called updateUserStatus if it is only returning the result of a query? It should also always return an array, for consistency.
My controller:
public function thread($page = 'empty')
{
$data['user_id'] = $this->tank_auth->get_user_id();
$data['username'] = $this->tank_auth->get_username();
//User data variable grab
//default template load data
$data['thread'] = $this->thread_model->get_thread($page);
$data['title'] = ucfirst($page); // Capitalize the first letter
$data['replies'] = $this->thread_model->get_reply($data['thread']['thread_id']);
$this->thread_model->view_increment($data);
$this->load->view('templates/head', $data);
$this->load->view('main/wrapper-start', $data);
$this->load->view('templates/leftbar', $data);
$this->load->view('main/thread', $data);
$this->load->view('main/wrapper-end', $data);
$this->load->view('templates/footer', $data);
}
The error appears on the $data['replies'] line:
Severity: Notice
Message: Undefined index: thread_id
Filename: controllers/main.php
Line Number: 40
Here is my model code that replies refers to: $this->thread_model->get_reply
public function get_reply($thread_id)
{
$query = $this->db->query("SELECT r.reply_id FROM reply_thread rt
INNER JOIN replies r
ON rt.reply_id = r.reply_id
WHERE thread_id = ". $thread_id);
$replies = $query->result_array();
$replyarray = array();
foreach ($replies as $reply)
{
$id = $reply['reply_id'];
$query = $this->db->query("SELECT * FROM replies WHERE reply_id='$id'");
$thisRow = $query->row_array();
$replyarray[] = $thisRow;
}
return $replyarray;
}
Any ideas why this error is popping up? Other than the error, the page loads perfectly fine...all the info is in tact and available through the query.
here is my get_thread model
public function get_thread($page)
{
$slug = $page;
$query = $this->db->get_where('thread', array('slug' => $slug));
return $query->row_array();
}
using a print_r($data['thread']); I get this array:
Array ( [thread_id] => 233 [owner_id] => 8 [subject] => Repercussions of D3 addiction [body] => 1. signs of unhygienic practices 2.become irritable when forced to stop playing 3. carpal tunnel 4. loss of interest in pursuing a significant other 5. sleep deprivation 6. malnutrition There's nothing positive about this. [timestamp] => 2012-05-08 19:03:02 [replystamp] => 2012-05-09 12:38:32 [last_post_id] => 3 [slug] => 233-repercussions-of-d3-addiction [replies_num] => 2 [views] => 21 )
I think you could try this:
$threadinfo = $this->thread_model->get_thread($page);
$data['thread'] = $threadinfo;
$data['replies'] = $this->thread_model->get_reply($threadinfo->thread_id);