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);
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())
$result = pg_query(Postgres::getInstance(), "SELECT
date_start,
date_end,
cnt_hands,
cnt_hands_won,
amt_won,
id_session,
id_player,
amt_won
FROM
cash_table_session_summary
WHERE
date_start = '2016-04-27 07:20:47'");
This works perfectly.
echo $sessionStart;
$result = pg_query(Postgres::getInstance(), "SELECT
date_start,
date_end,
cnt_hands,
cnt_hands_won,
amt_won,
id_session,
id_player,
amt_won
FROM
cash_table_session_summary
WHERE
date_start = $sessionStart");
This throws this:
2016-04-27 07:20:47 Warning: pg_query(): Query failed: ERROR: syntax
error at or near "07" LINE 13: ... date_start = 2016-04-27 07:20:47 ^
in /home/haris/public_html/project/DAL_General.php on line 102
Is colon a problem? Do I need to escape it somehow? If so, how? I've google but found nothing about escaping colons.
Your date must be inside quotes. You need to change
FROM
cash_table_session_summary
WHERE
date_start = $sessionStart
To
FROM
cash_table_session_summary
WHERE
date_start = '$sessionStart'// add quotes here
For more understand about quotes check When to use single quotes, double quotes, and backticks in MySQL
I had the same issue when I was learning database work, but you need the query to be made in ""'s - Then when referencing a variable or data you need to put it in ''s, example:
$test_query = type_of_connection_query("SELECT * FROM users WHERE id = '1'");
That would not work like:
$text_query = type_of_connection_query("SELECT * FROM users WHERE id = 1");
And of course there are some times when you don't need quotes, like referencing LIMIT's
$text_query = type_of_connection_query("SELECT * FROM users WHERE id = '1' LIMIT 1");
I'm having trouble getting my sql to pull in photos based on the column "order" which contains numbers. The following works fine, however it seems to be pulling in photos based on the "num" column in "cms_uploads".
$photoSql = "SELECT * FROM cms_uploads WHERE ('tableName'='residential_master_rental_availabilities' AND 'recordNum' = '".$rent['num']."' AND 'fieldname' = 'image') LIMIT 1";
This query returns nothing:
$photoSql = "SELECT * FROM cms_uploads WHERE ('tableName'='residential_master_rental_availabilities' AND 'recordNum' = '".$rent['num']."' AND 'fieldname' = 'image') ORDER BY 'order' LIMIT 1";
order is a reserved word.
You need backticks rather than single quotes for the order by. You are ordering by a constant -- that is, doing nothing:
ORDER BY `order`
To help with writing code, only use single quotes for string constants and dates.
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 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