Cannot insert UTC_TIMESTAMP() with pdo prepared statement - pdo

How can I use UTC_TIMESTAMP() with this prepared insert statement? It is not liking the format/binding and errors out.
// prepare the statement
$stmt = $db->prepare("INSERT INTO accounts (
account_username,
account_password,
account_email,
fname,
lname,
dtCreated
) VALUES (
:account_username,
:account_password,
:account_email,
:fname,
:lname,
:dtCreated
)
");
//set bindings
$binding = array(
'account_username' => $_POST['username'],
'account_password' => $newhash,
'account_email' => $_POST['email'],
'fname' => $_POST['fname'],
'lname' => $_POST['lname'],
'dtCreated' => UTC_TIMESTAMP()
);
// execute the insert
$stmt->execute($binding);

Specify the UTC_TIMESTAMP() in the statement itself and not as a parameter.
$stmt = $db->prepare("INSERT INTO accounts (
account_username,
account_password,
account_email,
fname,
lname,
dtCreated
) VALUES (
:account_username,
:account_password,
:account_email,
:fname,
:lname,
UTC_TIMESTAMP()
)
");
//set bindings
$binding = array(
'account_username' => $_POST['username'],
'account_password' => $newhash,
'account_email' => $_POST['email'],
'fname' => $_POST['fname'],
'lname' => $_POST['lname']
);
// execute the insert
$stmt->execute($binding);
If you do this:
$stmt = $db->prepare("INSERT INTO accounts (
account_username,
account_password,
account_email,
fname,
lname,
dtCreated
) VALUES (
:account_username,
:account_password,
:account_email,
:fname,
:lname,
:dtCreated
)
");
//set bindings
$binding = array(
'account_username' => $_POST['username'],
'account_password' => $newhash,
'account_email' => $_POST['email'],
'fname' => $_POST['fname'],
'lname' => $_POST['lname'],
'dtCreated' => UTC_TIMESTAMP()
);
// execute the insert
$stmt->execute($binding);
Then UTC_TIMESTAMP() gets turned into a string (because its not a PHP function) and the SQL statement ends up being turned into:
INSERT INTO accounts (
account_username,
account_password,
account_email,
fname,
lname,
dtCreated
) VALUES (
'jblow',
'password',
'jblow#gmail.com',
'Joe',
'Blow',
'UTC_TIMESTAMP()'
);
And if you notice, UTC_TIMESTAMP() is specified as a string which will not work.

You can do that using the NOW() MySQL function I guess.

Related

One Oracle scheduled job runs parallel with another instance. How to prevent this problem?

I have a stored Procedure which triggerd by a job every day. Sometimes the procedure takes more time an Oracle scheduler
runs another instance of my procedure. This is a problem. How can i prevent Oracle from starting a new procedure until
the first one is finsish or kill the first one and start the second.
BEGIN
SYS.DBMS_SCHEDULER.CREATE_JOB
(
job_name => 'GWANEU.AS_ABMELDEN_JOB'
,start_date => TO_TIMESTAMP_TZ('2021/08/11 09:30:00.000000 Europe/Berlin','yyyy/mm/dd hh24:mi:ss.ff tzr')
,repeat_interval => 'FREQ = DAILY; INTERVAL = 1'
,end_date => NULL
,job_class => 'DEFAULT_JOB_CLASS'
,job_type => 'STORED_PROCEDURE'
,job_action => 'GWANEU.AS_LOESCHEN_JOB_PROCEDURE'
,comments => 'Job, der autom. nach X Jahren die abgemeldeten Arbeitsstätten löscht.'
);
SYS.DBMS_SCHEDULER.SET_ATTRIBUTE
( name => 'GWANEU.AS_ABMELDEN_JOB'
,attribute => 'RESTARTABLE'
,value => FALSE);
SYS.DBMS_SCHEDULER.SET_ATTRIBUTE
( name => 'GWANEU.AS_ABMELDEN_JOB'
,attribute => 'LOGGING_LEVEL'
,value => SYS.DBMS_SCHEDULER.LOGGING_OFF);
SYS.DBMS_SCHEDULER.SET_ATTRIBUTE_NULL
( name => 'GWANEU.AS_ABMELDEN_JOB'
,attribute => 'MAX_FAILURES');
SYS.DBMS_SCHEDULER.SET_ATTRIBUTE_NULL
( name => 'GWANEU.AS_ABMELDEN_JOB'
,attribute => 'MAX_RUNS');
SYS.DBMS_SCHEDULER.SET_ATTRIBUTE
( name => 'GWANEU.AS_ABMELDEN_JOB'
,attribute => 'STOP_ON_WINDOW_CLOSE'
,value => FALSE);
SYS.DBMS_SCHEDULER.SET_ATTRIBUTE
( name => 'GWANEU.AS_ABMELDEN_JOB'
,attribute => 'JOB_PRIORITY'
,value => 3);
SYS.DBMS_SCHEDULER.SET_ATTRIBUTE_NULL
( name => 'GWANEU.AS_ABMELDEN_JOB'
,attribute => 'SCHEDULE_LIMIT');
SYS.DBMS_SCHEDULER.SET_ATTRIBUTE
( name => 'GWANEU.AS_ABMELDEN_JOB'
,attribute => 'AUTO_DROP'
,value => FALSE);
SYS.DBMS_SCHEDULER.SET_ATTRIBUTE
( name => 'GWANEU.AS_ABMELDEN_JOB'
,attribute => 'RESTART_ON_RECOVERY'
,value => FALSE);
SYS.DBMS_SCHEDULER.SET_ATTRIBUTE
( name => 'GWANEU.AS_ABMELDEN_JOB'
,attribute => 'RESTART_ON_FAILURE'
,value => FALSE);
SYS.DBMS_SCHEDULER.SET_ATTRIBUTE
( name => 'GWANEU.AS_ABMELDEN_JOB'
,attribute => 'STORE_OUTPUT'
,value => TRUE);
SYS.DBMS_SCHEDULER.ENABLE
(name => 'GWANEU.AS_ABMELDEN_JOB');
END;
/
You can check if the job is running already at the beginning of your procedure
GWANEU.AS_LOESCHEN_JOB_PROCEDURE
Just paste that into you procedure.
select nvl(max(1), 0)
into nExists
from user_scheduler_running_jobs j
where j.JOB_NAME = 'AS_ABMELDEN_JOB'
-- If job running already
if (nExists = 1) then
return;
end if;

How to sub query a select into another select and put inside a variable with laravel?

I want to make a select that solves the fact that I have to perform two validations one for each table, what i'm doing now is validating the $table1 and validating the $table2 ,i just want to validate $table2
$table1 = DB::table('name_table1')
->where('invoice_id','{$table2}.id')
->get();
$table2 = DB::table('name_table2')
->select('name','invoices','{$table} as itens_of_invoice')
->get();
dd($table);
And return me something like;
Array (
[0] => Object(
'name' => 'value_name',
'invoices' => 'value_invoices',
'item_of_invoice' =>
Array(
[0] => Array(
'name_item' => 'value_name',
'price' => 'value_price'
)
)
)
[1] => Object(
'name' => 'value_name',
'invoices' => 'value_invoices',
'item_of_invoice' =>
Array(
[0] => Array(
'name_item' => 'value_name',
'price' => 'value_price'
)
)
)
[2] => Object(
'name' => 'value_name',
'invoices' => 'value_invoices',
'item_of_invoice' =>
Array(
[0] => Array(
'name_item' => 'value_name',
'price' => 'value_price'
)
)
)
)
I don't find any solution if someone ever did this please help me

SUM function on multiple columns in Lithium ORM

I want something like this:
$res = Model::find('all', array(
'fields' => array(
'SUM(col1)' => array(
'alias' => 'col1_total',
),
'SUM(col2)' => array(
'alias' => 'col2_total',
)
)
);
expected generated SQL:
SELECT SUM(col1) AS col1_total, SUM(col2) AS col2_total
FROM `tbl` AS `Model` WHERE 1;
I tried many ways.
is this possible?
a working example for a single col:
$res = Model::find('all', array(
'fields' => 'SUM(col1)'
)
);
Cool!
working example:
$res = Model::find('all', array(
'fields' => array(
'SUM(col1) AS col1_total',
'SUM(col2) AS col2_total'
)
);

Order by using Sum Points in Yii?

$resource_cnt = ShopVisituser::model()->findAll(array(
'select' => '*, sum(points) as amt',
'condition' => 'user_id=:user_id AND merchant_id=:merchant_id',
'order' => 'amt DESC',
'params' => array(':user_id' => $datas->user_id, ':merchant_id' => $datas->merchant_id))
);
I can't order by sum(points) as amt Desc.Why?how to order that Sum(points) in Yii.
Adding a function on your Model, for example:
public function scopes() {
return array(
'byamt' => array('order' => 'amt DESC'),
);
}
Now, you can use your query like this:
$resource_cnt = ShopVisituser::model()->byamt()->findAll(array(
'select' => '*, sum(points) as amt',
'condition' => 'user_id=:user_id AND merchant_id=:merchant_id',
'params' => array(':user_id' => $datas->user_id, ':merchant_id' => $datas->merchant_id))
);

updating three table with one execution

i try to updating three tables with one button execution, but why just t_user that changed? t_publisher and t_label didnt change and no error warning..
function update($id_user=null){
if (($this->input->post('submit') == 'Update')){
$user=$this->input->post('username');
$pass=$this->input->post('userpassword');
$ussta=$this->input->post('userstatus');
$usty=$this->input->post('usertype');
$id_label = $this->db->query("select ID_LABEL from t_label where LABEL = '$name->USER_NAME'")->row();
$id_publisher = $this->db->query("select ID_PUBLISHER from t_publisher where PUBLISHER = '$name->USER_NAME'")->row();
$data = array(
'USER_NAME' => $user,
'USER_PASS' => $pass,
'USER_STATUS' => $ussta,
'USER_TYPE' => $usty
);
$data2 = array(
'LABEL' => $user,
);
$data3 = array(
'PUBLISHER' => $user,
);
$this->db->where('USER_ID', $this->input->post('id'));
$this->db->update('t_user', $data);
$this->db->where('ID_LABEL', $this->input->post('$id_label'));
$this->db->update('t_label', $data2);
$this->db->where('ID_PUBLISHER', $this->input->post('$id_publisher'));
$this->db->update('t_publisher', $data3);
redirect("registrasi/reg");
}
$var['data'] = $this->db->query("select * from t_user where USER_ID= '$id_user'")->row_array();
$this->load->view('update', $var);
}
please help :)
Try
function update( $id_user = NULL ){
if ( ( $this->input->post( 'submit' ) === 'Update' ) ) {
$user = $this->input->post( 'username' );
$pass = $this->input->post( 'userpassword' );
$ussta = $this->input->post( 'userstatus' );
$usty = $this->input->post( 'usertype' );
$sql = "SELECT ID_LABEL FROM t_label WHERE LABEL = ?";
$id_label = $this->db->query( $sql, array( $name->USER_NAME ) )->row();
$sql = "SELECT ID_PUBLISHER FROM t_publisher WHERE PUBLISHER = ?";
$id_publisher = $this->db->query( $sql, array( $name->USER_NAME ) )->row();
$data = array(
'USER_NAME' => $user,
'USER_PASS' => $pass,
'USER_STATUS' => $ussta,
'USER_TYPE' => $usty
);
$data2 = array(
'LABEL' => $user,
);
$data3 = array(
'PUBLISHER' => $user,
);
$this->db->where('USER_ID', $this->input->post('id'));
$this->db->update('t_user', $data);
$this->db->where('ID_LABEL', $id_label);
$this->db->update('t_label', $data2);
$this->db->where('ID_PUBLISHER', $id_publisher);
$this->db->update('t_publisher', $data3);
redirect("registrasi/reg");
}
$var['data'] = $this->db->query( "SELECT * FROM t_user WHERE USER_ID= '$id_user'")->row_array();
$this->load->view('update', $var);
}
I'm a bit confused. Is it a model file? If it's so, why there is a redirect command? And also why there is a $this->load->view()? And once more, where $name came from? You assign $id_user as default parameter, where do you use it in this file?