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
Related
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())
i want to write a MySql function that the Limit is set by a row on the table called.
Let me explain.
$query=mysqli_query($this->db,"SELECT a_row, b_row, c_row FROM table WHERE a_row='something' ORDER BY 'whatever' **LIMIT 'c_row'** DESC")or die(mysqli_error($this->db));
c_row in an integer number on the db.
I tried to make a variable before calling the $query from the row (c_row) but i dont think thats possible.
You can use a sub-query to access the value of c_row and use it for the LIMIT clause.
$query=mysqli_query($this->db,"SELECT a_row, b_row, c_row FROM table
WHERE a_row='something' ORDER BY 'whatever' LIMIT (SELECT c_row from table) DESC")or die(mysqli_error($this->db));
I'm using Joshcam's PHP Mysqli Database Class (github) and have been fighting over comparing dates for a long while now.
Bottom line is could anyone explain this error message:
PHP Fatal error: Problem preparing query (SELECT * FROM jobs WHERE business_id = 5 AND active = 1 AND date_visit > `2015-01-01 05:02:14` ORDER BY date_appt DESC) Unknown column '2015-01-01 05:02:14' in 'where clause'
My query goes:
$jobs = $db->rawQuery("SELECT * FROM jobs WHERE business_id = $business_id AND active = 1 AND date_visit > `".$search_from."` ORDER BY date_appt DESC");
Why would my date input be considered a column instead of a field value?
I've tried double quotes, single quotes, no quotes, and it's either turning the quotes to ' or put off by the space between Y-m-d and H:i:s.
You are using backticks for query variable rather single quote so try to remove backticks from value($search_from) else it will be treat as column
$jobs = $db->rawQuery("SELECT * FROM jobs WHERE business_id = '$business_id' AND active = 1 AND date_visit > '$search_from' ORDER BY date_appt DESC");
Enclose the names of database objects (databases, tables, fields, indexes, triggers a.s.o.) in backquotes (``). This is usually not needed but it is useful (read "required") when the name is a MySQL reserved word.
Enclose the string literals in apostrophes ('2015-01-01 05:02:14') or quotes ("2015-01-01 05:02:14"):
SELECT *
FROM `jobs`
WHERE `business_id` = 5
AND `active` = 1
AND `date_visit` > '2015-01-01 05:02:14'
ORDER BY `date_appt` DESC
None of the field names in the query above is a MySQL keyword, there is no need to enclose them in backquotes. I only did it for explanatory purposes.
Your PHP code should read:
$jobs = $db->rawQuery("SELECT * FROM jobs WHERE business_id = $business_id AND active = 1 AND date_visit > '".$search_from."' ORDER BY date_appt DESC");
The issue was mainly that this class doesn't allow for
WHERE the_date > $date1
AND the_date < $date2
I actually had to use between to solve it.
My final code:
$params = array($business_id,1,$search_from,$search_to,$limit_end);
$jobs = $db->rawQuery("SELECT * FROM jobs WHERE business_id = ? AND active = ? AND date_visit BETWEEN ? AND ? ORDER BY date_appt DESC, id DESC LIMIT ?",$params,false);
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";
I can not find a proper answer to this question. I have a very simple code for making a query in mysql to select the row with the maximum value in a determined column (called popularity) from a table (called comments). Every row has a column named comment_id
here is the code:
$connect_error = 'Sorry, try again, there was a connection error';
$con = mysqli_connect('localhost','user_name','password') or die($connect_error);
mysqli_select_db($con, 'database') or die($connect_error);
$result = mysqli_query($con, "SELECT MAX(`popularity`) FROM `comments`");
while ($row = mysqli_fetch_assoc($result)) {
$most_popular = $row['comment_id'];
}
echo "most popular is: $most_popular";
mysqli_free_result($result);
mysqli_close($con);
the screen does not show a proper result. Can someone give me an advice in this regard?
Thank you
You are looking to display the comment_id field, but you don't have that in your SELECT query. You are only selecting the max popularity value, and nothing else.
Try this for your query:
SELECT comment_id FROM comments ORDER BY popularity DESC LIMIT 1
This is sorting your comments by popularity, and then just picking the top one.
Of course you can easily change that to select more columns or even a SELECT * if you want to be able to display other values in this record.
To select the row with max 'popularity' column use this query;
$result = mysqli_query($con, "SELECT * FROM `comments` ORDER BY `popularity` DESC LIMIT 1");
In case you want all sorted by popularity remove the LIMIT 1...
You need
SELECT MAX(`popularity`) AS comment_id FROM `comments`
This will give the column the correct name for the associated array.
You are trying to read the result from a column named comment_id when the result from your query will be named MAX(popularity)
Run this query in mysql "SELECT MAX(popularity) FROM comments" first.The output of this gives you the index to use with $row[index] ie first row which in this case the index will be popularity else just change $row[content_id] to $row[popularity]