I am trying to get the details of a persons based on the user input age (Integer). Now my query always returns null Array() whenever I execute the below code. I haven't specified the $postdata array. You can see I have used $postdata['ageto'] and $postdata['agefrom'] are used in calculating $agefrom and $ageto
$now = new DateTime();
//Converting _POST age to Date
$agefrom = date("Y-m-d",strtotime($now->format("Y")-$postdata['ageto'].'-'.$now->format("m").'-'.$now->format("d")));
$ageto = date("Y-m-d",strtotime($now->format("Y")-$postdata['agefrom'].'-'.$now->format("m").'-'.$now->format("d")));
$this->db->select('uacc_id, uacc_username, name, dob, city, education');
$this->db->from('user_accounts as a');
$this->db->join('personal as b','a.uacc_id = b.pruserid','INNER');
$this->db->join('profession as c','a.uacc_id = c.puserid','INNER');
$this->db->join('location as d','a.uacc_id = d.luserid','INNER');
$this->db->where('dob >= ',$agefrom);
$this->db->where('dob <= ',$ageto);
$this->db->limit(10, $offset);
$query = $this->db->get();
return $query->result();
I have suspected that my input post is not fetching the data properly. So I have replaced it with my simple query select * from... where dob >.... and it worked well. So there is no problem with _POST variables. I am not sure what I am doing wrong. Can some one help me.
I think you have to specify to which table DOB belongs like a.dob. Sorry for the post but I could not comment. Hope this helps.
The select field should add the table name
$this->db->select('a.uacc_id, a.uacc_username, a.name....); // example
and your where add table name too.
$this->db->where('b.dod >= ', $agefrom); // example
or you can output the sql command.
You can look here
Related
I have a object (Illuminate\Database\Eloquent\Builder) that have a (Illuminate\Database\Query\Builder) inside it. I dump the object and can see this:
The image bellow, is a dd($querybuilder) from the object
My plan is to get the query and extract the selected columns but I already tried ($querybuilder->query) and ($querybuilder['query']) and I received errors.
How can I get the "columns" value?
Solved. Thank you
Tim Lewis
$query->getQuery()->columns worked!
Im posting where what im tying to do. select and addSelect are not doing what I wanted if no colunms are selected.
public function scopeAddStatusColumn($query)
{
$previous_columns = $query->getQuery()->columns;
$status_column = DB::raw('(begin <= NOW() AND end >= NOW()) OR (begin <= NOW() AND end IS NULL) as status');
$new_columns = $previous_columns == null ? ['*', $status_column] : [$previous_columns] + [$status_column];
$query = $query->select($new_columns);
return $query;
}
If you want to get all the columns of the table:
$query = Products::where('product_name', 'pencil')->get();
If you want to get a spesific column:
$query = Products::where('product_name', 'pencil')->get(['product_id']); // this is an array, you can get any column name here
If you want to get value of a single column:
$query = Products::where('product_name', 'pencil')->first(['description'])->description;
I have a table call viewed books like below in database
book_id user_uniq_id date
3 111111 2018-11-06 00:29:15
like that so many rows I need to get book_id and user_uniq_id according to date
as a example this date these users viewed these books like that
How can I write a sql query for this in codeigniter
I'm not sure if your question is clear enough, but from what i got you want a query based on the timestamp then you should try this.
$timestamp = '2018-11-06 00:29:15';
$this->db->select('book_id, user_uniq_id');
$query = $this->db->get_where('table', array('date' => '>' . $timestamp));
$sql = "SELECT book_id, user_uniq_id from viewed_books ORDER BY date";
$query = $this->db->query($sql);
$bookData= $query->result_array();
You can see your results from
var_dump($bookData);
Hope this is the answer you seek
So basically I have a TIMESTAMPDIFF query in my model to determine the duration and I want it to display the duration in a view. The problem is, it will display the error.
Please help me, thank you.
Here's my query of my model (model_attendance.php):
public function getOne($username){
$sql = "SELECT empnum FROM employees WHERE username='$username'";
$result = $this->db->query($sql);
$sql1 = "SELECT a.empnum,CONCAT(a.name,' ',a.midname,' ',a.lastname) AS
NAME,CONCAT(b.indate,' ',b.intime) AS 'TIMEIN',CONCAT(b.outdate,'
',b.outtime)AS 'TIMEOUT', TIMESTAMPDIFF(HOUR, 'TIMEIN','TIMEOUT') AS 'DUR'
FROM employees AS a
JOIN times AS b ON (a.empnum=b.userid)
WHERE b.indate BETWEEN
DATE(CONCAT(YEAR(CURRENT_TIMESTAMP),'-',MONTH(CURRENT_TIMESTAMP),'-','01'))
AND DATE(LAST_DAY(CURRENT_TIMESTAMP)) AND b.userid='".$result->row(0)->empnum."'";
$query = $this->db->query($sql1);
return $query->result();
}
To solve this problem, you need to Pass return type as "false" to Active record select query.
$this->db->select("TIMESTAMPDIFF(YEAR,a.date_birth, CURDATE()) AS age", false);
Assign that to variable and pass that to your SQL Query.
$now = date('Y-m-d H:i:s'); # 2015-1-30 04:32:25 / With time
$now = date('Y-m-d'); # 2015-1-30 / Without time
Its potentially a bug. I Havent checked the Query Builder class yet. But to fix this...you can try by adding ` to the column like this
$this->db->select("TIMESTAMPDIFF(YEAR, `a.date_birth`, CURDATE()) AS age");
or
$this->db->query("TIMESTAMPDIFF(YEAR, `a.date_birth`, CURDATE()) AS age");
I know my question is similar to other question already answered but my issue is different because I need some alternative or advice no how to go the other way around.
My issue is: I want to get values between either two dates or one according to what user wants..
When User request data of one day.. php query data successful.. but problem is when data requested is between two dates..
$query = $this->db->query("SELECT * FROM `meta_receipt_data`
WHERE `meta_transaction_date` >= '$first_date' AND
`meta_transaction_date` <= '$second_date' ");
return $query->result();
I get an empty set...
So I thought may be the values are not submitted correct.. so I echoed the values to see if they are correct or not. I find they are correct...
$first_date = 09/13/2014;
$second_date = 09/19/2014;
But if I try to put the value like
$query = $this->db->query("SELECT * FROM `meta_receipt_data`
WHERE `meta_transaction_date` >= '09/13/2014' AND
`meta_transaction_date` <= '09/19/2014' ");
return $query->result();
I get my result back correct.. so is there anything am doing it wrong??
Change the type of meta_transaction_date to DATE as that is what it is! Also use the standard 'yyyy-mm-dd' when passing in DATEs.
Your problem probably stems from string ordering of the 'mm/dd/yyyy' US date format which is horrible for coding. If you wish to display the DATE in this format, convert it when SELECTing the final output.
MySQL has a built in function called Between that you can use like this:
SELECT * FROM table_name WHERE date_column BETWEEN 'start_date_parameter' AND 'end_time_parameter'
Try to cast the date first , and then with between statement:
$query = $this->db->query("SELECT * FROM `meta_receipt_data`
WHERE `meta_transaction_date` BETWEEN
date_format(str_to_date('$first_date', '%d/%m/%Y'), '%Y-%m-%d') AND
date_format(str_to_date('$second_date', '%d/%m/%Y'), '%Y-%m-%d')");
$query = $this->db->query("SELECT * FROM `meta_receipt_data`
WHERE `meta_transaction_date` >= '09/13/2014'
AND `meta_transaction_date` <= '09/19/2014' ");
Since the above seems to be working fine, the problem is in your code.
$query = $this->db->query("SELECT `meta_transaction_date` FROM meta_receipt_data WHERE
meta_transaction_date BETWEEN "
.date( "Y-M-d", ( strtotime($first_date) ) )." AND "
.date( "Y-M-d", ( strtotime($second_date) ) ) );
A word of advice, do not use queries like SELECT * as they will degrade performance of your application. And I just read in your comment to your own question:
I have set the type as Varchar
Do not do that. It is best to use the DATE type to store dates. You can use the query:
ALTER TABLE `meta_receipt_data`
MODIFY `meta_transaction_date` DATE NOT NULL;`
Well, that is assuming you wish to keep the column to not accept null values.
I found that the value had space before and after so I use $first = trim($first_date); and problem solved...
I have a PHP script which users submit a post and saves in to the database; I'm storing the date it was posted using time(). I'm trying to figure out a way to filter my data by day. I have a while loop which is returning all the posts that user made which is ordered by date, however what I'm looking to do is have headings of a date for e.g. 10 December 2011 and only have posts that was submitted on that day. I want it only to display the heading if there is data for that day.
Thanks a lot.
EDIT:
Here is my while loop at the moment:
$get_posts = mysql_query( "SELECT * FROM `posts` WHERE `userid` = '$name[id]' ORDER BY `date` DESC" );
while( $userpost = mysql_fetch_assoc( $get_posts ) )
{
echo $userpost[ 'post' ];
}
But somehow before the loop I need to display the date heading the post or posts was submitted. I'm thinking it would need to be a loop outside of the current loop but I have no idea how to go about it.
As you have mentioned in one of your comments to the question that the DB column is of type integer. And so I assume you are storing the UNIX timestamp in the column. From what I understand, you are looking to display something like:
DATE HEADING 1
- data
- data
DATE HEADING 2
- data
- data
And if that is correct, this code snippet might help:
$rs = mysql_query('SELECT `column1`, `column2`, FROM_UNIXTIME(`the_time_column`, \'%d %M %Y\') AS `date_value`
FROM `your_table`
ORDER BY `the_time_column`');
$old = '';
while ($row = mysql_fetch_assoc($rs)) {
$new = $row['date_value'];
if ($new != $old) { // marks the beginning of a new heading
echo 'heading = ' . $new;
}
// start displaying data under the heading
echo $row['column1'];
echo $row['column2'];
// finish displaying data
$old = $new;
}
Please note that I've used dummy column and table names, so please replace them with the actuals. Also, the above snippet will display heading in ascending order of date. To show in reverse, add DESC at the end of the query.
Hope the above helps!
You get that right from the database, i.e.:
SELECT * FROM YourTable
WHERE
DAY(yourDateField)='15' AND
MONTH(yourDateField)='10' AND
YEAR(yourDateField)='2011'
;
or better yet, directly, like this:
SELECT * FROM YourTable
WHERE yourDateFieldld=2011-12-10;
Depends how you store the data in your DB.
In any case, this should be done in the database, not PHP
You could use BETWEEN.
Have a look at The MySQL Reference