I Have Database Errors on my Codeigniter - codeigniter

i receive the following errors when i try to login to my project, Please Help
A Database Error Occurred
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 'WHERE `_id` = '1'' at line 2
SELECT * WHERE `_id` = '1'
Filename: views/backend/header.php
Line Number: 34
...........................................................................
Here is the code line for the header file
..........................
<?php
$name = $this->db->get_where($this->session->userdata('login_type'),
array($this->session->userdata('login_type').'_id' => $this->session->userdata('login_user_id')))
->row()
->name;
echo $name;
A Database Error Occurred
Error Number: 1054
Unknown column '_id' in 'where clause'
UPDATE `ci_sessions` SET `timestamp` = 1519230907 WHERE `_id` = '1' AND `id` = 'd25c0dcdaa9a86810d791b05ba53fe45b76a7bcd'
Filename: libraries/Session/drivers/Session_database_driver.php
Line Number: 243

You didn't define from which table you have to fetch data, that's why this error comes. Normally we write query like
SELECT * FROM `table_name` WHERE `_id` = 1
you just the missed out selecting table. In codeigniter form the query should like
$this->db->select('*');
$this->db->from('table_name');
$this->db->where('_id', '1');
$data = $this->db->get();
This is the normal procedure to fetch data in codeigniter.

It's better to add manually column and table name. And value store in variable
$id = $this->session->userdata('login_user_id');
$name = $this->db->get_where('table-name',array('column-name' => 'your-value'))->row();
$echo $name->column-name; //Using this code you can echo all your data retrieved from database

Related

Laravel Query Builder with CONCAT and DATE_FORMAT giving errorr

I have a query like below:
$events = Event::select(DB::raw('CONCAT(title," : 📅", DATE_FORMAT(date,"%M %d %Y") AS event'), 'id')->where('club_id', '=', \Auth::guard('web_club')->user()->id)->where('date','>',date("Y-m-d",strtotime("-2days")))->orderBy('date','asc')->pluck('event', 'id');
I am trying to format the datetime into a more legible format but keep getting the error:
"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from `events` where `club_id` = ? and `date` > ? order by `date` asc' at line 1 (SQL: select CONCAT(title," : ", (DATE_FORMAT(date,"%M %d %Y")) AS event, `id` from `events` where `club_id` = 75 and `date` > 2020-03-13 order by `date` asc) "
Can anyone see what I am doing wrong at all or alternative methods to achieve the same result?
I was missing a bracket near here which was responsible for the error.
DATE_FORMAT(date,"%M %d %Y"))

Laravel unable to update field to null

I can't figure out how to set a field to null on certain condition. It should be an easy query but I'n not able to set the right value for NULL on a nullable field.
Product::where('id', $product_list)->update(['family_id' => null]);
nor
Product::where('id', $product_list)->update(['family_id' => DB::raw(null)]);
did the trick.
Both cases compiled fine but the generated query seems to be wrong because I get a SQL error. In the irst case the error is :
SQLSTATE[HY093]: Invalid parameter number (SQL: update `products` set `family_id` = ?, `products`.`updated_at` = 2019-11-12 08:41:07 where `id` = ?)
and the error for the second one is :
QLSTATE[42000]: Syntax error or access violation: 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 ', `products`.`updated_at` = ? where `id` = ?' at line 1 (SQL: update `products` set `family_id` = , `products`.`updated_at` = 2019-11-12 08:21:50 where `id` = ?)
In the first case it seems that NULL is not being added to the parameters to the prepared query, and in the secon the NULL value is being inserted as nothing in the query. There is any simple way to set the value to NULL?
You should consider the following things first.
1) family_id column is nullable in your table. if not then set nullable.
2) Make sure you have in $fillable property of Product model column family_id.
Make sure $product_list returns id of that particular product.
if $product_list is collection/array of product data then use like:
Product::where('id', $product_list->id)->update(['family_id' => null]);
Try this
Product::where('id', $product_list)->update(['family_id' => 'NULL']);

Laravel - select temporary column where temporary column

$search_str = "full name search";
$user = App\User::selectRaw("CONCAT(`f_name`, `l_name`) AS `fullname`")
->where('fullname', 'LIKE', '%$search_str%')
->get();
Based on the code above, the column fullname actually does not exist in DB Table. fullname column is just a temporary column.
But when I use it on where clause, Laravel return an error:
Column not found: 1054 Unknown column 'fullname' in 'where clause'
You can use the CONCAT function in your where filter.
$search_str = "full name search";
$user = App\User::selectRaw("CONCAT(`f_name`, `l_name`) AS `fullname`")
->whereRaw("CONCAT(`f_name`, `l_name`) LIKE '%?%'", [$search_str])
->get();
Use Having for aliases. See below reference link.
https://laravel.com/docs/5.6/queries#ordering-grouping-limit-and-offset

Laravel morph query join relationship

I need to do a join statement on a query that has a morph relationship and I need to select the first image row and a field which is images.small but I'm getting the following error.
Query:
Movie::select(['id', 'images.small as poster', 'title', 'release_date', 'created_at', 'updated_at'])->join('images', function($join) {
$join->on('images.imageable_id', '=', 'movie.id')
->where('images.imageable_type', '=', 'App\Movie')->first();
})
Error:
(2/2) QueryException
SQLSTATE[42000]: Syntax error or access violation: 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 'on images.imageable_id = movie.id and images.imageable_type = ? limi' at line 1 (SQL: select * on images.imageable_id = movie.id and images.imageable_type = App\Movie limit 1)

Laravel4.2: Eloquent - Use DATE_FORMAT in SQL query

I am trying this simple sql query
SELECT DATE_FORMAT(my_date, '%m-%d-%Y') FROM my_tbl WHERE id = 1
in Eloquent like
$tbl = DB::table('my_tbl');
$tbl->select( 'DATE_FORMAT(my_date, "%m-%d,-%Y")' );
$tbl->where( 'id', "=", 1);
Datatable::query($tbl);
but it accepts as string and throws error:
{"error":{"type":"Illuminate\\Database\\QueryException","message":"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'DATE_FORMAT(arrival, \"%d %M, %Y\")'
Okay I just add DB::raw and it worked :)
$tbl->select( 'DB::raw('DATE_FORMAT(my_date, \'%m-%d-%Y\') AS arrival')' );

Resources