Convert MYSQL RAW included hash(encrypt) query to Laravel eloquent query - mysql

This is the raw query
"SELECT * FROM re_customer
WHERE LOWER(email) = '" . $input['email'] . "'
AND
(password = SHA1(CONCAT(salt, SHA1(CONCAT(salt, SHA1('" . $input['password'] . "')))))
OR password = '" . md5( $input['password'] ) . "')
AND status = '1'";
I tried
$customer = DB::connection( 'oc' )
->table( Customer::$customerTable )
->where( DB::raw( 'LOWER(email)' ), '=', mb_strtolower( $input['email'] ) )
->whereRaw( "(password = SHA1(CONCAT(salt, SHA1(CONCAT(salt, SHA1('?'))))) OR password = '?')", [
$input['password'],
md5($input['password'])
] )
->where( 'status', 1 )
->first();
But not working for me (both query results are not same). Can you please address me that what wrong on this query.

I solved the query conversion.
$customer = DB::connection( 'oc' )
->table( Customer::$customerTable )
->where( DB::raw( 'LOWER(email)' ), '=', mb_strtolower( $input['email'] ) )
->where( 'status', 1 )
->where( function ( $query ) use ( $input ) {
$query->whereRaw( "password = SHA1(CONCAT(salt, SHA1(CONCAT(salt, SHA1(?)))))", $input['password'] )
->orWhere( 'password', '=', md5( $input['password'] ) );
} )
->first();
this is working like a charm

Related

How to make Laravel eloquent request with 1 filter on many fields

In my Laravel 5.7/ mysql app I make request on a form with 10 filter inputs,
one of them ($filter_search) if non empty must be compared with all fields(string, number,
date, ref to fields of other tables) in resulting listing.
I made scope on this table fields:
public function scopeGetBySearch($query, $search = null)
{
if (empty($search)) {
return $query;
}
$tb= with(new StorageSpace)->getTable();
return $query->where(
$tb.'.number', $search
) ->orWhere(function ($query) use ($search, $tb) {
$query->where( $tb.".notes", 'like', '%'.$search.'%' )
->orWhere($tb.".selling_range", $search)
->orWhere($tb.".actual_storage_rent", $search)
->orWhere($tb.".insurance_vat", $search)
// ->havingRaw("job_ref_no", $search)
})
But I have a problem how can I set filter on job_ref_no field from other table :
$storageSpacesCollection = StorageSpace
::getByStatus($filter_status)
->getById($relatedStorageSpacesArray)
->getByLocationId($filter_location_id, 'warehouses')
->getByCapacityCategoryId($filter_capacity_category_id, 'storage_capacities')
->getByLevel($filter_level)
->getByNumber($filter_number, true)
->orderBy('storage_spaces.id', 'asc')
->getByStorageCapacityId($filter_storage_capacity_id)
->getByClientId($filter_client_id)
->getByColorId($filter_color_id)
->getBySearch($filter_search)
// ->havingRaw("job_ref_no = " . $filter_search)
->leftJoin( 'storage_capacities', 'storage_capacities.id', '=', 'storage_spaces.storage_capacity_id' )
->leftJoin( 'warehouses', 'warehouses.id', '=', 'storage_spaces.warehouse_id' )
->leftJoin( 'clients', 'clients.id', '=', 'storage_spaces.client_id')
->select(
"storage_spaces.*",
\DB::raw( "CONCAT(storage_capacities.count, ' ', storage_capacities.sqft ) as storage_capacity_name" ),
\DB::raw("( SELECT check_ins.job_ref_no FROM check_ins WHERE // I got job_ref_no field in subquesry check_ins.storage_space_id=storage_spaces.id ORDER BY check_ins.id ASC limit 1 ) AS job_ref_no"),
"warehouses.name as warehouse_name",
"clients.full_name as client_full_name")
->get();
havingRaw does not work both in the scope and in the request above if to uncomment it.
I tried to use addSelect, like:
But with request :
$storageSpacesCollection = StorageSpace
::getByStatus($filter_status)
->whereRaw('storage_spaces.id <= 8') // DEBUGGING
->getById($relatedStorageSpacesArray)
->getByLocationId($filter_location_id, 'warehouses')
->getByCapacityCategoryId($filter_capacity_category_id, 'storage_capacities')
->getByLevel($filter_level)
->getByNumber($filter_number, true)
->orderBy('storage_spaces.id', 'asc')
->getByStorageCapacityId($filter_storage_capacity_id)
->getByClientId($filter_client_id)
->getByColorId($filter_color_id)
->getBySearch($filter_search)
// ->havingRaw("job_ref_no = " . $filter_search)
->leftJoin( 'storage_capacities', 'storage_capacities.id', '=', 'storage_spaces.storage_capacity_id' )
->leftJoin( 'warehouses', 'warehouses.id', '=', 'storage_spaces.warehouse_id' )
->leftJoin( 'clients', 'clients.id', '=', 'storage_spaces.client_id')
->select(
"storage_spaces.*",
\DB::raw( "CONCAT(storage_capacities.count, ' ', storage_capacities.sqft ) as storage_capacity_name" ),
"warehouses.name as warehouse_name",
"clients.full_name as client_full_name")
->addSelect([
'job_ref_no' => CheckIn::selectRaw('job_ref_no')->whereColumn('check_ins.storage_space_id', 'storage_spaces.id'),
])
->get();
But I got an error:
local.ERROR: stripos() expects parameter 1 to be string, object given {"userId":11,"email":"nilovsergey#yahoo.com","exception":"[object] (ErrorException(code: 0): stripos() expects parameter 1 to be string, object given at /mnt/_work_sdb8/wwwroot/lar/the-box-booking/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php:1031)
[stacktrace]
#0 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'stripos() expec...', '/mnt/_work_sdb8...', 1031, Array)
#1 /mnt/_work_sdb8/wwwroot/lar/the-box-booking/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php(1031): stripos(Object(Illuminate\\Database\\Eloquent\\Builder), ' as ')
#2 [internal function]: Illuminate\\Database\\Query\\Grammars\\Grammar->wrap(Object(Illuminate\\Database\\Eloquent\\Builder))
Which way is valid ?
MODIFIED BLOCK:
I try to create sql I need manually.
With Laravel request :
$storageSpacesCollection = StorageSpace
::getByStatus($filter_status)
->whereRaw('storage_spaces.id <= 8') // DEBUGGING
->getById($relatedStorageSpacesArray)
->getByLocationId($filter_location_id, 'warehouses')
->getByCapacityCategoryId($filter_capacity_category_id, 'storage_capacities')
->getByLevel($filter_level)
->getByNumber($filter_number, true)
->orderBy('storage_spaces.id', 'asc')
->getByStorageCapacityId($filter_storage_capacity_id)
->getByClientId($filter_client_id)
->getByColorId($filter_color_id)
->getBySearch($filter_search)
->leftJoin( 'storage_capacities', 'storage_capacities.id', '=', 'storage_spaces.storage_capacity_id' )
->leftJoin( 'warehouses', 'warehouses.id', '=', 'storage_spaces.warehouse_id' )
->leftJoin( 'clients', 'clients.id', '=', 'storage_spaces.client_id')
->select(
"storage_spaces.*",
\DB::raw( "CONCAT(storage_capacities.count, ' ', storage_capacities.sqft ) as storage_capacity_name" ),
\DB::raw("( SELECT check_ins.job_ref_no FROM check_ins WHERE check_ins.storage_space_id=storage_spaces.id ORDER BY check_ins.id ASC limit 1 ) AS job_ref_no"),
"warehouses.name as warehouse_name",
"clients.full_name as client_full_name")
->get();
And tracing I see sql statement with job_ref_no column :
SELECT `storage_spaces`.*, CONCAT(storage_capacities.count, ' ', storage_capacities.sqft ) AS storage_capacity_name, ( SELECT check_ins.job_ref_no
FROM check_ins
WHERE check_ins.storage_space_id=storage_spaces.id
ORDER BY check_ins.id ASC limit 1 ) AS job_ref_no, `warehouses`.`name` AS `warehouse_name`, `clients`.`full_name` AS `client_full_name`
FROM `storage_spaces`
LEFT JOIN `storage_capacities` on `storage_capacities`.`id` = `storage_spaces`.`storage_capacity_id`
LEFT JOIN `warehouses` on `warehouses`.`id` = `storage_spaces`.`warehouse_id`
LEFT JOIN `clients` on `clients`.`id` = `storage_spaces`.`client_id`
WHERE storage_spaces.id <= 8 AND (`storage_spaces`.`number` = '999' OR
(`storage_spaces`.`notes` like '%999%' OR
`storage_spaces`.`selling_range` = '999' OR
`storage_spaces`.`actual_storage_rent` = '999' OR
`storage_spaces`.`insurance_vat` = '999') )
ORDER BY `storage_spaces`.`id` asc
I want to set filter on job_ref_no column manually :
SELECT `storage_spaces`.*, CONCAT(storage_capacities.count, ' ', storage_capacities.sqft ) AS storage_capacity_name, ( SELECT check_ins.job_ref_no
FROM check_ins
WHERE check_ins.storage_space_id=storage_spaces.id
ORDER BY check_ins.id ASC limit 1 ) AS job_ref_no, `warehouses`.`name` AS `warehouse_name`, `clients`.`full_name` AS `client_full_name`
FROM `storage_spaces`
LEFT JOIN `storage_capacities` on `storage_capacities`.`id` = `storage_spaces`.`storage_capacity_id`
LEFT JOIN `warehouses` on `warehouses`.`id` = `storage_spaces`.`warehouse_id`
LEFT JOIN `clients` on `clients`.`id` = `storage_spaces`.`client_id`
WHERE storage_spaces.id <= 8 AND (`storage_spaces`.`number` = '999' OR
(`storage_spaces`.`notes` like '%999%' OR
`storage_spaces`.`selling_range` = '999' OR
`storage_spaces`.`actual_storage_rent` = '999' OR
`storage_spaces`.`insurance_vat` = '999' OR
job_ref_no = '999' ) // I added this line #
)
ORDER BY `storage_spaces`.`id` asc
But I got error :
Error in query (1054): Unknown column 'job_ref_no' in 'where clause'
Which is valid way in raw sql and how it can be implemented with eloquent ?
MODIFIED BLOCK # 2:
I try to make with join:
$storageSpacesCollection = StorageSpace
::getByStatus($filter_status)
->getById($relatedStorageSpacesArray)
->getByLocationId($filter_location_id, 'warehouses')
->getByCapacityCategoryId($filter_capacity_category_id, 'storage_capacities')
->getByLevel($filter_level)
->getByNumber($filter_number, true)
->orderBy('storage_spaces.id', 'asc')
->getByStorageCapacityId($filter_storage_capacity_id)
->getByClientId($filter_client_id)
->getByColorId($filter_color_id)
->getBySearch($filter_search)
->leftJoin( 'storage_capacities', 'storage_capacities.id', '=', 'storage_spaces.storage_capacity_id' )
->leftJoin( 'warehouses', 'warehouses.id', '=', 'storage_spaces.warehouse_id' )
->leftJoin( 'clients', 'clients.id', '=', 'storage_spaces.client_id')
->leftJoin( 'check_ins', 'check_ins.storage_space_id', '=', 'storage_spaces.id') // I ADDED THIS LINE
->select(
"storage_spaces.*",
\DB::raw( "CONCAT(storage_capacities.count, ' ', storage_capacities.sqft ) as storage_capacity_name" ),
// \DB::raw("( SELECT check_ins.job_ref_no FROM check_ins WHERE check_ins.storage_space_id=storage_spaces.id ORDER BY check_ins.id ASC limit 1 ) AS job_ref_no"),
"warehouses.name as warehouse_name",
"check_ins.job_ref_no as job_ref_no",
"clients.full_name as client_full_name")
->distinct()
->get();
and I have a sql :
SELECT distinct `storage_spaces`.*, CONCAT(storage_capacities.count, ' ', storage_capacities.sqft ) AS storage_capacity_name,
`warehouses`.`name` AS `warehouse_name`, `check_ins`.`job_ref_no` AS `job_ref_no`, `clients`.`full_name` AS `client_full_name`
FROM `storage_spaces`
LEFT JOIN `storage_capacities` on `storage_capacities`.`id` = `storage_spaces`.`storage_capacity_id`
LEFT JOIN `warehouses` on `warehouses`.`id` = `storage_spaces`.`warehouse_id`
LEFT JOIN `clients` on `clients`.`id` = `storage_spaces`.`client_id`
LEFT JOIN `check_ins` on `check_ins`.`storage_space_id` = `storage_spaces`.`id`
WHERE ( `storage_spaces`.`number` = 'S1-102' OR (`storage_spaces`.`notes` like '%S1-102%' OR `storage_spaces`.`selling_range` = 'S1-102' OR
`storage_spaces`.`actual_storage_rent` = 'S1-102' OR `storage_spaces`.`insurance_vat` = 'S1-102' OR `job_ref_no` = 'S1-102') )
ORDER BY `storage_spaces`.`id` asc
I have have different results
I need to get only last row from check_ins, that is why in my request I have limit 1:
\DB::raw("( SELECT check_ins.job_ref_no FROM check_ins WHERE check_ins.storage_space_id=storage_spaces.id ORDER BY check_ins.id ASC limit 1 ) AS job_ref_no"),
that is why have have several rows of as storage_spaces as all check_ins ae joined
2) I have all rows from storage_spaces and I do not see why
I tried to change leftJoin with Join /rightJoin but it did not help...
Thanks!
You need to set the join into the main query instead of select query because in select query it's not a actual column which you set into the where condition.
I found a decision addin in scope :
->orWhereRaw("(SELECT check_ins.job_ref_no FROM check_ins WHERE
check_ins.storage_space_id=storage_spaces.id ORDER BY check_ins.id ASC LIMIT 1) = '".$search."'")
and that works for me.

Laravel 5: whereRaw escapes integer to string

What is the Laravel eloquent query for this:
select * from `jobs` where (
400000 between min_salary and max_salary
or
600000 between min_salary and max_salary
);
I tried the below eloquent query which encapsulates the integer to string
$min = 400000;
$max = 600000;
Job::whereRaw('
? between min_salary and max_salary
or
? between min_salary and max_salary',
[$min,$max]
)->get();
Also tried Casting and DB::Raw none of the options were worked as expected.
protected $casts = [
'min_salary' => 'integer',
'max_salary' => 'integer',
];
$min = 400000;
$max = 600000;
Job::whereRaw('
? between min_salary and max_salary
or
? between min_salary and max_salary',
[DB::Raw($min),DB::Raw($max)]
)->get();
I tried the below eloquent query works as expected but i have hard coded the query directly(Unsafe)
$min = 400000;
$max = 600000;
Job::whereRaw(
$min.' between min_salary and max_salary
or
'.$max.' between min_salary and max_salary'
)->get();
Try this:
Job::where(function($query){
$query->where('min_salary','<',400000);
$query->where('max_salary','>',400000);
})->orWhere(function($query){
$query->where('min_salary','<',600000);
$query->where('max_salary','>',600000);
})->get();

SQL, MySQL and php

I have 2 tables
CREATE TABLE `employee` (
`Employee_ID` smallint(4) NOT NULL AUTO_INCREMENT,
`Name` varchar(25) NOT NULL DEFAULT '',
PRIMARY KEY (`Employee_ID`)
) ;
--- -----------------
CREATE TABLE `attendance` (
`Attendance_ID` int(9) NOT NULL AUTO_INCREMENT,
`Employee_ID` smallint(3) NOT NULL,
`Date` date NOT NULL,
`Attendance` char(1) NOT NULL DEFAULT '',
PRIMARY KEY (`Attendance_ID`)
) ;
For Attendance field it's a "P" when the employee is present "L" when late and "A" when absent.
I want to make a query that crosses the dates with employees names and show atendance.
Something similar to:
Attendance 07/02/2015 14/02/2015 21/02/2015 ...
Diane P P L
Robert A P P
Karine P P A
...
I didn't post any query because actually I failed many times to figure out how to
Thanks for help
I do not know how to get dynamic columns in MySQL. This is how I would do it:
SELECT attd.Date, attd.Attendance, empl.Name FROM attendance attd, employee empl WHERE empl.Employee_ID = attd.Employee_ID
That will give result:
Then I would use server side code (example PHP):
<?php
$attendance = array();
foreach ($query as $row) {
if (isset($attendance[$row['Name']])) {
$attendance[$row['Name']][$row['Date']] = $row['Attendance'];
} else {
$attendance[$row['Name']] = array($row['Date'] => $row['Attendance']);
}
}
print_r($attendance);
/* Array(
"Diane" => Array("2015-02-07" => 'P', "2015-02-14" => 'P'),
"Robert" => Array("2015-02-07" => 'P', "2015-02-14" => 'P'),
"Karine" => Array("2015-02-07" => 'L', "2015-02-14" => 'L')
)
*/
select
a.Employee_ID,
min(e.Name) as Employee_Name,
case when a.Date = dateadd(:basedate, interval 7*0 day) then min(a.Attendance) end as d0 /* d for data? */
case when a.Date = dateadd(:basedate, interval 7*1 day) then min(a.Attendance) end as d1
case when a.Date = dateadd(:basedate, interval 7*2 day) then min(a.Attendance) end as d2
case when a.Date = dateadd(:basedate, interval 7*3 day) then min(a.Attendance) end as d3
dateadd(:basedate, interval 7*0 day) as week0
dateadd(:basedate, interval 7*1 day) as week1
dateadd(:basedate, interval 7*2 day) as week2
dateadd(:basedate, interval 7*3 day) as week3
from
attendance as a inner join employee as e ...
where a.Date between :basedate and dateadd(:basedate, interval 7*3 day)
group by
a.Employee_ID
You could pass in a :basedate parameter and offset a fixed number of weeks from that point. (I don't know what's the parameter convention for PHP and MySQL.) This will work if you can fix your results to a set number of weeks.
Pick out the dates from the first row to build the header and then build the rest of the table as you normally would. Yes there's some redundant data but there's no easy way to alias the columns with the dates themselves.
Following query you can use to gain the shown output along with a bit of PHP application
SELECT E.Name, A.Date, A.Attendance
FROM attendance AS A
INNER JOIN employee AS E
ON A.Employee_ID = E.Employee_ID
ORDER BY A.Date, E.Name
For implementing this in PHP
<?php
if (!$link = mysql_connect('< mysql_host >', '< mysql_user >', '< mysql_password >')) {
echo 'Could not connect to mysql';
exit;
}
if (!mysql_select_db('< mysql_dbname >', $link)) {
echo 'Could not select database';
exit;
}
$sql = 'SELECT E.Name, A.Date, A.Attendance '.
'FROM attendance AS A '.
'INNER JOIN employee AS E '.
'ON A.Employee_ID = E.Employee_ID '.
'ORDER BY E.Name, A.Date';
$result = mysql_query($sql, $link);
if (!$result) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
$name = '';
$resultArray = array();
$attendance = array();
foreach ($query as $row) {
if (isset($attendance[$row['Name']])) {
array_push($attendance[$row['Name']], $row['Date'] => $row['Attendance']);
} else {
$attendance[$row['Name']] = array($row['Date'] => $row['Attendance']);
}
}
print_r($attendance);
mysql_free_result($result);
?>
You would get an a associative array like this
Array(
"Diane" => Array("2015-02-07" => 'P', "2015-02-14" => 'P'),
"Robert" => Array("2015-02-07" => 'P', "2015-02-14" => 'P'),
"Karine" => Array("2015-02-07" => 'L', "2015-02-14" => 'L')
)
Now you can use this associative array to in a loop to render these elements in the Web page
Hope this helps...

Google chart values from different times

Working on a google line chart. Having the problem that two of the values (T_Temperatur) And (T_Badende_per_Time) is inserted into the database at the same time with one submit. But not T_Lufttemperatur. That is creating problem with the chart, which is also showing the nulls. So this is my sql:
This is the google chart:
Code:
<?php
$con=mysql_connect("localhost","root","") or die("Failed to connect with database!!!!");
mysql_select_db("nih_bw", $con);
$sth = mysql_query("
SELECT routines.date, routines.time,
SUM( IF( measurements.title = 'T_Temperatur', CAST( REPLACE( routines.value, ',', '.' ) AS DECIMAL( 18, 2 ) ), 0 ) ) AS Temperatur,
SUM( IF( measurements.title = 'T_Badende_per_Time', CAST( REPLACE( routines.value, ',', '.' ) AS DECIMAL( 18, 2 ) ), 0 ) ) AS Badende,
SUM( IF( measurements.title = 'T_Luft_Temperatur', CAST( REPLACE( routines.value, ',', '.' ) AS DECIMAL( 18, 2 ) ), 0 ) ) AS Luft
FROM routines
INNER JOIN measure_routine ON routines.id = measure_routine.routine_id
INNER JOIN measurements ON measure_routine.measure_id = measurements.id
GROUP BY routines.date, routines.time
ORDER BY routines.date, routines.time;
;");
$rows = array();
//flag is not needed
$flag = true;
$table = array();
$table['cols'] = array(
array('label' => 'routines.date' & 'routines.time', 'type' => 'datetime'),
array('label' => 'Temperatur', 'type' => 'number'),
array('label' => 'Badende', 'type' => 'number'),
array('label' => 'Luft', 'type' => 'number'),
);
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$temp = array();
// assumes dates are in the format "yyyy-MM-dd"
$dateString = $r['date'];
$dateArray = explode('-', $dateString);
$year = $dateArray[0];
$month = $dateArray[1] - 1; // subtract 1 to convert to javascript's 0-indexed months
$day = $dateArray[2];
// assumes time is in the format "hh:mm:ss"
$timeString = $r['time'];
$timeArray = explode(':', $timeString);
$hours = $timeArray[0];
$minutes = $timeArray[1];
$seconds = $timeArray[2];
$temp = array();
$temp[] = array('v' => "Date($year, $month, $day, $hours, $minutes, $seconds)");
$temp[] = array('v' => (string) $r['Temperatur']);
$temp[] = array('v' => (string) $r['Badende']);
$temp[] = array('v' => (string) $r['Luft']);
$rows[] = array('c' => $temp);
}
$table['rows'] = $rows;
$jsonTable = json_encode($table);
// echo $jsonTable;
?>
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable(<?=$jsonTable?>);
var options = {
/*width: 900, height: 900, */
title: 'Visualization',
curveType: 'function',
legend: { position: 'bottom' },
pointSize: 5,
vAxis: {title: "Values", titleTextStyle: {italic: false}},
hAxis: {title: "Time", titleTextStyle: {italic: false}},
explorer: {
actions: ['dragToZoom', 'rightClickToReset'],
axis: 'vertical'}
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>
jsontable:
{"cols":[{"label":"routines.dade","type":"datetime"},{"label":"Temperatur","type":"number"},
{"label":"Badende","type":"number"},{"label":"Luft","type":"number"}],"rows":[{"c":
[{"v":"Date(2014, 3, 02, 09, 04, 12)"},{"v":"29.23"},{"v":"55.00"},{"v":""}]},{"c":
[{"v":"Date(2014, 3, 02, 10, 04, 01)"},{"v":"23.34"},{"v":"34.00"},{"v":""}]},{"c":
[{"v":"Date(2014, 3, 02, 10, 04, 39)"},{"v":"43.54"},{"v":"39.00"},{"v":""}]},{"c":
[{"v":"Date(2014, 3, 02, 10, 04, 53)"},{"v":""},{"v":""},{"v":"23.23"}]}]}
Please feel free to ask if you need more info provided.
Try replacing the 0's in the query with nulls:
SELECT routines.date, routines.time,
SUM( IF( measurements.title = 'T_Temperatur', CAST( REPLACE( routines.value, ',', '.' ) AS DECIMAL( 18, 2 ) ), null ) ) AS Temperatur,
SUM( IF( measurements.title = 'T_Badende_per_Time', CAST( REPLACE( routines.value, ',', '.' ) AS DECIMAL( 18, 2 ) ), null ) ) AS Badende,
SUM( IF( measurements.title = 'T_Luft_Temperatur', CAST( REPLACE( routines.value, ',', '.' ) AS DECIMAL( 18, 2 ) ), null ) ) AS Luft
FROM routines
INNER JOIN measure_routine ON routines.id = measure_routine.routine_id
INNER JOIN measurements ON measure_routine.measure_id = measurements.id
GROUP BY routines.date, routines.time
ORDER BY routines.date, routines.time;
[EDIT - added PHP code to avoid casting the numbers as strings]
When inputting your data, don't cast them as strings:
$temp[] = array('v' => $r['Temperatur']);
$temp[] = array('v' => $r['Badende']);
$temp[] = array('v' => $r['Luft']);
and use the JSON_NUMERIC_CHECK option in the json_encode call:
$jsonTable = json_encode($table, JSON_NUMERIC_CHECK);

Query mysql for post and comments system

I'm using codeigniter for make a post and comments system. I did all the querys but I don't know how get the comments in the table post_comment. This is my query for take all the post shared. Thank you for the help.
function get_post_profile($user_id,$limit) {
$this->db->select('post_user.user_id,post_user.id_post_shared,post_shared.post_text,users.name,users.surname,users.id');
$this->db->join('post_shared', 'post_shared.id_post = post_user.id_post_shared');
$this->db->join('users','users.id = post_user.user_id');
$this->db->where('post_user.user_id',$user_id);
$this->db->order_by('post_user.id_post_shared','DESC');
$this->db->limit($limit);
$query = $this->db->get('post_user');
if ($query->num_rows() > 0) {
return $query->result();
} else {
return false;
}
}
users_table
id | name | surname |
1 jhon Smith
2 Sally Dunk
post_user table
id_post_shared | user_id |
1 1
post_share table
id_post | post_text
1 Hello guys
post_comment table
comment_text | id_post | id_user
Hello! 1 2
Try and reply:
$this->select('post_comment.comment_text, post_comment.id_post, post_comment.id_user')->join('post_share', 'post_share.id_post = post_comment.id_post')->join('users', 'users.id = post_comment.id_user')->get('post_comment');
EDIT
function get_comments(){
$data = array();
$posts = array();
$posts = $this->db->select('id_post as post_id, post_text', false)->order_by('id_post', 'desc')->get('post_share', 10)->result_array(); #get first 10 posts
if( is_array( $posts ) && count( $posts ) > 0 ){
foreach( $posts as $key=>$each ){
## gather the comments for the posts ###
$comments = array();
$comments = $this->db->select('comment_text, id_user')->where('id_post', $each['post_id'])->get('post_comment')->result_array();
if( is_array( $comments ) && count( $comments ) ){
$posts[$key]['comments'] = $comments;
}
}
}
return $posts;
}
EDIT 1
if( isset( $posts ) && is_array( $posts ) && count( $posts ) > 0 ){
foreach( $posts as $key=>$each ){
echo "Post id :".$each['post_id']." Post txt: ".$each['post_text']."<br>";
if( isset( $each['comments'] ) && is_array( $each['comments'] ) && count( $each['comments'] ) ){
foreach( $each['comments'] as $subKey=>$subEach ){
echo "Comment Txt :".$subEach['comment_text']."<br>";
}
}
}
}
Add LEFT JOIN to your post_comment
$this->db->select('post_user.user_id,post_user.id_post_shared,
post_shared.post_text,users.name,users.surname,users.id,post_comment.comment_text');
$this->db->join('post_shared', 'post_shared.id_post = post_user.id_post_shared');
$this->db->join('users','users.id = post_user.user_id');
$this->db->join('post_comment ','users.id = post_comment .id_user','LEFT');
$this->db->where('post_user.user_id',$user_id);
$this->db->order_by('post_user.id_post_shared','DESC');
$this->db->limit($limit);
$query = $this->db->get('post_user');
third parameter in the call is to specify the type of join join('table','relation','join type')
Try this:
$sql = "SELECT a.*,b.id,b.name,b.surname,c.id_post_shared,c.user_id,d.id_post,d.post_text
FROM post_comment a, users_table b, post_user c,post_share d
WHERE b.id = c.user_id
AND b.id = $user_id
AND c.id_post_shared = d.id_post
AND d.id_post = a.id_post";
$result = $this->db->query($sql)->result_array();
echo $result[0]['comment_text'];