how to use the function time in query myql - php

i have this query :
SELECT *
FROM news
WHERE STATE LIKE 'SI'
AND data<'".time()."'
ORDER
BY data DESC
limit 0,1
and i would like to know if the function time it's correct because there is an error on synthase.
thank's you !

There are reserved words in MySQL which you cannot use as column names without clearly indicating they are names. See:
https://dev.mysql.com/doc/refman/5.7/en/keywords.html
Both 'data' and 'date' are reserved words. Use back ticks to indicate they are used as names:
$query = "SELECT *
FROM `news`
WHERE `STATE` LIKE 'SI'
AND `data` < '".time()."'
ORDER
BY `data` DESC
LIMIT 0,1
or better, in my opinion, use better column names:
$query = "SELECT *
FROM newsItems
WHERE itemState LIKE 'SI'
AND creationDate < '".time()."'
ORDER
BY creationDate DESC
LIMIT 0,1";
As you can see I had to guess what the columns really stand for, because it's not directly clear from the names. It should be, because that's what they are there for.

TIME() is a function in which you also need to pass your parameter.
So, instead of using it blank like:
select TIME();
you need to use it in this way:
select TIME(now());
Note: In your query, you need to pass like (only if you have datetime field in your table):
AND time(data) < time(now())

Related

How to order strings format from database in PHP?

I have numbers like this: 666/2014, 01/2014, 1/2014, 02/2014, 125/2014, 06/2014 ...etc as STRING named 'n_inscription' in database.
I want to retrieve those strings In ORDER from database
I used this:
$sql_students = $bd->query("SELECT * FROM `es_student_infos`
WHERE school_year='$school_year'
ORDER BY right(n_inscription, 4) * 1,
substring_index(n_inscription, '/', 1) * 1");
I get result like this:
01/2014,
02/2014,
06/2014,
1/2014,
125/2014,
666/2014
and the result I'm looking for is like this:
01/2014,
1/2014,
02/2014,
06/2014,
125/2014,
666/2014
any suggestion please?
The best approach is probably to normalize the input so normal sorting does what you want it to do. For example, store the student number and year in two separate INTEGER columns and then ORDER BY studentNumber ASC, inscriptionYear ASC.
If that's absolutely not possible:
SELECT
*
FROM
es_student_infos
ORDER BY
CAST(RIGHT(n_inscription, 4) AS UNSIGNED) ASC,
CAST(LEFT(n_inscription, LOCATE('/', n_inscription) - 1) AS UNSIGNED) ASC
Link to fiddle demonstrating the solution: http://sqlfiddle.com/#!2/a5538/1/0
The correct way is change the type of n_inscription to date/datetime/timestamp and then use order by as default.
There's more advantages to use date fields like comparison and date calculations, so I suggest you do like this.
EDIT: Change the order by collumn to school_year and not n_inscription:
SELECT * FROM `es_student_infos`
WHERE school_year='$school_year'
ORDER school_year

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";

Mysql order query by index

I am running an mysql query and I am trying to order the results by there auto incrementing index value. I am running my query with this code.
$query = mysql_query("SELECT * FROM chanels WHERE videolocation != '' ORDER BY index DESC ");
This worked before I added the ORDER BY function and now when I run a mysql_num_rows test it is returning 0 rows. If you have any ideas thank you I appreciate it.
Are you sure that the column is called index? That is not an ideal choice for a column name because it is a reserved word. Normally the auto-increment column should be called id or similar.
If you really have called your column index then you need to quote it using backticks in your SQL queries:
SELECT * FROM chanels
WHERE videolocation != ''
ORDER BY `index` DESC
Edit yoru code like this
$query = mysqli_query("SELECT * FROM chanels
WHERE videolocation != ''
ORDER BY 'index' desc");
may it helps you

Select a Random Database Row WHERE ID=a random value created by PHP function

If I have a PHP function that generates a random number, is it possible to pass that variable into the sql statement in the WHERE clause? I'm using CodeIgniter, so this is my code using its syntax.
$random = rand(1, 572);
$result = $this->db->query( ' SELECT part1, part2, _id FROM `questions` WHERE `_id` >= '$random' LIMIT 0,1 ');
Is this even possible to do?
EDIT: The reason I want the php to execute the random number is because I need to call it multiple times throughout my pages, and it needs to do another call to another database using a sql query
Yes it is possible if you concatenate the variable with the string:
$query = "SELECT
part1,
part2,
_id
FROM
questions
WHERE _id >= " . $random . " LIMIT 0,1";
$result = $this->db->query($query);
But if what you want is to select a random row, then you might want this query
SELECT part1, part2, _id FROM questions ORDER BY RAND() LIMIT 1
EDIT
I understand that _id will be random, but you are specifying the min and max for rand(), right? So you'd have to change it whenever you insert a new row, or you'd have to use two queries if you want to make sure rand() does not return a value too high. By using ORDER BY RAND() you are free from both problems. You simply have to get the value of _id that was returned from the query.
This might just be a mater of using double quotes instead of single quotes on the outside of your string.
$result = $this->db->query("SELECT part1, part2, _id FROM `questions` WHERE `_id` >= '$random' LIMIT 0,1 ");
Try this:
$result = $this->db->query("SELECT part1, part2, _id FROM questions WHERE _id >= '".$random."' LIMIT 0,1 ");

Categories