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
Related
I have this raw query that extracts data from the database table in Laravel
$username = DB::table('hotspot_users')
->select('userName')
->where('assignedTo', '786')->get();
I get these values: [{"userName":"kim"}]
but I want to extract just the username "kim" itself, not an array. I have tried to do this without success:
$convert1=json_decode($username);
$newusername=$convert1->userName;
but i get an error: Trying to get property 'userName' of non-object
Any idea how I can solve this
Instead of what you're doing, you can just do this:
HotspotUser::where('assignedTo', 786)->pluck('userName');
Now you will have a collection of names; or if you only want the first one:
HotspotUser::where('assignedTo', 786)->first()->user_name;
(I'm not certain if the property will be user_name or userName; you should have simple column names to make things easier.)
You are getting the result of DB as php stdClass object, so you can iterate over it and get every variable that is queried. Something like below code may help you.
In case of using get()
$data = DB::table('hotspot_users')
->where('assignedTo', '786')->select('userName')->get();
foreach ($data as $datum) {
$result[] = $datum->userName;
}
In case of using first()
$data = DB::table('hotspot_users')
->where('assignedTo', '786')->select('userName')->first()->userName;
Hi As you mentioned that is an array [{"userName":"kim"}]
So you have two choices
First , you first insted of get
$username = DB::table('hotspot_users')
->select('userName')
->where('assignedTo', '786')->first();
$newusername=$convert1->userName;
Second , get the first element of array
$username = DB::table('hotspot_users')
->select('userName')
->where('assignedTo', '786')->get();
$username = reset($username);
$convert1=json_decode($username);
$newusername=$convert1['userName'];
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
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.
After submitting a form in PHP the database field for one field has "Resource id #12" as its content. What is this supposed to mean and how do I find the issue?
$userid = $_POST['userid'];
$paid = $_POST['paid'];
$amountpaid = $_POST['amountpaid'];
$method = $_POST['method'];
$checknum = $_POST['checknum'];
$invoicenum = $_POST['invoicenum'];
mysql_query("INSERT INTO auction_payments (id,purchaser_id,
invoicenum, amountpaid, method, checknum,on_date) VALUES('','$userid','$invoicenum','$amountpaid',
'$method','$checknum',NOW())") or die(mysql_error());
When you echo or print the result from mysql_query it will return the Resource id #12.
After the INSERT return the number affected rows mysql_affected_rows and test if the result is larger than 0. I would recommend using mysqli as mysql is depreciated.
This is my current code:
$wonAwards = $altdb->get_var("SELECT achievement_id
FROM user_1
WHERE isTold='false'");
Currently, $wonAwards is set equal to the first result of the sql query. Is it possible to set $wonAwards equal to an array of all the results of the query?
////////////////// Full Function
function the_header_function(){ //called when wordpress header is loaded
if (is_user_logged_in()){ //checks if user is logged in
global $altdb; //wp global database object
$user_info = wp_get_current_user();
$wonAwards = $altdb->query("SELECT achievement_id FROM user_".$user_info->ID." WHERE isTold='false'"); //query
if(is_array($wonAwards)) $hello='true';
if(!is_array($wonAwards)) $hello='false';
if($wonAwards != ''){
echo "<script>jQuery(document).ready( function(){alert('".$hello."');});</script>";
}
}
}
As for checking what the result of my query was, I simply changed this:
echo "<script>jQuery(document).ready( function(){alert('".$hello."');});</script>";
to this:
echo "<script>jQuery(document).ready( function(){alert('".$wonAwards."');});</script>";
Well, according to the Documentation Here, get_var will give you a single result, and query will give you the number of results.
What you really wants (getting all results) can be achieved by the get_results method, so you should write:
$wonAwards = $altdb->get_results("SELECT achievement_id
FROM user_1
WHERE isTold='false'");
Try it and tell us if it works, hope that's helpful.
As i understand that you want to set array instead of equals
Hope this will work:
$wonAwards = $altdb->get_var("SELECT achievement_id
FROM user_1
WHERE isTold
IN ('false', 'true', 'another_value', 'some_other')");
Edit: for #Shahmeer Navid
if you have some data in an array like
$array = array("data1", "data2", "data3");
$implode = implode("', '", $array);
//Output or $implode
data1', 'data2', 'data3
//apply on query
$wonAwards = $altdb->get_var("SELECT achievement_id
FROM user_1
WHERE isTold
IN ('$implode')");
PS Also use mysql_real_escape_string() if you get the data from the client