Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I want to count all the posts from the user, from the last hour. This is my code:
$all_user_recent_posts=mysql_query("select * from user_post where user_id=$userid and where post_time >= DATE_SUB(NOW(),INTERVAL 1 HOUR);");
$count_user_recent_posts=mysql_num_rows($all_user_recent_posts);
This code doesn't work(it has the value 0). But when I delete and where post_time >= DATE_SUB(NOW(),INTERVAL 1 HOUR), it does work, but it show me ALL the post of the user, and not posts from the last hour. Code:
$all_user_recent_posts=mysql_query("select * from user_post where user_id=$userid;");
$count_user_recent_posts=mysql_num_rows($all_user_recent_posts);
As I stated in comments, and posting this as a community wiki because nothing should be gained from this in regards to rep points.
where user_id=$userid and where post_time it's a syntax error.
where user_id=$userid and post_time the where clause uses ONE where, not multiple and seperated by AND or OR.
Reference:
http://dev.mysql.com/doc/en/where-optimizations.html
and error checking against your query would have told you about it too.
http://php.net/manual/en/function.mysql-error.php
and should also be checked against your query, should there be any there also.
Add or die(mysql_error()) to mysql_query()
Error reporting is an additional tool you can use.
http://php.net/manual/en/function.error-reporting.php
Additionally, the post_time column must be a valid MySQL date type.
If you're trying to do math on a varchar type, then it won't work, or anything that isn't a valid date-related type.
Consult:
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-types.html
It is unknown as to how you're using mysql_num_rows().
The following two examples will react differently.
if(mysql_num_rows($result) > 0)
and
if(mysql_num_rows($result) == 1)
You have an extra where in your query
near
and where post_time >= DATE_SUB(NOW(),INTERVAL 1 HOUR);
Replace your with this
$all_user_recent_posts=mysql_query("select * from user_post where user_id=$userid and post_time >= DATE_SUB(NOW(),INTERVAL 1 HOUR);");
Hope that helps :-)
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Trying just to order some ids by the date if their status is not active (aktiv = nei) and if its less then 7 days ago they where created, but i'm getting the "mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given" error, and I can't seem to figure out why.
I'm following the general order of caluses (SELECT,FROM,WHERE,GROUP BY,HAVING,ORDER BY). It works if i remove ORDRE BY or the date selection.
$aktivnei = 'SELECT * FROM `test3` WHERE aktiv LIKE "Nei" AND datex BETWEEN (CURRENT_DATE() - INTERVAL 7 DAY) AND CURRENT_DATE(); ORDER BY datex DESC';
$resultaktivnei = mysqli_query($conn,$aktivnei);
<?php
while ($row = mysqli_fetch_assoc($resultaktivnei)) {
echo '<li class="list-group-item">'.$row['id'].' / '.strftime('%H:%M, %e.%b',strtotime($row['datex'])).'</li>';
};
?>
I have also tried this, but getting the exact same error. I have also tried moving the caluses around but to no success.
$aktivnei = 'SELECT * FROM `test3`';
$aktivnei .= ' WHERE aktiv LIKE "Nei" AND datex BETWEEN (CURRENT_DATE() - INTERVAL 7 DAY) AND CURRENT_DATE();';
$aktivnei .= ' ORDER BY datex DESC';
It's probably dead simple, but right now i'm just scratching my head after trying so many different things.
remove semicolon before order by
'SELECT * FROM `test3` WHERE aktiv LIKE "Nei" AND datex BETWEEN (CURRENT_DATE() - INTERVAL 7 DAY) AND CURRENT_DATE() ORDER BY datex DESC'
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Sorry im just new to mysql and php world. can someone help me?
There is an error of
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'AND NOT= End_date LIMIT 0, 30' at line 1
This is the sql code
SELECT * FROM config_modi_ord WHERE Start_date < && != End_date
Are you using datetime or just date fields? If you're comparing datetime fields that fall on the same day, but different times, then you will get some that appear to fall on the same day. If you just want to compare the date component only, you can extract it using the date() function, so try this:
select * from config_modi_org where date(date_acknowledge) > date(end_date)
If that still isn't what you want, then you really need to add your table schema, some sample data and your exact query to the question.
You need to specify the column you are planning to use, in all filter conditions.
Try this.
SELECT * FROM config_modi_ord WHERE Start_date < End_date
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
This query runs well in mysql but cannot run in mssql. Why?
Don't judge me, I am new in mssql.
UPDATE cvcolumnlist
SET columnindex=columnindex+1
WHERE cvid=40 AND columnindex>=3
ORDER BY columnindex DESC
Error:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'order'.
You just need to remove ORDER clause. ORDER is pointless in an UPDATE statement.
UPDATE cvcolumnlist
SET columnindex=columnindex+1
WHERE cvid=40 AND columnindex>=3
You don't need at all the ORDER BY clause. this is only when SELECTing data from the database.
You may want to note that even though SQL is a language with a specified structure, different SQL engines may parse the query differently and use different syntax since a lot of the SQL functionality is not defined by the language.
for example in mysql you may use the LIMIT keyword for limiting the number of records that are returned and in mssql you may use ROWNUMBER and oracle uses the ROWNUM system.
UPDATE cvcolumnlist
SET columnindex = columnindex+1
WHERE cvid = 40
AND columnindex > 2
this should work
UPDATE [cvcolumnlist]
SET [columnindex] = [columnindex] + 1
WHERE [cvid] = 40 AND [columnindex] >= 3
I'm not sure if this applies. Most of my queries use something like this.
UPDATE [database].[dbo].[cvcolumnlist]
SET [columnindex] = [columnindex] + 1
WHERE [cvid] = 40 AND [columnindex] >= 3
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
looking for something like
Select * FROM 'database' WHERE id = 1-40
something where I can select a number through a number like 1 through 40
multiple number select statement sql
SELECT * FROM database_table WHERE id BETWEEN 1 AND 40
This is an example page you can use: http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_between
The between operator is what you're looking for:
SELECT * FROM `database` WHERE id BETWEEN 1 AND 40
Try:
Select * FROM `tablename` WHERE id >= 1 AND id <= 40
Two possible ways for selecting range in DB:
Select * FROM `database.tablename` WHERE id BETWEEN 1 AND 40 ;
or
SELECT * FROM `DATABASE.TABLENAME` WHERE ID >= 1 AND <=40;
In case, you want to select multiple items which are not in range, you can use:
Select * FROM `database.tablename` WHERE id in (1,2,3,4,5,6.....40);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to retrieve content content based on their time stamp. And my query is
SELECT link,title,timestamp,photo,link,author,comments FROM posts
WHERE timestamp='*Todays Date -1*' LIMIT 6
If the query returns zero rows, how can I query for results 2 days back or three days back using a while statement?
Why not just query full table (without timestamp filter) and return limit of 6 rows. in this case there would be no reason to iteratatively query
SELECT link,title,`timestamp`,photo,link,author,comments
FROM posts
ORDER BY `timestamp` DESC
LIMIT 6
This gives you up to 6 records with most recent records returned first. You should have index on timestamp field for this query, which is OK because you should already have one because you were previously trying to filter on this field.
If you have a case where more than 3 rows show up within last day and you don't need to show records from previous days, this is easy enough to achieve by inspecting the values as you loop through the result set.
This also prevents you from getting into an infinite loop if you only have 2 records in the database.
Also note timestamp is a reserved word, you should be careful when using such field names. If you have to use a reserved word as field name, you must enclose it with backticks.
maybe a for could work
$i=1
for ($row='';$count=0;$i++) {
$result = mysql_query("SELECT link,title,timestamp,photo,link,author,comments FROM posts WHERE timestamp='*Todays Date -$i *' LIMIT 6");
$count = mysql_num_rows($result);
}
I think the query that you want both filters the results and orders them.
SELECT link, title, timestamp, photo, link, author, comments
FROM posts
WHERE timestamp <= '*Todays Date -1*'
ORDER BY timestamp desc
LIMIT 6;
If you actually want 3 rows and not 6 (as suggested by the post title), then change the 6 to 3.