Laravel - php How to get current Id while inserting value in Database? - php

I want to get the current ID while inserting the value in database.Which i will use to save custom Data. How Do I do it?
I have written a query already.
$currentId = Student::orderBy('id', 'desc')->get(['id']) + 1;
But it's showing the following Error :
Object of class Illuminate\Database\Eloquent\Collection could not be
converted to int

$currentId = Student::orderBy('id', 'desc')->first()->id + 1;
But on the heavy traffic website this potentially can cause problems with data integrity, so I'd user this approach:
$student = new Student();
....
$student->save();
$currentId = $student->id;

try this
$data = Student::orderBy('id', 'desc')->get(['id']);
$data->getId();

You have to do something like this :-
$insertData['name'] = 'your name';
$insertData['password'] = 'password';
..
etc..
// $insertData this will be your array which you want to insert in your db. name and password used in insertData array will be same as your table column name..
$lastId = Student::insertGetId($insertData);
It will give you last inserted id in laravel, it will help you.

Related

Fetching Data From MySQL with $this->db

So, I am trying to get the IMDb id for the from the table called videos. I tried it with these these are giving me error.
Code 1:
$imdb_id = $this->db->get_where('videos', array('imdbid'))->result_array();
Code 2:
$imdb_id = $this->db->get('videos','imdbid');
code 3:
$query = $this->db->query("SELECT * FROM videos;");
$row = $query->row(0, 'videos');
$imdb_id = $row['imdbid'];
Here is a screenshot of database table. Thank you for your contribution in advance.
Please explain the error and output expectations.
you want only one return data or multiple data?
This Codeigniter 3 right?
the current code you are using is incorrect :
Code 1 :
// will get only one data with where condition
// videos_id = 1
$video_id = 1;
$data = $this->db->get_where('videos', array('videos_id'=>$video_id))->result_array();
$imdb_id = $data[0]['imdbid'];
Code 2 :
// will get all data without where condition
// must use loop foreach
$data = $this->db->get('videos')->result_array();

getting an array instead of string from a query yii2

the task is to save the $model->image with row id in it.
to anticipate the next autoincrement id i use the query below. mysql returns right result, but when i try to save this query result in model it saves it like 'Array' string.
For example : article_Array_1516534305.png instead of article_13_1516534305.png
i tried to use array_shift() function, but result is the same
$connection = Yii::$app->getDb();
$dbName = $this->getDsnAttribute('dbname', Yii::$app->getDb()->dsn);
$query = new Query;
$query->select('AUTO_INCREMENT')
->from('information_schema.TABLES')
->where("TABLE_SCHEMA = '".$dbName."'")
->andWhere("TABLE_NAME = '".$this->tableName()."'");
$id = $query->one();
$this->image = 'article_'.$id.'_'.time(). '.' . $image->extension;
With $query->one() you get a single model
If you need only the column value you should use scalar()
$query->scalar();
http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html
http://www.yiiframework.com/doc-2.0/yii-db-query.html#scalar()-detail
or if you want the model however you can use
$id = $query->one();
$id = $id['AUTO_INCREMENT'];
the problem was that i had associated array
$id = $query->one();
$id = $id['AUTO_INCREMENT'];
now it works

How to add title in JPGraph from mysql database

I generate chart using jpgraph. I succesfully get data for the plot from database and i want the title also take from database. i already use this script
$sql = $this->db->select("title from table")->get()->first_row();
$title = $sql->title;
$graph->title->Set($title);
But that not work. can anyone solve this issue? thank you
This will help to get first_row along with field:
$this->db->select('title'); // your column
$this->db->from('table'); // your table
$result = $this->db->get()->result(); // get result
$title = $result->first_row()->title; // get ist row using first_row with your field name
$graph->title->Set($title); // as you are using for graph
Also note that, in CI function row() also return the ist row of your query in object form.
From the manual:
You can walk forward/backwards/first/last through your results using
these variations:
$row = $query->first_row()
$row = $query->last_row()
$row = $query->next_row()
$row = $query->previous_row()
Or if you want to print result in array instead of object than you can use a word 'array' as param, like:
$query->first_row(‘array’)
You can also follow the CI manual for better understanding: http://www.codeigniter.com/userguide3/database/results.html

Phalcon PHP: Update a primary natural key

Is there a good way to be able to update a table which has a natural key in Phalcon?
Consider this table:
people
------
person
created_at
updated_at
We're going to assume that the person field is unique and is the primary key. I try to do the following:
$person = new People();
$person->person = 'Ed';
$person->save();
$personUpdate = People::findFirst('person = "Ed"');
$personUpdate->person = 'Bob';
$person->save();
What Phalcon ends up trying to do is to INSERT a new record, rather than to update the existing record. What I need it to do is to UPDATE ... WHERE person = 'Ed';
Thoughts?
Thanks!
Try the following...
<?php
$personUpdate = People::findFirst('person = "Ed"');
$personUpdate->person = 'Bob';
$person->update();
You are doing correct except ... People::find
find will prepare to fetch all data.. this means its in array Documentation
You need to use findFirst instead of find
$personUpdate = People::findFirst('person = "Ed"');
$personUpdate->person = 'Bob';
$person->save();
Please note that you are using $person->update() instea of $personUpdate->update();
<?php
$personUpdate = People::findFirst('person = "Ed"');
$personUpdate->person = 'Bob';
$person->update();

Would this be an appropriate way to append two php variables onto a sql statement without overwriting the data already set in the sql field?

I want to append the $comment variable onto the $notes variable and update the LX table without overwriting the current data inside the note1 field of the LX table. So I've created a variable with the current note data ($notes) and upon update i set $notes + $comments for the particular account... Would this effectively be the correct solution?
$patient_ids = $_POST['patient_ids'];
$comment = strtoupper($_POST['comment']);
foreach($patient_ids as $id)
{
$patient = new patient($id);
$patient->insertComment(array('note'=>$comment));
$ltc_rx_event_id = sql::value ("SELECT id FROM ev.do.ent
WHERE task_id = 'LX' and status = 1 and patient_id = $patient->hex");
$notes = sql::value("Select note1 FROM ev.do.ent
Where task_id = 'LX' and status = 1 and patient_id = $patient->hex");
sql::query("update ev.do.LX set $notes + $comment where event_id = $ltc_rx_event_id");
}
echo "<h3 class='passed'>Comment has been added to " . count($patient_ids) . " patient(s)</h3>";
I feel it should work, I'm just wondering if there is something i'm missing or overlooking and if my syntax is correct using the "+" sign to append? Additionally, do i have to concatenate? Thanks for any help, it's greatly appreciated.
You may want to read this. It will save you some code.
http://www.plus2net.com/sql_tutorial/concat.php

Categories