Order mysql results vs like - php

$query = "SELECT * FROM `tele`.`pedidos`
WHERE `data` LIKE '".date('d/m/Y')."';";
How can I order this by any column? I try:
$query = "SELECT * FROM `tele`.`pedidos`
WHERE `data` LIKE '".date('d/m/Y')."
ORDER BY numero DESC';";
But doesnt work...
Any suggestion?

I bet you are getting syntax error exception right? It's because you lack single quote after the value of date. Try this for clearer view,
$dateHere = date('d/m/Y');
$query = "SELECT * FROM `tele`.`pedidos` WHERE `data` LIKE '$dateHere' ORDER BY numero DESC';";
if you are searching for a specific date, use = not LIKE because it's for pattern matching
$dateHere = date('d/m/Y');
$query = "SELECT * FROM `tele`.`pedidos` WHERE `data` = '$dateHere' ORDER BY numero DESC';";

The single quote in the second query is misplaced. The failure to work could possibly be related to that.
$query = "SELECT * FROM `tele`.`pedidos` WHERE `data` LIKE '".date('d/m/Y')."' ORDER BY numero DESC";

The query syntax is basically right but the single quotes are wrong:
$query = "SELECT * FROM `tele`.`pedidos` WHERE `data` = '".date('d/m/Y')."' ORDER BY numero DESC;";
Actually, this query doesn't make sense. Originally, I read the question that the first query worked but not the second (hence the focus on quotes). Are you trying to say that the date is today? What is data? A string data type or a date data type?
If you wanted to find a match to today and data is a character string, it would be more like:
$query = "SELECT * FROM `tele`.`pedidos`
WHERE date( `data`) = date(now())
ORDER BY numero DESC;";

Related

SELECT query failing after updating it

I modified the following query and am now getting this error.
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result,
boolean given
I have read into what this error means and I know it is my query that is wrong. This worked perfectly before I changed the query. It was this before...
$query = mysqli_query($con, "SELECT * FROM users WHERE `group` = 3");
I changed it to...
$query = mysqli_query($con, "SELECT * FROM users WHERE `group` = 3, 4 ,5");
$array = array();
while ($row = mysqli_fetch_assoc($query)) {
What am I doing wrong in the new query?
your query has a mistake.
Try this:
$query = mysqli_query($con, "SELECT * FROM users WHERE `group` in (3, 4 ,5)");
The in serves the same function as an OR
Your query is indeed wrong, you cannot check a value like that.
If you want to check against multiple values, you can make use of the IN keyword.
"SELECT * FROM users WHERE `group` IN (3, 4 ,5)"
If you want to check of either one of the choice use IN instead:
$query = mysqli_query($con, "SELECT * FROM users WHERE `group` IN (3, 4 ,5)");
For comma separated value use IN
SELECT * FROM users WHERE `group` IN(3,4,5);
you may try this following also,may be you will get your result.
SELECT * FROM users WHERE group = 3 or group = 4 or group = 5;
Thanks.

MySQL, get data between two dates that are formatted as strings

Would be grateful for some help!
Database Tables are set up like this:
id(varchar),
temp(varchar),
humi(varchar),
time(varchar)
Then I thought the user to input the ID, start date and end date.
The problem is how the string in the Time column is formatted, example: 18/03/14: 21:52:36
The user should not have to enter the time, just the date.
I thought it would be possible to do in a similar way:
$result = mysql_query("SELECT * FROM $tbl WHERE id = '$id' AND time BETWEEN '$start%' AND '$stop%'");
But it did not work.
Is it possible to do this with a sql query when the date is stored in such a way?
Regards
. Anders
Edit:
It did not work, probably because I'm doing wrong though = /
If I do this:
$start= "13/02/14 : 12:17:34";
$stop = "13/02/14 : 12:36:18";
$result = mysql_query("SELECT * FROM $tbl WHERE id = '$id' AND tid BETWEEN '$start' AND '$stop'");
..the data will appear as expected
But when I try to to use str_to_date () ,it did not work as I thought, or it did not come out any data at all.
$start= "13/02/14";
$stop = "10/02/14";
$id = "3E000004C6DB8D28";
$result = mysql_query("SELECT * FROM $tbl WHERE id = '$id' AND tid BETWEEN str_to_date('$start%', '%d/%m/%Y') AND str_to_date('$stop%', '%d/%m/%Y')");
edit2:
Do not really know what I was doing weird the first time, but now it works with this code:
$result = mysql_query("SELECT * FROM $tbl WHERE id = '$id' AND tid BETWEEN '$start' AND '$stop'");
You need to use str_to_date():
WHERE id = '$id' AND
tid BETWEEN str_to_date('$start%', '%d/%m/%Y') AND str_to_date('$stop%', '%d/%m/%Y')
Obviously, you can also do this in the application before inserting the values into the query. If so, convert the values to 'YYYY-MM-DD' format.

Mysql select list after a specific id

I'm trying to find a solution to select a list of rows coming after a certain Id from an ordered list.
For example, first I select 1000 rows. Then, on a subsequent request, i want to fetch another 1000 rows coming from after the last id of the first request. I know i can do it with limit, but suppose there has been 100 rows added between the first and second request, there will be 100 rows that will be from the first request.
Both queries will be ordered by the date of the entries.
Here's an example of the query I thought of:
$query = "SELECT * FROM table WHERE id AFTER $id ORDER BY date DESC";
$query = "SELECT * FROM `table` WHERE `id` > '$id' ORDER BY `date` DESC LIMIT 1000";
Two ways to do this:
WHERE
"SELECT * FROM `table` WHERE `id` > '$id' ORDER BY `date` DESC LIMIT $length"
LIMIT
"SELECT * FROM `table` LIMIT $start, $length"
$query = "SELECT * FROM table WHERE id > $id ORDER BY date LIMIT 1000";
You're asking about logic, not code so here it is.
The first request selects the first 1000.
$query = "SELECT * FROM the_table ORDER BY `date` DESC LIMIT 0,1000";
NB date is a reserved word so needs escaping if you've called a column "date" which you shouldn't.
$rs=$db->selectMany($query); // replace this with however you select the rows. $rs is results set
Do stuff with PHP and save the maximum id. They may not be in order.
$maxid=0;
foreach ($rs as $r){
// whatever you need to do with your results
$maxid=max($maxid, $r->id);
}
Your subsequent select uses the last id
$query = "SELECT * FROM the_table WHERE id > $maxid ORDER BY date DESC LIMIT 0,1000";
BUT you need to take note that you're ordering by date and using id to find a breakpoint which sounds like it would cause data to be missed.
Perhaps you mean to use WHERE`date`> $maxdate? If so you can figure that out from the code given.

Where do I put DESC in a mysql query? (PHP)

This is my query:
$query = "SELECT * FROM table ORDER BY 'timestamp' LIMIT $startAt, $perPage";
I'm trying to sort the results by the latest (latest on top), so I tried to add DESC.
But where ever I put it in the query it gives me an error, the only way it doesn't give an error is like this:
$query = "SELECT * FROM movies ORDER BY 'timestamp' DESC LIMIT $startAt, $perPage";
But even though it doesn't give an error, it still doesn't work.
This probably seems like a silly thing to ask but it's really driving me crazy
Timestamp is the datatype in MySQL. So, always try to avoid such column name in your table. Even, if you have a column named as timestamp, you must have to use backtick. So, use below query:
$query = "SELECT * FROM table ORDER BY `timestamp` LIMIT $startAt, $perPage";
Try backtick on timestamp :
$query = "SELECT * FROM movies ORDER BY `timestamp` DESC LIMIT $startAt, $perPage";
Using backticks permits you to use alternative characters
I think however, instead of mandating whether or not you can use backticks, they should have a standard for names. It solves more 'real' problems.
Do not use quote in field name. Try this:
$query = "SELECT * FROM movies ORDER BY timestamp DESC LIMIT $startAt, $perPage";

How to do a MYSQL Select dependant on a previous MYSQL Select

My problem is that I need to SELECT fields from a MYSQL table depending on a previous MYSQL SELECT. Before I did this through an if and else if statement when it could only be one of two things but now I've added a third and so this no longer works. This is the effect I want:
$call = "SELECT * FROM blog_posts WHERE id = $_GET[id] LIMIT 1";
$latest = mysql_query($call);
$result = mysql_fetch_array($latest);
$post_cat = $result['category'];
$sql="SELECT title, post_thumb, id FROM blog_posts WHERE category = $post_cat AND id <> $_GET[id] ORDER BY id DESC LIMIT 6";
Thanks in advance.
$sql="SELECT `title`, `post_thumb`, `id` FROM `blog_posts` WHERE category = '".$post_cat."' AND `id` = '".$_GET[id]."' ORDER BY `id` DESC LIMIT 6";
Put tables/columns in backticks and surround PHP variables in concatenated quotes. At the moment, your query asks MySQL to find a value in the category column which is $post_cat. Not the PHP variable $post_cat, the explicit string $_post_cat.

Categories