PHP mysql query code not working - php

this code is not working why :(
$id = $temp['curchar'];
$data=strtotime(date('Y-m-d H:i:s'))+30; //+30 seconds to unix time
mysql_query("UPDATE `chars` SET data='$data' WHERE id='$id'");
Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in C:\Program Files\WebServ\httpd\world_1\char_info_slow.php on line 23

too many questions, you probably can start by checking
how you connect to mysql?
what is the column type for data?
is $id match any record in table?
how to verify are the matched records get updated?
if your account connect to mysql allow to do write?
ps:
$data=strtotime(date('Y-m-d H:i:s'))+30; <-- wordless ...
$data = time()+30;
pps:
at least, you should try
$sql = "UPDATE `chars` SET data='$data' WHERE id='$id'";
mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);

There's no reason to generate a date in PHP just to do some date arithmetic in MySQL. You can do this far easier within mysql as is:
UPDATE chars
SET data=DATE_ADD(data, INVERVAL 30 SECOND)
WHERE id=$id
Of course, this assumes you've made data a datetime type field. If it's just an int, then why bother with all the date math, and just do data=data+30.
As well, you're generating your time value in a highly inefficient manner. You format the current date as a string, convert that string to a number, and add 30 to it. Why not just do
$data = time() + 30;
instead? time returns the current date/time as a single integer (a unix timestamp), saving you the round trip through String Land.

Related

Different Mysql date Results On website and in PHPmyadmin

I wants to select rows which are less than with today date.
In database my date column is saving data in unix time stamp.
I am running on following query with php and mysql. (this query should show 1 row as i have 1 result in the database, but it is showing two rows.)
SELECT * from products where seller_id ='1' and FROM_UNIXTIME(marketing_end_date,'%Y-%m-%d') < CURRENT_DATE();
But when i run above query in phpmyadmin it retrun one row( my desired result).
Here is my code which is reading data from mysql.
function get_sellerenddateproducts($admin,$seller_id,$limit_per_page,$start_index){
$this->db->select('*');
$this->db->where(array('seller_id' => $seller_id));
$this->db->where("FROM_UNIXTIME(marketing_end_date,'%Y-%m-%d') <","CURRENT_DATE()");
$this->db->limit($limit_per_page, $start_index);
$this->db->order_by('id','desc');
$query = $this->db->from($this->_table_name)->get();
$arr = $query->result_array();
//echo $this->db->last_query(); die();
return $arr;
}
Question
Why same query show different results ? How to get correct results?
MySQL actually stores datetime data type fields internally as UTC.
However, PhpMyAdmin shows you the dates using the server default time.
Use this line to detect TimeZone of your MySQL Server:
SELECT ##system_time_zone;
For example, try adding this line before PhpMyAdmin SQL statement:
Set time_zone = '+00:00';
This sets timezone to GMT/UTC, so that any further operations will use GMT.
Let me know if they return same result now :)
I resolve my issue like below.
I create a variable like this.
$todaydate = date('Y-m-d');
and change my query from
$this->db->where("FROM_UNIXTIME(marketing_end_date,'%Y-%m-%d') <","CURRENT_DATE()");
to this
$this->db->where("FROM_UNIXTIME(marketing_end_date,'%Y-%m-%d') <",$todaydate);
then it gives me correct result.

Sum Column with Oracle 11 and PHP - what's wrong?

Trying to get a simply query going through and its not working - newbie here.
Other php files and queries do run well.
$sql = "select sum(rese_nshw) as noshows from tnht_eseo where edta_data = '19.10.01'" ;
$sumParse = oci_parse($conn, $sql);
oci_define_by_name($sumParse, "noshows", $total);
oci_execute($sumParse);
while(oci_fetch($sumParse)){
echo "noshows:". $total;
}
what's wrong ? just outputs blank.
Running the SQL query in Oracle directly, it outputs 6 as NOSHOWS for this query.
If EDTA_DATA is date (datatype), don't compare it to a string as '19.10.01' is a string. Oracle will implicitly try to convert it to appropriate date, but that doesn't have to work always. Besides 19.10.01 can be anything (2019 Oct 01, or 19 Oct 2001, or ...), depends on NLS settings.
Take control over it; see whether using date literal helps (it always has yyyy-mm-dd format):
where edta_data = date '2019-10-01'
Furthermore, if edta_data contains time component (hours, minutes, seconds), then the simplest option is to truncate it, e.g.
where trunc(edta_data) = date '2019-10-01'
but it'll prevent Oracle from using index on that column (if it exists). It can be fixed, no problem; but - first see whether anything of above helps.
You have to use Upper Case as defined here:
column_name The column name used in the query.
Use uppercase for Oracle's default, non-case sensitive column names.
Use the exact column name case for case-sensitive column names.
from: https://www.php.net/manual/en/function.oci-define-by-name.php
Then:
oci_define_by_name($sumParse, "NOSHOWS", $total);

PHP and MYSQL Find Records Based on Date

I have been trying to pull records based on a specified date. In the DB I have a column where I store unix timestamps. The user has the option to select records based on a date. The user inputs: 08/08/2016 (for example)
How would I write my SQL statement to handle this?
$query .= "`".$o."` = '".date("Y-m-d",strtotime($v))."' AND ";
Here we see the line where part of the SQL statement is built, because more than one column could be targeted by the user during their search request.
Somehow I need to be able to turn $o (which is a column storing unix timestamps) into the Y-m-d format for comparison, as shown above and make this work in an SQL statement.
Any help would be appreciated.
EDIT
Here is what worked for me:
$query .= "".$o.">= '".strtotime($v)."' AND".$o."< '".(strtotime($v)+(24*60*60))."' AND ";
You can do something like:
"WHERE DATE(`$o`) = '".date("Y-m-d",strtotime($v))."'"
However this won't use indexes, so if you have a large table it will be slower. If you want to be able to use indexes you can do:
"WHERE `$o` >= ".date("Y-m-d",strtotime($v))."
AND `$o` < ".date("Y-m-d",strtotime($v))." + INTERVAL 1 DAY"
You can also try this approach and use UNIX_TIMESTAMP on MySQL.
$v = '2016-08-04';
$query .= "'".$o."' = UNIX_TIMESTAMP('$v') AND ";

oci_execute(): ORA-01840: input value not long enough for date format

I am currently receiving the following error message from oci in php (I've been on it for a few hours).
oci_execute(): ORA-01840: input value not long enough for date format
It strange, because when I run the query within SQL Developer it seems to work fine.
This makes me think that when I bind the parameter it is turning the dates into a type that is not able to calculate using conventional operators in oracle.
$startDateTime = '2015-03-06 00:00:00';
$endDateTime = '2015-04-06 00:00:00';
$value = '20';
$type = '$';
$SQL = "SELECT count(*) AS \"COUNT\"
FROM bonus where value = :d_value
AND TYPE = :d_type
AND ((:d_valid_from between valid_from AND valid_till) OR (:d_value_till between valid_from AND valid_till) OR (:d_valid_from < valid_from AND valid_till < :d_valid_till))";
$this->stmnt = $this->conn->prepare($SQL);
$this->stmnt->bindParam('d_valid_from', $startDateTime);
$this->stmnt->bindParam('d_valid_till', $endDateTime);
$this->stmnt->bindParam('d_value', $value);
$this->stmnt->bindParam('d_type', $type);
$this->stmnt->execute();
I am unable to find many resources that deal with php directly with this problem in hand. Does anybody have any experience with it?
I think that your dates are being bound as strings in the query. Assuming that the columns you are comparing it to (e.g. valid_from) are dates, then the string value is being converted to a date using the default date format for the sessions. The difference in behavior in SQL Developer is probably because the default format is different.
Anyway, the solution is to follow a very simple and important rule, which is not to rely on default type conversion. Explicitly convert the string to a date in your query, specifying the appropriate format:
TO_DATE( :d_valid_from, 'YYYY-MM-DD HH24:MI:SS' )

mysql date compare query gives wrong results

I have this table and the query I give returns wrong results, I am not sure where the problem is
the date comparisons
or
structure of the query
The query if not clear in the image is :
select * from transact where item_code='msft234' or item_code='hp550x' and transact_date>=STR_TO_DATE('06-07-2013','%d-%m-%Y') and transact_date<=STR_TO_DATE('12-07-2013','%d-%m-%Y')
Your query employs a wrong syntax:
WHERE item_code='msft234' OR item_code='hp550x'
AND transact_date>=STR_TO_DATE('06-07-2013','%d-%m-%Y')
AND transact_date<=STR_TO_DATE('12-07-2013','%d-%m-%Y')
since AND priority is higher, it means that it will be satisfied if either you get hp550x in that date interval, or you get msft234 regardless of the date.
You have to put the OR'ed item codes in parentheses: (item_code='..' OR item_code='..' OR ..), or use IN: e.g.
SELECT * FROM transact
WHERE item_code IN ('msft234', 'hp550x')
AND transact_date BETWEEN
STR_TO_DATE('06-07-2013','%d-%m-%Y')
AND
STR_TO_DATE('12-07-2013','%d-%m-%Y')
Also, depending on the type you select for the date fields, consider that for a date to be "less or equal than 12-07-2013", it has to be less or equal than 12-07-2013 at 00:00, i.e., almost the latest date that will match is 11-07-2013 at 23:59:59.
So "less or equal than 12-07" will actually never select any row from 12-07-2013 unless it happens to have been inserted exactly at midnight.
If you insert rows by only specifying the date, then it will very probably work - the rows will be input at midnight and matched at midnight. But if (some) rows are entered with the full datetime, e.g. because they're type datetime and updated with NOW(), then they will not match.
put the item conditions between ()
(ítem_code = 'msft234' OR ítem_code = 'hp550x') AND transact_date>=STR_TO_DATE('06-07-2013','%d-%m-%Y') and transact_date<=STR_TO_DATE('12-07-2013','%d-%m-%Y')
(ítem_code = 'msft234' OR ítem_code = 'hp550x') AND transact_date BETWEEN STR_TO_DATE('06-07-2013','%d-%m-%Y') and STR_TO_DATE('12-07-2013','%d-%m-%Y')
For security I will put with 2 ()
(ítem_code = 'msft234' OR ítem_code = 'hp550x') AND (transact_date BETWEEN STR_TO_DATE('06-07-2013','%d-%m-%Y') and STR_TO_DATE('12-07-2013','%d-%m-%Y'))

Categories