Results of a query in CodeIgniter - codeigniter

I want to know where I can retrieve the result of a query in Codeigniter.
The code:
$this->db->where('usuario', $this->input->post('drcorreo'));
$cdb=$this->db->get('usuarios');
What I want is where is saved the name of the user in this query in order to save it in the session class.
Thanks.

Try this:
### saving in the session ###
$rs = $this->db->where('usuario', $this->input->post('drcorreo'))->get('usuarios');
if($rs->num_rows() > 0){
$data = $rs->row_array(); #for a single tuple use result_array() for multiple tuples
$this->session->set_userdata('usuario', $data['usuario']); #saving it in session
}
### to retrieve dfrom the session ###
$usuario = $this->session->userdata('usuario');

You can try this:
$result = $this->db->get_where($table, array('username' =>$this->input->post('drcorreo')));
$result->result_array();
for documentation you may read it in here Codeigniter

Related

Get query string in laravel 6

I have a problem here I send the date via url, so my url becomes like this
localhost:8000/info/transaksi/89?start_date=2019-09-20&end_date=2019-12-20
I want to take my time to be a variable containing something like this
$var = ?start_date=2019-09-20&end_date=2019-12-20
can it be posible?
or can I replace my web.php url request on the controller?
localhost:8000/info/transaksi/89?start_date=2019-09-20&end_date=2019-12-20
be like this
sandbox/synch/account/89?start_date=2019-09-20&end_date=2019-12-20
i'm only need start_date=2019-09-20&end_date=2019-12-20
i try with
$input = $request->all();
but it produces a different format because it becomes an array
Get query string:
$uri = \Request::getRequestUri();
$query = parse_url($uri)['query'];
$query is the query string.
get explicitly data.
$start_date = request()->start_date;
$end_date = request()->end_date;
Get raw query string:
$request->getQueryString()

How to get string value using eloquent query

I am trying to execute a query using eloquent, however it returns me an array. What I want is to only get the string value of my query.
This is what I get
[{"name":"hey"}] [{"name":"sdasdasd"}]
Here's my eloquent query:
$categoryName = Category::select('name')->where('id', request('category'))->get();
$subCategory = Subcategory::select('name')->where('id', request('subCat'))->get();
as per the Laravel Docs
Retrieving A Single Row / Column From A Table
If you just need to retrieve a single row from the database table, you may use the first method. This method will return a single stdClass object:
$user = DB::table('users')->where('name', 'John')->first();
echo $user->name;
If you don't even need an entire row, you may extract a single value from a record using the value method. This method will return the value of the column directly:
$email = DB::table('users')->where('name', 'John')->value('email');
so in your example maybe you need to do something like the following :
$subCategory = Subcategory::select('name')->where('id', request('subCat'))-> first();
echo $subCategory->name;
There are so many ways to achive your goal.
Solutions
Use pluck().
$categoryName = Category::select('name')
->where('id', request('category'))
->pluck()
->first();
Use model properties.
$categoryName = Category::find(request('category'))
->name;
If you take only name then follow the below code.
$categoryName = Category::where('id', request('category'))->first()->name;
$subCategory = Subcategory::where('id', request('subCat'))->first()->name;
echo $categoryName;
echo $subCategory;
If you take all columns in a row follow the below code:
$categoryName = Category::where('id', request('category'))->first();
$subCategory = Subcategory::where('id', request('subCat'))->first();
echo $subCategory->name;
echo $categoryName->name;
I hope this i may help you.Try this.
You can get the answer using
$name = DB::table('Category')->where('id','=',request('category'))->pluck('name')->first();
echo $name;
$name = Category::select('name')->where('id', request('category'))->take(1)->orderBy('id','asc')->first()->name;
echo $name;
OR
echo Category::select('name')->where('id', request('category'))->take(1)->orderBy('id','asc')->first()->name;

Doctrine ORM Update Row with ID

Is there a way to update a row directly with and ID? I just want the ability to update a table row field without querying for the object first. I tried this...
$id = 1;
$s = new Sandbox();
$s->setId($id);
$s->setFname('moon');
$e = $em->merge($s);
$em->flush($e);
and it tried to do an update to the database, however, it failed because it tried to update all the undefined fields as well whereas I just want to update the fname field.
Thanks
$id = 1;
$s = $em->getReference('Sandbox', $id);
$s->setFname('moon');
$em->persist($s);
$em->flush($s);

Codeigniter Active Record return type

I tried to get a umat_id with this SQL query :
SELECT umat_id FROM msumat WHERE nama = $nama
I converted this SQL query into CI's Active Record :
$this->db->select('umat_id');
$terdaftar = $this->db->get_where('msumat', array('nama' => $nama));
So this query should return a string (example : "John").
But I got an error when I tried to echo it :
Object of class CI_DB_mysql_result could not be converted to string
I have tried something like this : echo (string)$terdaftar;, but it's not working.
All I want is to echo "John"
EDIT
Just said I want to insert "John" into a variable. How to do that?
$john = ????
As some of the users already pointed the solution, I'm only explaining why you did get this error so you can understand better the querying results that codeigniter gives.
This error:
But I got an error when I tried to echo it : Object of class
CI_DB_mysql_result could not be converted to string
Happens because you were trying to echo an object.
This piece of code
$terdaftar = $this->db->get_where('msumat', array('nama' => $nama));
Will return an object, this object will have information about the query you've done.
With this object you can get the result(rows) as objects doing this:
$results = $terdaftar->result();
Or you if you feel more comfortable with arrays you can return the results(rows) as an array doing this:
$results = $terdaftar->result_array();
You can also get the number of results doing this:
$number_results = $terdaftar->num_rows()
And this is just an example you can read more about the results here
http://ellislab.com/codeigniter/user-guide/database/results.html
EDIT
A better explanation: imagine that we use the result_array() function to get the result in a pure array format:
$results = $terdaftar->result_array();
Now your variable $results is an array, to iterate through it and get the data you want you'll do something like this:
foreach ($results as $key => $row) {
//the row variable will have each row of your database returned by your query
//so if you want to access a field from that row,
//let's say for example the name field. You would do something like this
if($row['name']=='John')
echo $row['name'];
}
Try:
$this->db->select('umat_id');
$terdaftar = $this->db->get_where('msumat', array('nama' => $nama));
foreach ($terdaftar->result() as $row)
{
echo $row->umat_id;
}
Read the documentation for more information.
Try this:
$this->db->select('umat_id');
$terdaftar = $this->db->get_where('msumat', array('nama' => $nama));
$row = $terdaftar->row_array();
$your_variable = $row['umat_id']; /*Here comes your john*/

How to optimize saving multiple models?

I need to manually save a very large number of order items to the database. I'm currently doing something like this:
for($x=0; $x<250000; $x++)
{
$orderItem = Mage::getModel('sales/order_item');
$data = array('a'=>1, 'b'=>2, 'c'=>3);
$orderItem->setData($data)->save();
}
Attempting to run this code from a shell script takes forever. What strategies can I use to speed up this code?
I'm not sure but take a look # Mage::getModel('core/resource_transaction')
See https://stackoverflow.com/a/4879133/1191288
You could try doing a batch save on ever X amount
$batch = 500;
$orderItem = Mage::getModel('sales/order_item');
$transactionSave = Mage::getModel('core/resource_transaction');
for($x=0; $x<250000; $x++){
$orderItem->setData( array('a'=>1, 'b'=>2, 'c'=>3));
$transactionSave->addObject($orderItem);
$orderItem->reset()
if ($x % $batch == 0){
$transactionSave->save();
$transactionSave = null;
$transactionSave = Mage::getModel('core/resource_transaction');
}
}
if($x % $batch > 0)
$transactionSave->save();
I think in this situation, your best bet to speed this up significantly would be to create custom database queries.
Edit: Related option: try to rewrite the model and resource model to implement a method bulkSave() which is like save() but only creates the corresponding query object and returns it, so it does not actually use the database. Then collect all the queries and run them in big transactions every thousand items or so.

Resources