I'm trying to get the id of last inserted record in the Db. But i'm getting the error
Parse error: syntax error, unexpected T_RETURN in Z:\www\CI4\application\models \report_model.php on line 69
my model:
function getLastInserted() {
$query ="SELECT $id as maxID from info where $id = LAST_INSERT_ID()"
return $query; //line 69
}
my controller:
function index()
{
$id=$this->report_model->getLastInserted();
$this->load->view('u_type1',$id);
}
Assuming you are using the CI database library, you can use $this->db->insert_id().
function getLastInserted() {
return $this->db->insert_id();
}
I think you are missing a ; on line 67 after the last ".
if you want to get last id without insert function
$this->db->select('id')->order_by('id','desc')->limit(1)->get('table_name')->row('id');
Related
How can I retrieve the second to last row of the id provided?
public function show($id)
{
if (! Gate::allows('backup_view')) {
return abort(401);
}
$backup = Backup::findOrFail($id);
$second = Backup::select('id')->union($backup)->where('id', '<=' , $backup->id);
dd($second);
return view('admin.backups.show', compact('backup', 'secondlast'));
}
Please help.
I assume you are trying to return the variable $second. You are returning a variable called 'secondlast'. Try changing the name of the variable to $secondlast.
In order to return the second last record from the Backup Model, the following query should work assuming your primary key is 'id':
Backup::orderBy('id', 'desc')
->skip(1)
->take(1)
->first();
I have an query, where it returns 1 or more results. After this query, I execute a delete on these tuples.
I think it's my coding failure, but what I'm trying to do is get this select and send it by ID parameter to Delete.
public function TestDelet(AcademicoRequest $request)
{
$academico = new Academico();
$res = $request->all();
$academico->member_id = $res->member_id;
if($res->member_id){
$dados_academicos[] = DB::table('academicos')->where('member_id', $res->member_id)->orderby('id')->get();
$this-> destroy($dados_academicos->Id);
}
}
List I try on postman:
{
"member_id": "1",
}
But, return this is error: Trying to get property of non-object
Hope these suggestions help:
$request->all();
the all() method is returning array format, so you need to change from
$res->member_id
into
$res['member_id']
I'm assuming you are trying to delete one/many rows from database at academicos table
if($res['member_id']){
Academico::where('member_id', $res['member_id'])->delete();
}
Please check values of object array:
dd($dados_academicos)
If you are using laravel with mysql by default it is id not Id and with mongodb it is _id. so use
$this-> destroy($dados_academicos->id);
OR
$this-> destroy($dados_academicos->_id);
OR
$this-> destroy($dados_academicos['Id']);
I have a scenario where the record is like this:
date
2018-01-15
2018-01-16
Now what I need to do is just delete the very last date. I need to delete the query that has 2018-01-15.
My code is like this:
public function deletelatDate()
{
// $datedel = '2018-04-03';
$datedel = Attendance::sortBy('date', 'desc')->first();
// $query->sortBy('date', 'desc')->first();
DB::table('attendances')->where('date', $datedel )->delete();
return back()->with('success','Last attendance entries successfully deleted');
}
Do you have any suggestions on how am I able to fix this?
Thanks.
Use orderBy and get column date after getting first record
public function deletelatDate()
{
$datedel = Attendance::orderBy('date', 'asc')->first();
DB::table('attendances')->where('date', $datedel->date )->delete();
return back()->with('success','Last attendance entries successfully deleted');
}
Also check success of query before sending success message
You can use latest to get the latest record: https://laravel.com/docs/5.5/queries#ordering-grouping-limit-and-offset
public function deleteLatest()
{
// $datedel = today()->format('Y-m-d'); <-- use today's date
$datedel = '2018-04-03';
// find the record to delete
$toDelete = Attendance::where('date', $datedel)->oldest('date')->first();
// only perform delete if a record is found
if($toDelete) {
$toDelete->delete();
return back()->with('success','Last attendance entries successfully deleted');
}
return back()->with('error', 'No attendance entry found for this date: ' . $datedel);
}
I am using codeigniter that has mysqli as db driver, am trying to call a simple stored procedure from my model but get an error. What am i doing wrong
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'pc()' at line 1
pc()
Filename: C:\hosted\saner.gy\ipa\system\database\DB_driver.php
Line Number: 330
When i run the query call Stored Procedure it runs well but from codeigniter it throws the above error
Stored Procedure
CREATE DEFINER=`root`#`localhost` PROCEDURE `pc`()
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
SELECT * FROM tbl_flo
WHERE name = 'sam';
END
Controller
public function sp()
{
$this->User_model->pc();
}
Model
public function pc()
{
$query = $this->db->query("pc()");
return $query->result();
}
Stored procedures are invoked using the CALL procedure_name(optional_params) query.
You need to edit the query used in your model like this:
public function pc()
{
$query = $this->db->query("CALL pc()");
return $query->result();
}
Here is dude, This block of code work for me at model
function get_sunmeter_for_initiator($data){
try {
$this->db->reconnect();
$sql = "CALL `get_sunmeter_for_initiator`(?, ?, ?)";
$result = $this->db->query($sql,$data); // $data included 3 param and binding & query to db
$this->db->close();
} catch (Exception $e) {
echo $e->getMessage();
}
return $result;
}
You are using the following way to call procedure.
$this->db->call_function('pc');
Or you can also use this
$this->db->query("call pc()");
simple issue I presume.
My controller is getting the if to display from the url using $this->uri->segment(3). This will always be a single value. I am putting this in an array to pass to the model with:
$customerid = array(
'id' => $this->uri->segment(3)
);
The controller syntax is below:
function confirm_delete_customer()
{
$data['title']="Confirm Customer Deletion";
$customerid=array(
'id'=>$this->uri->segment(3)
);
//query model to get data results for form
$data=array();
if($query=$this->model_master_data->get_customer_records_to_delete()){
$data['records']=$query;
$this->load->view("master_data/view_master_data_header",$data);
$this->load->view("master_data/view_master_data_nav");
$this->load->view("master_data/view_content_master_data_confirm_customer_deletion",$data);
$this->load->view("master_data/view_master_data_footer");
}
I am then trying to access this array value and pass it to my model to process. If I hard code the array into the model it works as per below syntax:
Model - Manual Syntax is:
function get_customer_records_to_delete()
{
$query = $this->db->get_where('customers', array('id'=>43));
return $query->result();
}
if I try replace this with the array from my controller it fails with error:
Undefined variable: customerid
idea of model that I want to get working:
function get_customer_records_to_delete()
{
$query = $this->db->get_where('customers', $customerid);
return $query->result();
}
I have a feeling it is something small. however is this the best way to get a single record from the database in order to output to a view?
Thanks in advance for the assistance.
The best way to do that is:
function confirm_delete_customer()
{
$data=array();
$data['title']="Confirm Customer Deletion";
$customerId = $this->uri->segment(3);
//Prevent SQL injections
if(!is_numeric($customerId) || empty($customerId)) {
show_error("Bad Request");
}
$query = $this->model_master_data->get_customer_records_to_delete($customerId);
if ($query){
$data['records']=$query;
$this->load->view("master_data/view_master_data_header",$data);
$this->load->view("master_data/view_master_data_nav");
$this->load->view("master_data/view_content_master_data_confirm_customer_deletion",$data);
$this->load->view("master_data/view_master_data_footer");
}
}
and then you can simply call:
function get_customer_records_to_delete($customerId)
{
$query = $this->db->get_where('customers', array('id'=>$customerId));
return $query->result();
}
at your model.
You need to pass the value as an argument to the function so it can access it.
Ex:
get_customer_records_to_delete($customerid)
{
// now $customerid is accessible
$query = ....;
return $……;
}
You should heavily rely on function parameters. Grab the customer id from the controller and send it to the model. Moreover, you can use row() to get a single result from the database.
Controller:
function confirm_delete_customer(){
$data['title']="Confirm Customer Deletion";
$customerid=$this->uri->segment(3);
//query model to get data results for form
$data=array();
if($query=$this->model_master_data->get_customer_records_to_delete( $customerid)) //you are sending customer id as a parameter here
$data['records']=$query;
$this->load->view("master_data/view_master_data_header",$data);
$this->load->view("master_data/view_master_data_nav");
$this->load->view("master_data/view_content_master_data_confirm_customer_deletion",$data);
$this->load->view("master_data/view_master_data_footer");
}}
Model
function get_customer_records_to_delete($customerid)
{
$query = $this->db->get_where('customers', array("id"=>$customerid)); //you are using the customer id sent from the controller here
return $query->row(); //this will return a single row
}
Old thread but the answer is to declare the variable as "public" in the controller (i.e. public $customerid;), in which case it'll be available to your model. In some cases it's probably safer to explicitly pass as an argument. However, when you have several variables, it's useful to have the option to declare them instead.