If I have a MySQL statement like this:
$sql="SELECT `name`, DATE(`time`), FROM `students` WHERE `id`='5'";
I am using CodeIgniter and trying to fetch the result in object format rather than array format (i.e. not using row_array())
$query=$db1->query($sql);
$row=$query->row();
I can get the name as
echo $row->name;
I cannot get the value of date with this method.
Any Ideas how I can get the date with this method, with something like:
echo $row->date;
?
You should be able to use your own field names in SQL like so:
$sql="SELECT `name`, DATE(`time`) as mydate, FROM `students` WHERE `id`='5'";
and subsequently access it like
echo $row->mydate;
If you want the column in the result set to be named date, then add AS date after the DATE expression. You'll then be able to access it as $row->date.
Related
I am Trying to get the current_timestamp()::timestamp_ntz in a variable and trying use the variable in the insert statement for a timestamp datatype its giving error and when i debug the format was "Mon Dec 09 2019 04:24:50 GMT-0800 (PST)" this in the return statement of the procedure. When i am trying to insert into a timestamp column using that variable its giving error.how to resolve this.
Try leveraging Snowflake's variables instead. The issue you are seeing is because you are setting the current_timestamp() to a text string and then trying to insert it back in, but JS is changing the format. Do something like:
SET var = current_timestamp();
INSERT INTO XXX(LOAD_END_TS) VALUES ($var);
This will work for the duration of the session. The other thing you could try is to change your INSERT in JS to something like:
var rs = snowflake.execute( { sqlText: "select current_timestamp()::timestamp_ntz::varchar"} );
var my_sql_command = "INSERT INTO XXX(LOAD_END_TS) VALUES('"+end_ts+"'::timestamp_ntz);" ;
Ie, given a dataset object ds = DB[:transactions].where{updated_at > 1.day.ago} - no funny joins and stuff going on - how could I fetch the table name (:transactions) ?
If you want the first table in the dataset, you can use ds.first_source.
If you want it as a string you can do:
ds.first_source_table.to_s
If you want a symbol, just omit .to_s
Based on the example provided, I would do something like this.
ds.klass.name
That will return a string with the name of your table.
How to get only date on database column, I have difficulties using Carbon on controller:
$data['nowUser'] = User::where('date', Carbon::today()->toDateString())->get();
Date column looks like this in the database:
That is a DATETIME column, so there's no need for additional formatting the Carbon instance. However you need to use whereDate if you want the fetch all users for which the date column contains today's date:
$data['nowUser'] = User::whereDate('date', '=', Carbon::today())->get();
Because when you pass Carbon::today() to the Query Builder method, the __toString method will be automatically called and return a DATETIME string with the format from Carbon::DEFAULT_TO_STRING_FORMAT, which is exactly that MySQL format Y-m-d H:i:s.
i am using codeigniter with postgresql database
i want to set Default value if we can't get value or value == '' from $_POST or $this->input->post() then need to set value DEFAULT but whenever codeigniter execute query its append a quote like "DEFAULT" but i want my query like below example
Example:
Current Output :
update tablename set fieldname = 'DEFAULT' where
Required Output :
update tablename set fieldname = DEFAULT where condition
here problem with codeigniter query which append quotes for DEFAULT value whenever i will update
please help me to remove quotes from core file whenever i will execute query in database through codeigniter
This is a valid question. First, in postgres for example there are constants such as CURRENT_DATE and CURRENT_TIMESTAMP or in MySQL NOW() that, if quoted will cause an error. The way I handle these case is by using the code igniighter set function with a third parameter set to False to tell code ignighter to False like so:
$this->db->where('id',999);
$this->db->set('draft', False);
$this->db->set('submission_date', 'CURRENT_DATE', False);
$this->db->set('status','ok');
$this->db->update('projects');
This will produce the following query:
UPDATE projects SET draft=False, submission_date=CURRENT_DATE, status='ok' WHERE id='999';
Reference: https://www.codeigniter.com/userguide3/database/query_builder.html#inserting-data
I have my query something like this
INSERT INTO tbl VALUES(UNHEX('some_value'));
How can I pull this query off using codeigniter active record, specifically the '$this->db->set' function
this->db->set('password', $value); // How do I insert the mysql function here
$this->db->insert('tbl');
Take a look at the documentation - it states that if you need some value unescaped, you should set the third parameter in set() to false. Like this:
$this->db->set("password", "UNHEX('".$value."')", FALSE);