php mysql order by timestamp is incorrect - php

I have database records such as below
Name Date(timestamp) Time(timestamp)
I want to order them by time DESC. But it shows me incorrect order. It shows now:
Name 19:00
Other 18:30
One more 19:00
As you can see hours are not going from earliest to latest. I want it to be like this:
Other 18:30
Name 19:00
One more 19:00
What am i doing wrong?
SELECT * FROM table WHERE date='".$date."' ORDER BY book_time DESC

Try to use Timestamp() for specifying that you expect the fields you wanna extract data from as timestamps.
something like this:
SELECT timestamp( `timestamp` ) as 'timestamp'
FROM randomTable
ORDER BY 1 ASC;
it would be great if you can post screenshots of your output to make the issue clearer.

Are you sure that select only for the date that you want? Because I think that select multiple date not only one.
Also for what you want you need to use ASC instead of DESC.
Because I have try and it's work for me. This is my simple code :
$sql = "SELECT * FROM test WHERE date = '2016-02-10' ORDER BY time ASC";
$res = $DB->query($sql);
while($enr = $res->fetch()){
var_dump($enr);
}
With this DB :
And the result is :
array (size=8)
'id' => string '4' (length=1)
0 => string '4' (length=1)
'date' => string '2016-02-10' (length=10)
1 => string '2016-02-10' (length=10)
'time' => string '01:00:07' (length=8)
2 => string '01:00:07' (length=8)
array (size=8)
'id' => string '3' (length=1)
0 => string '3' (length=1)
'date' => string '2016-02-10' (length=10)
1 => string '2016-02-10' (length=10)
'time' => string '02:03:05' (length=8)
2 => string '02:03:05' (length=8)
The result is order in the ASC order like you want
Hope that helps you.

Related

Need Help in Writing a Query

I have a table wich keep phone call records. I need to get answered and not answered calls count for each hour ( example 8:00 to 9:00 ) in specificed date range ( example 1 march to 5 march ), i dont need to separate the date, just need to separate hours.
Already i used this query:
SELECT `event`, DATE(`time`) AS `date`, HOUR(`time`) AS `hour`, COUNT(*) AS `count` FROM `queue_log` WHERE `queuename` = 'Hozoori_Q' AND `event` IN ('CONNECT', 'RINGNOANSWER') AND `time` >= '2014-03-01' AND `time` <= '2014-03-05' GROUP BY `event`, `date`, `hour`;
the "event" field is call status.
this query output is something like this:
1 =>
array (size=4)
'event' => string 'CONNECT' (length=7)
'date' => string '2014-03-01' (length=10)
'hour' => string '9' (length=1)
'count' => string '99' (length=2)
2 =>
array (size=4)
'event' => string 'RINGNOANSWER' (length=7)
'date' => string '2014-03-01' (length=10)
'hour' => string '9' (length=1)
'count' => string '5' (length=2)
but i want to be like this:
1 =>
array (size=4)
'date' => string '2014-03-01' (length=10)
'hour' => string '9' (length=1)
'answered_count' => string '99' (length=2)
'not_answered_count' => string '5' (length=2)
is there any solution ?
Straight SQL answer:
SELECT DATE(`time`) AS `date`,
HOUR(`time`) AS `hour`,
SUM(`event` = 'CONNECT') AS `answered_count`,
SUM(`event` = 'RINGNOANSWER') AS `not_answered_count`
FROM `queue_log`
WHERE `queuename` = 'Hozoori_Q'
AND `event` IN ('CONNECT', 'RINGNOANSWER')
AND `time` >= '2014-03-01'
AND `time` <= '2014-03-05'
GROUP BY `date`, `hour`;

Var_dump shows value but returns null mysql query

Using PHP and MYSQL I do a left join on 2 tables. I then use var_dump to show the results.
$sql = "SELECT pro_table.pro_id, pro_table.sport_id, pro_table.pro_name,
results_table.pro_id, results_table.year, results_table.result_rank, results_table.result_score
FROM pro_table
LEFT JOIN results_table
ON pro_table.pro_id=results_table.pro_id
WHERE sport_id LIKE '$dropdownrecord'";
$myData = mysql_query($sql,$con);
while($record = mysql_fetch_array($myData)){
var_dump($record);
this outputs
array (size=13)
0 => string '276' (length=3)
'pro_id' => null
1 => string '14' (length=2)
'sport_id' => string '14' (length=2)
2 => string 'Bradley Wiggins' (length=15)
'pro_name' => string 'Bradley Wiggins' (length=15)
3 => null
4 => null
'year' => null
5 => null
'result_rank' => null
6 => null
'result_score' => null
I am not sure why my pro_id returns a 276 yet also says null?
My Problem was that I had both columns in both tables named the same think. I renamed one column and now it is working property.
Try something like this :
for($i=0;$record[$i] = $mysql_fetch_array($myData); $i++){
var_dump($record[$i]);
}

Retrieving specific data from sql query

Let's say I have the following query, wich gives me the amount of coupons downloaded and redeemed, grouped by date: [EDIT](I can't modify this query)[/EDIT]
$sql = "SELECT DATE(datetime) ,
SUM(CASE WHEN datetime!='' THEN 1 ELSE 0 END) As downloaded ,
SUM(CASE WHEN used = 1 AND DATE(datetime) != '' THEN 1 ELSE 0 END) AS redeemed
FROM Promotion_Redeem
WHERE datetime BETWEEN '2013-10-01' AND '2013-10-04'
GROUP BY DATE(datetime)";
How can I get the sum of downloaded and the sum of redeemed? this should be within $sql, somewhere... below is the result for this query:
row number 1
array (size=3)
0 => string '2013-10-01' (length=10)
1 => string '126' (length=3)
2 => string '11' (length=2)
row number 2
array (size=3)
0 => string '2013-10-02' (length=10)
1 => string '106' (length=3)
2 => string '5' (length=1)
row number 3
array (size=3)
0 => string '2013-10-03' (length=10)
1 => string '228' (length=3)
2 => string '12' (length=2)
row number 4
array (size=3)
0 => string '2013-10-04' (length=10)
1 => string '149' (length=3)
2 => string '9' (length=1)
[EDIT]bove you can see I get 4 rows, each one whith an array... I want, for instance, the sum of the third field from each of these arrays... In my example, this would equals to 37 (that means, 37 coupons redeemed)[/EDIT]
I got this data structure after using this:
while ($row = mysql_fetch_row($result)) {
echo "row number ".++$i;
var_dump($row);
}
Thanks in advance
Modify your php loop like so:
$downloaded=0;
$redeemed=0;
while ($row = mysql_fetch_row($result)) {
echo "row number ".++$i;
$downloaded+=$row[1];
$redeemed+=$row[2];
var_dump($row);
}
echo "Downloaded: $downloaded<br>";
echo "Redeemed: $redeemed<br>";

Count() returns different number of entries than var_dump()

Hey guys I'm tired and can't figure this one out so any help would be appreciated.
I have an array called $Row that I get from database table.
When I run var_dump($Row); I get the following:
array
0 => string '1' (length=1)
'id' => string '1' (length=1)
1 => string 'erik' (length=4)
'username' => string 'erik' (length=4)
2 => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
'password' => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
3 => string '' (length=0)
'email' => string '' (length=0)
4 => string '0' (length=1)
'date_join' => string '0' (length=1)
5 => string '0' (length=1)
'date_mod' => string '0' (length=1)
6 => string '1' (length=1)
'active' => string '1' (length=1)
7 => string '1' (length=1)
'admin' => string '1' (length=1)
8 => string '0' (length=1)
'deleted' => string '0' (length=1)
When I run echo count($Row); I get value 18.
Count and var_dump are right next to each other, there is no modification of $Row.
Question: Why does Count() return 18 entried when there are only 8 of them inside $Row shown by var_dump()? I guess I just don't understand count()... I checked http://php.net/manual/en/function.count.php, but still don't get it...
Edit: I understand whats wrong know, thanks everyone. Another question. How can I remove the ones that are in a string, for instance I want this kind of a table:
array
0 => string '1' (length=1)
1 => string 'erik' (length=4)
2 => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
3 => string '' (length=0)
4 => string '0' (length=1)
5 => string '0' (length=1)
6 => string '1' (length=1)
7 => string '1' (length=1)
8 => string '0' (length=1)
* I'm using mysql_fetch_array() to retrieve the data and put it into a table.
You are having mixed array integer and associative and having 18 elements. This is I think returned from mysql_fetch_array which by default return associative and numeric indexed array.
Because your strings are also values in $Row they just seem to be indented because of the string char.
That means
'id' => string '1' (length=1)
'username' => string 'erik' (length=4)
'password' => string 'd95eb19a15301089985ad6fd6ecbf2d7' (length=32)
// ...
are also in your array
becuase you're getting back a mix of associative and numeric result set from you db ( you can precise which one you really want with FETCH_ASSOC or alike as a parameter to you dbvendor_query() function ).
There is very well 18 elements in your array.
I think that applying count to this:
0 => string '1' (length=1)
'id' => string '1' (length=1)
returns 2 instead of 1!
0 and id are two different items of the array!
This taken for the others 8 elements make the count return 18
Actually, there are indeed 18 elements in your array:
indices 0 through 8 (9 elements in total)
id, username, ... another 9 elements
9 + 9 = 18
If you only want the numeric indices, and supposing you're using MySQL:
$Row = mysql_fetch_array($result, MYSQL_NUM)
Source: http://php.net/manual/en/function.mysql-fetch-array.php

Doctrine_Pager returns wrong results with multiple groupBy columns

I have Doctrine query setup with mySQL as database:
$q = Doctrine_Query::create();
$q->select('make.id, make.make,
model.id, model.year, model.model')
->from('Make make')
->innerJoin('make.Models model');
$q->groupBy('make.id, model.id'); // <-- this is where the problem
$q->orderBy('make ASC');
$q->setHydrationMode(Doctrine_Core::HYDRATE_SCALAR);
$pager = new Doctrine_Pager($q, 1, 1); // just want one result to prove the concept
$items = $pager->execute();
This executes this three queries (from mysql log):
SELECT COUNT(*) AS num_results FROM (SELECT m.id FROM make m INNER JOIN model m2 ON m.id = m2.make_id GROUP BY m.id, m2.id) dctrn_count_query
SELECT DISTINCT m3.id FROM make m3 INNER JOIN model m4 ON m3.id = m4.make_id GROUP BY m3.id, m4.id ORDER BY m3.make ASC LIMIT 1
SELECT m.id AS m__id, m.make AS m__make, m2.id AS m2__id, m2.year AS m2__year, m2.model AS m2__model FROM make m INNER JOIN model m2 ON m.id = m2.make_id WHERE m.id IN ('33') GROUP BY m.id, m2.id ORDER BY m.make ASC
And the result set is
array
0 =>
array
'make_id' => string '33' (length=2)
'make_make' => string 'Alfa-romeo' (length=10)
'model_id' => string '288' (length=3)
'model_year' => string '2010' (length=4)
'model_model' => string '159' (length=3)
1 =>
array
'make_id' => string '33' (length=2)
'make_make' => string 'Alfa-romeo' (length=10)
'model_id' => string '289' (length=3)
'model_year' => string '2010' (length=4)
'model_model' => string 'MiTo' (length=4)
2 =>
array
'make_id' => string '33' (length=2)
'make_make' => string 'Alfa-romeo' (length=10)
'model_id' => string '290' (length=3)
'model_year' => string '2010' (length=4)
'model_model' => string '159 SPORTWAGON' (length=14)
The problem is with second query that returns make.id which is used in third query to select 1 make (Alfa-Romeo which has 3 models). What I want is to return NUMBER OF make/model combinations.
If I change number of items returned in Doctrine_Pager to 2, I get 33 rows (because from two selected makes Alfa-Romeo has 3 models and Audi (which is next) has 30 models.
Where I'm making a mistake?
You can not combine multiple columns in a single group by. Try this...
$q->groupBy('make.id');
$q->addGroupBy('model.id');
or a little more compressed...
$q->groupBy('make.id')->addGroupBy('model.id');
$table->setAttribute(Doctrine_Core::ATTR_QUERY_LIMIT, Doctrine_Core::LIMIT_ROWS);
http://www.doctrine-project.org/projects/orm/1.2/docs/manual/dql-doctrine-query-language/en

Categories