do something if (mysql) - php

i have made this:
$case=mysql_query("select * from project where url='$url'");
while($result=mysql_fetch_array($case))
{
$one = $result['date'];
$two = $result['time'];
}
$one contain the date of my article and $two contain the time of my article.
I need to do:
if $uno and $two is > of all row in my database called project do something.
Is possibile?
Update:
mysql information:
Date is "DATE" type and Time is "VARCHAR" type.
output example: 2016-02-10 - 12:22:18

Use this SQL instead:
select `date`, `time`
from project
where url='$url'
order by 1 desc, 2 desc;
Then the first record you get will also represent the most recent date/time combination.
If you are not interested in the other records, then add limit 1 to the SQL, and you will then only get one result:
select `date`, `time`
from project
where url='$url'
order by 1 desc, 2 desc
limit 1;

Related

TOP 10 article visitor counting via PDO PHP

I wanted to know if it is possible to obtain the 10 most viewed articles that week (Between today and 7 days back) using PDO PHP.
The main problem is that on two separate tables. Primary table is the table of articles. And the second table is a table visitors by IP.
Posts (ARTICLE TABLE):
1.ID (text)
2.TITLE (text)
3.TEXT (text)
Visitor (COUNTER TABLE):
1.ID (number)
2.IP (text)
3.DATE (TIMESTAMP)
4.ID_POSTS (text)
The full php code:
$week_start = date('Y-m-d',time()+( 1 - date('w'))*24*3600);
$week_end = date('Y-m-d',time()+( 7 - date('w'))*24*3600);
$query = "SELECT * FROM visitor WHERE DATE BETWEEN '".$week_start."' AND '".$week_end."' LIMIT 0, 10 ";
$result = $db->prepare($query);
$result->execute();
$i=1;
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$post[$i]=$row[ID];
$i++;
}
for ($i = 1; $i <= 10; $i++) {
$query = "SELECT * FROM POSTS WHERE ID_POST = '".$post[$i]."' LIMIT 0, 10";
$result = $db->prepare($query);
$result->execute();
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo<<<PRINT
$row[ID].$row[TITLE]: $row[text]
PRINT;
}
}
The problem I think is that you have to count how many people were at the table wrote the secondary then move the primary table.
Steps:
1. Count how many entered each article each week by the secondary table
2. extract the 10 Most Viewed same week
3. present the 10 most read article in the same week by the main table
Thanks in advance.
For such a query, I would expect a COUNT(), GROUP BY, ORDER BY, and LIMIT 10. Hence:
SELECT id_post, COUNT(*) as cnt
FROM visitor v
WHERE DATE BETWEEN '".$week_start."' AND '".$week_end."'
GROUP BY id_post
ORDER BY cnt DESC
LIMIT 0, 10 ;
Note: The WHERE clause always followed the FROM clause.
Also, you should not be embedding dates in the query string. You should learn to use parameters instead.
select * from (select id, count(1) as cnt
from visitor where date > (NOW()- INTERVAL 7 DAY) group by id) v1, posts p
where v1.id = p.id
order by v1.cnt desc
limit 10
Not tested.

Unable to get "inbetween" records if year is different

I am having this unique bug
TABLE : tbl
id | title | iscancel | sold_dt
id: UID
Title : varchar
iscancel : 0/1
sold_dt : timestamp
select * from tbl where iscancel = 0 and DATE_FORMAT(sold_dt,"%m/%d/%Y") BETWEEN "06/01/2015" AND "03/01/2016" GROUP BY day(sold_dt) order by (sold_dt) asc
(note that year is different)
0 records returned
but if i do
select * from tbl where iscancel = 0 and DATE_FORMAT(sold_dt,"%m/%d/%Y") BETWEEN "06/01/2015" AND "12/01/2015" GROUP BY day(sold_dt) order by (sold_dt) asc
or
select * from tbl where iscancel = 0 and DATE_FORMAT(sold_dt,"%m/%d/%Y") BETWEEN "01/01/2016" AND "03/01/2016" GROUP BY day(sold_dt) order by (sold_dt) asc
(note that year is same)
-I'll get some records
*Used DATE_FORMAT because my calendar is giving me MM/DD/YYYY (i can not change since it might affect other areas)
what i am doing wrong?
Instead of converting the stored value into another format try changing the parameter itself to correct format. If you cant do it on your website you can do it in your query as well
sold_dt BETWEEN
STR_TO_DATE('06/01/2015', '%m/%d/%Y') AND STR_TO_DATE('03/01/2016', '%m/%d/%Y')
Try by passing static dates into DATE_FORMAT() function
select * from tbl where iscancel = 0 and DATE_FORMAT(sold_dt,"%m/%d/%Y") BETWEEN DATE_FORMAT("06/01/2015","%m/%d/%Y") AND DATE_FORMAT("03/01/2016","%m/%d/%Y") GROUP BY day(sold_dt) order by (sold_dt) asc

mysql, use condition in "where"

I have a database table like:
id name date price
1 a 2014-05-12 10
2 a 0000-00-00 20
3 a 2014-05-13 30
I want to search by date, and if the date exists, return the price of the date.
if the date does not exist, return the price of date 0000-00-00.
For example:
search by date:2014-05-12, return `10`
search by date:2014-05-20, return `30`
I have tried:
select price from table where (date=table.date or table.date='0000-00-00')
but it does not work.
How do I write the correct query?
Here is one method that uses order by and limit:
select t.price
from table t
where t.date in ('0000-00-00', #date)
order by t.date desc
limit 1;
Note that I changed the parameter name from date to #date to distinguish it from the column with the same name.
The syntax should table table.field (e.g. table.date), while in your example you have field.field (price.date).
Try:
select price from table where (table.date='{$date}' or table.date='0000-00-00')
Note that $date is a variable (assuming you use PHP). If you don't use PHP, you can't just write date as a variable here, but you can use #date.
Get the value of the field to be searched.Let it be $date_search.
if(!empty($date_search)) {
$sql = 'select price from table where date ='.$date_search;
mysql_query($sql);
}
else {
$sql = 'select price from table where date ="0000-00-00"';
mysql_query($sql);
}

mysql search get specific rows

Suppose i have a variable ($var) that determines what rows get selected for an mysql query.The table to be searched has a date column along with some others.
If the value of $var = 1 then retrieve top 5 rows in desc date order.
If the value of $var = 2 then retrieve rows 6-10 in desc date order.
If the value of $var = 3 then retrieve rows 11-15 in desc date order.
SELECT
*
FROM
my_table
ORDER BY
my_table.date DESC
LIMIT
[($var - 1) * 5], 5
Where [] is where you should embed your PHP using . to concatenate strings if $var is between 1 and 3
This is a simple as using the OFFSET syntax in the mysql SELECT statement:
SELECT * FROM myTable ORDER BY date DESC LIMIT ($var*5, ($var-1)*5 +1)
select * from table order by date desc limit (($var-1)*5+1), ($var*5)

Select adjacent records in Mysql

Assuming this table is ordered by date
id | date | customer
3 | 2009-10-01| Frank
1 | 2010-10-11| Bob
4 | 2010-11-01| Mitchel
2 | 2010-11-02| Jim
I would like to make a query so that knowing ID = 4 the resulting rows are
$row[0]['id'] == 1 //previous
$row[1]['id'] == 4 //most recent/current
$row[2]['id'] == 2 //next
A mysql only solution would be best, but if there is an elegant php solution that would be cool as well.
As the table IS sorted by date column, you can run following queries to get it:
For previous row:
select * from tablename where `date` < (select `date` from tablename where id=4) order by `date` desc limit 1
For current row:
select * from tablename where id=4
For next row:
select * from tablename where `date` > (select `date` from tablename where id=4) order by `date` asc limit 1
Output: These three queries return the result (one by one) as following:
id date customer
1 2010-10-11 Bob
4 2010-11-01 Mitchel
2 2010-11-02 Jim
Since you are ordering by date, but basing the row you want the adjacent rows on id, your going to have to do 2 queries. The first to determine the date for the ID you have selected, the second to get the adjacent rows.
Step 1 - Get the date
Select date
FROM yourtable
WHERE id = 4
Step 2 - Get all the rows
SELECT *
FROM yourtable
WHERE date IN ( (select MAX( date ) from yourtable where date < $datefromquery1)
, $datefromquery1
, (select MIN( date ) from yourtable where date > $datefromquery1)
)
The LIMIT function can take two arguments, an offset and a number of rows to return. In your case, you want the offset to be (the number of rows with dates before the desired row) - 1, or in this case 2 - 1 = 1, and the number of rows to be three. So the SQL you want is
SELECT * FROM customers ORDER BY date ASC LIMIT 1,3;
and the number "1" will be the result of the query
SELECT COUNT(*)-1 FROM customers WHERE date > "2010-11-01";
I don't believe MySQL will let you use a subselect or function value as the argument of LIMIT, so you'll have to store that using PHP and construct the next query that way.

Categories