datediff causing error in php - php

I have sql statement that works great in mysql, where I'm am using datediff. When I try to use it in php, I get an "mysql_fetch_arrary() expects parameter 1 to be a resource, boolean given in"
This the statement is...
$result = mysql_query("select hname, hsn, hmodel, hmake, htype, hwar, datediff(`hwar`, now()) from host where stype='physical';",$db);
I know the statement works in mysql
mysql> select hname, hsn, hmodel, hmake, htype, hwar, datediff(`hwar`, now()) from servers.host where stype='physical';
+--------------+---------+--------+-----------+-------+------------+-------------------------+
| hname | hsn | hmodel | hmake | htype | hwar | datediff(`hwar`, now()) |
+--------------+---------+--------+-----------+-------+------------+-------------------------+
| moscow | XXXXXXX | Dell | PowerEdge | R710 | 2013-09-13 | 225 |
| sydney | XXXXXXX | Dell | PowerEdge | R710 | 2013-09-15 | 227 |
When I remove datediff(hwar, now()), my page works. I wanted to use that as field
$datediff=$row['datediff'];
Any clues as to why it doesn't work?

try to make limit in your query
$result = mysql_query("select hname, hsn, hmodel, hmake, htype, hwar, datediff(`hwar`, now()) as dif from host where stype='physical' LIMIT 0,10 ;",$db);
look this post

Try with this:
Replace this piece of sql statement: datediff(hwar, now())
With: datediff(hwar, now()) as Diffdate
And recover it this way:
$datediff=$row['Diffdate'];
Bye XD

You should give the selector an alias:
select hname, hsn, hmodel, hmake, htype, hwar, datediff(`hwar`, now()) AS diff from host where stype='physical';
Then you could access the column with the name 'diff'

Related

How to count record month group with unix timestamp in php & mysql.

Example my_table
ID | Name | Date
--------------------------
12 | John | 123456789
13 | Mike | 987654321
...
29 | Rick | 123498765
30 | Adam | 987651234
show output result like this
Month | Count
--------------------------
3 | 5 |
6 | 8 |
How can I do this with PHP?
You can do this using MySQL Query as below.
SELECT MONTH(FROM_UNIXTIME(`Date`)) `Month`
,COUNT(ID)
FROM my_table
GROUP BY `Month`;
Since post tagged codeigniter
This is codeigniter way:
$query = $this->db->select("month(from_unixtime(`Date`)) as `month`, count(1) as `count`",FALSE)
->group_by("month");
->get("your_table");

PHP SQL AJAX get data from before certain datetime

I am comparing two datetime in the SQL statement. This is done in PHP.
$result=mysqli_query($con,"SELECT
*
FROM
join
WHERE
join_date <= '$last_join_dates'
ORDER BY
join_date DESC LIMIT 3 OFFSET 3");
$last_join_dates gets update every time, so that I use offset to keep pulling in new data. However, comparing the date is not actually working.
Inside database: structure join_date looks like 2015-10-24 13:30:22 and it is a datetime type.
$last_join_datesI use is the exact same format 2015-10-26 08:23:22.
Since that doesn't work.. I tried the following to convert them.
TRIED FORMAT() the datetime, but does not work:
$result=mysqli_query($con,"SELECT
*
FROM
join
WHERE
FORMAT(join_date, 'yyyyMMddhhmmss') <= FORMAT($last_join_dates, 'yyyyMMddhhmmss')
ORDER BY
join_date DESC LIMIT 3 OFFSET 3");
TRIED Convert() the datetime, but doesn't seem to work either:
$result=mysqli_query($con,"SELECT
*
FROM
join
WHERE
REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(19), CONVERT(DATETIME, join_date, 112), 126), '-', ''), 'T', ''), ':', '') <= REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(19), CONVERT(DATETIME, $last_join_dates, 112), 126), '-', ''), 'T', ''), ':', '')
ORDER BY
join_date DESC LIMIT 3 OFFSET 3");
I'm not sure how to debug this.. Any suggestions?
well #1, JOIN is a mysql keyword so you need to either put back-ticks around it or better yet call your table something else
#2, this wouldn't be the reason it isn't working, but you should never use string interpolation like that, it is vulnerable to sql injection. Instead use $last_join_date as a bind variable: Bind variables in a mysql_query statement
The SQL you have above is correct, as seen below:
mysql> select * from test order by the_date;
+----+---------------------+
| id | the_date |
+----+---------------------+
| 1 | 2015-01-02 11:23:21 |
| 2 | 2015-01-03 12:23:21 |
| 3 | 2015-02-03 12:25:21 |
| 4 | 2015-02-05 12:25:21 |
| 5 | 2015-02-05 15:25:21 |
| 6 | 2015-02-05 15:37:21 |
| 7 | 2015-02-05 20:37:21 |
| 8 | 2015-02-05 20:37:40 |
| 9 | 2015-03-05 20:37:40 |
| 10 | 2015-03-06 20:37:40 |
+----+---------------------+
10 rows in set (0.00 sec)
mysql> select * from test where the_date <= '2015-02-10 01:12:24' order by the_date desc limit 3, 3;
+----+---------------------+
| id | the_date |
+----+---------------------+
| 5 | 2015-02-05 15:25:21 |
| 4 | 2015-02-05 12:25:21 |
| 3 | 2015-02-03 12:25:21 |
+----+---------------------+
3 rows in set (0.00 sec)
So try naming the table something else or use backticks "`". Also try running the query from the mysql command line. Maybe the data you have isn't what you thought but the query is working fine.
Try this one:
"SELECT * FROM join WHERE join_date <= '".date("Y-m-d H:i:s", strtotime($last_join_dates))."'ORDER BY join_date DESC LIMIT 3 OFFSET 3"
It converts $last_join_dates to a timestamp using strtotime, then the date function will convert it to the proper format.
Try this:
$result=mysqli_query($con,"SELECT
*
FROM
join
WHERE
UNIX_TIMESTAMP(join_date) <= '" . strtotime($last_join_dates) . "'
ORDER BY
join_date DESC LIMIT 3 OFFSET 3");

How to get the latest data of a specific day? [MYSQL]

How can I get the latest data of a specific day in MySQL?
Let's assume that I have a column of dates recorded on my database
dates | time | value
---------------------------------------------
2015-08-05 | 11:03:02 | 200
2015-08-05 | 23:04:22 | 2400
2015-08-07 | 8:00:22 | 500
2015-08-08 | 13:00:11 | 400
2015-08-08 | 13:23:11 | 200
2015-08-09 | 17:00:23 | 2200
2015-08-09 | 17:06:00 | 1290
2015-08-09 | 19:22:00 | 900
2015-08-13 | 01:01:22 | 1010
I want to get the latest data or transaction of a specific date, my desired result would be like this
dates | time | value
---------------------------------------------
2015-08-05 | 23:04:22 | 2400
2015-08-07 | 8:00:22 | 500
2015-08-08 | 13:23:11 | 200
2015-08-09 | 19:22:00 | 900
2015-08-13 | 01:01:22 | 1010
Only the latest data of a spefic or distinct date is chosen, what is the possible query with this?
You need to do this way to get that for each dates
select t1.dates,t1.time,t1.value from table as t1 inner join
(
select dates,max(time) as time from table group by dates
) as t2 on t1.dates=t2.dates and t1.time=t2.time
Use
SELECT * FROM TABLE_NAME GROUP BY dates ORDER BY time desc LIMIT 1;
Or
SELECT * FROM TABLE_NAME GROUP BY dates HAVING time <= '23:59:59' LIMIT 1;
Try this:
SELECT `dates`, MAX(`time`) AS `time`, MAX(`value`) AS `value`
FROM `tbl_name`
GROUP BY `dates`
ORDER BY `dates` ASC
Try this query:
SELECT * FROM `table` GROUP BY dates ORDER BY time DESC;

MySql AVG() and WHERE

I'm struggling on how to write this query and cant quite find an answer to help me with my case.
Consider the following table:
-----------------------------------------------
| ID | Value1 | Value2 | Value3 | Date |
-----------------------------------------------
| 1 | 10 | 23 | 30 | 2015-01-01 |
-----------------------------------------------
| 1 | 11 | 33 | 40 | 2015-02-01 |
-----------------------------------------------
| 2 | 26 | 93 | 20 | 2015-01-01 |
-----------------------------------------------
| 2 | 11 | 33 | 50 | 2015-02-01 |
-----------------------------------------------
I want to retrieve the average value of Value1 where the Date is 2015-01-01
I thought that
SELECT AVG(PAM_1) FROM MyTable WHERE DATE = 2015-01-01
would work but of course it does not. I'm aware that I probably need to use HAVING but I'm being confused if I must also use GROUP BY and if do I need the AS (something) part.
EDIT
The problem was not related to the query. I was supplying the date trough a variable as such:
$sth = $db->prepare("SELECT AVG(Value1) FROM MyTable WHERE DATE = $date");
Which is not possible to do with prepared statements.
Your query is basically fine. Your date constant is not. Dates constants should be enclosed in single quotes:
SELECT AVG(PAM_1)
FROM MyTable
WHERE DATE = '2015-01-01';
If the date could have a time component, then the following is the best way to handle this:
SELECT AVG(PAM_1)
FROM MyTable
WHERE DATE >= '2015-01-01' AND DATE < '2015-01-02';

select latest element from mysql table

I need to write a query to retrieve values from two columns using mysql table
My table has the following strucutre
| ID | user_id | datetime | message |
| 1 | 21 | 2012-05-10 04:13:01 | message1 |
| 2 | 07 | 2012-05-10 04:17:51 | message2 |
| 3 | 21 | 2012-05-11 04:21:51 | message3 |
| 4 | 21 | 2012-05-11 04:43:51 | message4 |
| 5 | 07 | 2012-05-11 04:21:51 | message5 |
| 5 | 21 | 2012-05-11 04:43:51 | message6 |
i wrote the below query
$query="SELECT MAX(datetime) FROM messages where user_id=21 and date=2012-05-11";
but i am not getting latest record from table iam getting null value
help us
$query="SELECT MAX(datetime) FROM messages where user_id=21 and date LIKE '2012-05-11%'";
You should use DATE(date) to get date of timestamp. MySQL function DATE() extracts only date without hours, minutes and seconds.
Here is your query:
SELECT MAX(datetime)
FROM messages
WHERE user_id = 21 AND DATE(date) = '2012-05-11'
Have you tried the following?
$query="SELECT MAX(datetime) FROM messages where user_id=21";
Update:
In your question, you didn't specify if you wanted to retrieve last record for a certain date. If you do, you'd need to use MySQL's date function.
Are you looking for the most recent record? You can get this with a subquery:
$query = "SELECT * FROM messages WHERE datetime = (SELECT MAX(datetime) FROM messages)";
Your query asks for ".....date=2012-05-11";" but your table does not have the field named date? did you mean datetime? if so, you may want to try ".....datetime like '2012-05-11%'";

Categories