Alright so right now i am generating a report from submit button and it has two input type that are from and to but the thing is i want from and too date and the result isnt showing up from the database
$order_time=$_POST["datefrom"];
$order_time=$_POST["dateto"];
$query = "SELECT * FROM ss_orders where order_time='".$order_time."' limit 60";
Thats my above code , so is it possible to use between in that above query ? and also my data type in the database of order_time is datetime ? so why i am not getting any result ?
Thanks in advance :) Help will be appreciated :)
$order_time=$_POST["datefrom"];
$s= date("Y-m-d", strtotime($order_time));
$order_timeto=$_POST["dateto"];
$e= date("Y-m-d", strtotime($order_timeto));
$query = "SELECT * FROM ss_orders where datetime<=$s and datetime>=$e";
as a programmer you should check and debug your code in all possible ways,you can print what are the values these variables having and you cal also print the query so you can know what is the actual query executing.
$fromdate=$_POST["datefrom"];
$todate=$_POST["dateto"];
$query = "SELECT * FROM ss_orders where datetime<='$todate' and datetime>='$fromdate' limit 60";
and PDO/MYsqli for security
Related
I'm trying to ordonate some things in oder by date, but the query that should select the articles doesn't work. I have an issue with where and order by.
What i've tried:
$query = mysql_query("select * from my_article where id = '<?php echo $_SESSION['idArticle]';?>' order by date") ;
How can i fix this?
A Suggestion:-
Regarding mysql_*:-
Deprecated from php5.5 onward
Removed from php7.0.
So use mysqli_* or PDO(with prepared statements).
Current solution:-
<?php
$id = $_SESSION['idArticle']; // here `'` is missed in your question code
$query = mysql_query("select * from my_article where id = $id order by `date`") ;
$query = mysql_query("select * from my_article where id='".$_SESSION['idArticle']."' order by date") ;
You can try this. You need to think sql injection ;)
Make sure data type of date field is not varchar.
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 have a table in an Oracle database called reservation that has a datetime column called date_reservation. And I have the following code:
$sql = "SELECT COUNT(*) AS numberrows FROM reservation WHERE TRUNC(date_reservation)=TO_DATE('15/06/2015', 'dd/mm/yyyy')";
$stid = $this->bd->execute($sql);
$row = $this->bd->get_row($stid, 0);
echo $row['NUMBERROWS'];
The table content is:
ID ... DATE_RESERVATION
1 13/06/2015 12:00:00
2 ... 15/06/2015 09:00:00
3 ... 15/06/2015 11:00:00
When I execute my PHP code I get 0, and I should get 2.
But if I execute this sql sentence in my DB admin tool I get 2.
If I remove the WHERE CLAUSE from my code ($sql = SELECT COUNT(*) AS numberrows FROM reservation) I get the number of rows in the reservation table.
So I guess something is wrong with TRUNC in PHP. How could I solve it?
It seems you miss a semicolumn at the line 1 :
$sql = SELECT COUNT(*) AS numberrows FROM reservation WHERE TRUNC(date)=TO_DATE('15/06/2015', 'dd/mm/yyyy');
$stid = $this->bd->execute($sql);
$row = $this->bd->get_row($stid, 0);
echo $row['NUMBERROWS'];
Maybe it came from that? :)
You have colons missing in first line:
$sql = "SELECT COUNT(*) AS numberrows FROM reservation WHERE TRUNC(date)=TO_DATE('15/06/2015', 'dd/mm/yyyy')";
$stid = $this->bd->execute($sql);
$row = $this->bd->get_row($stid, 0);
echo $row['NUMBERROWS'];
Try this now. If you still did not get accurate results than check your date format that you are using in query.
The first thing you must pay attention,what comes from trunc(date). Check the data in your date column, the format of date type must be exactly right with the format of to_date('15/06/2015','dd/mm/yyyy')
I am having issue were data does not want to show up because I can't figure the format for
2014-12-12 07:31:11.827 not sure how to write it anyone have any ideas?
require_once("../../db_connect.php");
//prepared statement with PDO to query the database
$stmt = $db->prepare("SELECT * FROM requests WHERE status='Received'
AND priority='High'AND date_requested=CAST(GETDATE() AS Date) ORDER BY id DESC");
$stmt->execute();
There are lots of ways around this. Try replacing this:
date_requested=CAST(GETDATE() AS Date)
with this:
DATEDIFF(dd,date_requested,GETDATE()) = 0
i got a fairly simple layout going and for the life of me i cant figure out why this returns nothing:
<?php
// Gets A List Of Comic Arcs
$result = mysql_query("SELECT * FROM ".$db_tbl_comics." GROUP BY ".$db_fld_comics_arc." ORDER BY ".$db_fld_comics_date." DESC LIMIT 20");
while ($comic = mysql_fetch_array($result)) {
// Now Go Back And Count Issues For Each Comic Arc Above
$result22 = mysql_query("SELECT * FROM ".$db_tbl_comics." WHERE ".$db_fld_comics_arc."=".$comic[$db_fld_comics_arc]);
$total_issues = mysql_num_rows($result22);
echo $total_issues;
}
?>
No other query is refered to as $result22.
$comic[] has already been defined in the previous query.
echo mysql_error($result22); returns no errors.
Let me know if you need any other info.
I am assuming that the column $db_fld_comics_arc is a string.
Change:
$result22 = mysql_query("SELECT * FROM ".$db_tbl_comics." WHERE ".$db_fld_comics_arc."=".$comic[$db_fld_comics_arc]);
To:
$result22 = mysql_query("SELECT * FROM ".$db_tbl_comics." WHERE ".$db_fld_comics_arc."='".$comic[$db_fld_comics_arc]."'");
Am I wrong? If so, let me know the table structure, and what your error reporting is set to.
Also, could you let us know the purpose of your SQL? It may also be possible to put the data together in one query, instead of looping sql queries through, and using data from a first query.
Maybe it is because $db_fld_comics_arc is in $comic[$db_fld_comics_arc]
if both are the same then you should try replacing $db_fld_camics_arc with $comic[$db_fld_comics_arc].