I'm kind of new in python and want to make a MySQL select.
I want to select from the MySQL and add a variable to the query like this:
date = now.strftime("%Y-%m-%d)
sql = "SELECT * FROM table WHERE date='%s'" % date
(This offcourse, doesn't work)
I'm used to Php so this is kind of new to me as you can see :)
THANKS!
Maybe this does what you want...
date = now.strftime("%Y-%m-%d)
sql = "SELECT * FROM table WHERE date= '%s' "
sql = sql % (date)
cursor.execute(sql)
results = cursor.fetchall()
import time
now = time.localtime()
date = time.strftime("%Y-%m-%d")
sql = "SELECT * FROM table WHERE date='%s'" % date
I think this fragment does what you want?
Hope this helps
Related
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.
My Sql Query Returning Empty results set. Is there any Mistake in format. I am Using some php variables inside the query..
public function getBlockDate($month,$year){
$connection = db::factory('mysql');
$time_from="00-00-00";
$time_to="23-59-59";
$sql = 'select * from blocks WHERE date LIKE "'.$year.'-'.$month.'%" AND (time_From="'.$time_from.'" AND time_to="'.$time_to.'")';
return $connection->getArray($sql);
}
*Here time_from and time_to table columns are of time type*
Change your SQL query
$sql = 'select * from blocks WHERE date LIKE "'.$year.'-'.$month.'%" AND (time_From="'.$time_from.'" AND time_to="'.$time_to.'")';
' single quotes are missing over $time_from and $time_to
EDITED QUERY
$time_from="00:00:00";
$time_to="23:59:59";
$sql = 'select * from blocks WHERE date LIKE "'.$year.'-'.$month.'%" BETWEEN (time_From="'.$time_from.'" AND time_to="'.$time_to.'")';
also change the - with : in your $time_from and $time_to variables
This should work for you. You didn't put quotas right at the AND clause.
Also when using LIKE in your query be sure to place the wildcard correctly. In your case you are looking for records in date column whose start with year-month then there's your time.
$time_from="00-00-00";
$time_to="23-59-59";
$sql = "SELECT * FROM blocks WHERE date LIKE '".$year."-".$month."%' AND (time_From='".$time_from."' AND time_to='".$time_to."')";
Convert date and time to universal format and then query using it.
I have this string in PHP:
$str = 1,3,6,5
These numbers can change and the length of the set can change to more or less than four. I want to convert to a MySQL query like this in PHP:
$qry = mysql_query("SELECT * FROM tbl WHERE id = 1 or id = 3 or id = 6 or id = 5")
It should be easy, but I don't know how to do it.
I will be grateful for any help.
You could use IN.
$query = mysql_query("SELECT * FROM table WHERE id IN ($str)");
Make sure $str is validated first to prevent sql injection.
There is a table to hold last update date in database:
$syncDate = "CREATE TABLE IF NOT EXISTS sync_info (last_sync TIMESTAMP DEFAULT '2003-01-01 00:00:00')";
And i'm updating it every time:
$dateUpdate = "UPDATE sync_info SET last_sync = NOW()";
Date is needed for data search:
$query = "SELECT * FROM results WHERE InsertionDate BETWEEN '$startDateToSearch' AND NOW()";
where insertion date is also a timestamp in table.
The problem is stupid, but I can't get proper startDateToSearch from database. It always returns integer and I can't use it for data search in that way...
$startDate = "SELECT * FROM sync_info";
$startFind = mysql_query($startDate, $GLOBALS['con']);
$array = mysql_fetch_array($startFind);
$row = $array[0];
$startDateToSearch = $row['last_sync'];
What I'm doing wrong???
Why not just use SQL in one statement, such as
SELECT * from results, sync_info
WHERE InsertionDate BETWEEN sync_info.last_sync AND NOW()
I am new to PHP and MYSQL so this should be a pretty basic question.
I am querying a mysql database and getting three fields into an array. One of the fields type is datetime, but with my current query, it is being captured as a string.
This is my code:
$myquery = mysql_query ("SELECT id,text,when FROM messages ORDER BY cuando DESC");
$nrows = mysql_num_rows ($myquery);
for ($i = 0; $i < $nrows; $i++) {
$row = mysql_fetch_assoc ($myquery);
$when = $row["when"];
I've been googling and I think i have to use the AS operator in my query, but I dont' know how. How can I do this? If possible, just by changing the query...
Thanks!
in PHP: $when = strtotime($row["when"]);
in mySQL: SELECT id, text, UNIX_TIMESTAMP( when ) AS when FROM...
If you want a unix timestamp, you can either do it in your query or in php.
In your query:
$myquery = mysql_query ("SELECT id,text,UNIX_TIMESTAMP(when) FROM messages ORDER BY when DESC");
in PHP:
$when = strtotime($row['when']);
Edit: WHEN being a reserved MySQL keyword, this query will give an error. Use something else for your date field.