CodeIgniter Forums
need help vor mySQL Query!! - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: need help vor mySQL Query!! (/showthread.php?tid=62882)



need help vor mySQL Query!! - Harry05 - 09-04-2015

Aloha,

I have a sql query looks like this 1:1 test query this is it how I have to used it.

Code:
insert into files (created,name,timestamp)values (now(),'xxxx', '2015-08-26 00:00:00')
ON DUPLICATE KEY UPDATE created=NOW(),status='NEW';

and I like have it so ,for exampel.

PHP Code:
public function get_select_file() {
 
$this->db->select 'id' );
 
$this->db->from 'table' );
 
$this->db->where 'name''xxxx' );
 
$this->db->where 'timestamp''2015-08-26 00:00:00' );
 
$query $this->db->get ();
 return 
$query->result_array ();
 } 

or some thing like this pleas help my !!


RE: need help vor mySQL Query!! - rtorralba - 09-04-2015

(09-04-2015, 10:05 AM)Harry05 Wrote: Aloha,

I have a sql query looks like this 1:1 test query this is it how I have to used it.




Code:
insert into files (created,name,timestamp)values (now(),'xxxx', '2015-08-26 00:00:00')
ON DUPLICATE KEY UPDATE created=NOW(),status='NEW';

and I like have it so ,for exampel.




PHP Code:
public function get_select_file() {
 
$this->db->select 'id' );
 
$this->db->from 'table' );
 
$this->db->where 'name''xxxx' );
 
$this->db->where 'timestamp''2015-08-26 00:00:00' );
 
$query $this->db->get ();
 return 
$query->result_array ();
 } 

or some thing like this pleas help my !!

I think that you want execute the first query with codeigniter query builder (like second code). If you want to do this there isn't on duplicate key update sentence at codeigniter query builder. But you can execute it as following way:


PHP Code:
public function insert($name$timestamp)
{
  $sql "insert into files (created,name,timestamp)values (now(),'$name', '$timestamp')
             ON DUPLICATE KEY UPDATE created=NOW(),status='NEW';"
;
  return $this->db->query($sql);


But viewing your query my question is, I supose you use on duplicate key sentence for skip possible concurrence because your primary key is "created", why you don't use autoincrement int for primary key? then your sentence whould be like following:


Code:
insert into files (created,name,timestamp)values (now(), 'xxxx', '2015-08-26 00:00:00')

And with ci query builder:

PHP Code:
public function insert($name$timestamp)
{
  $data = array('name' => $name'timestamp' => $timestamp);
  return $this->db->insert('files' $data);




RE: need help vor mySQL Query!! - Harry05 - 09-11-2015

(09-04-2015, 11:20 AM)rtorralba Wrote:
(09-04-2015, 10:05 AM)Harry05 Wrote: Aloha,

I have a sql query looks like this 1:1 test query this is it how I have to used it.







Code:
insert into files (created,name,timestamp)values (now(),'xxxx', '2015-08-26 00:00:00')
ON DUPLICATE KEY UPDATE created=NOW(),status='NEW';

and I like have it so ,for exampel.







PHP Code:
public function get_select_file() {
 
$this->db->select 'id' );
 
$this->db->from 'table' );
 
$this->db->where 'name''xxxx' );
 
$this->db->where 'timestamp''2015-08-26 00:00:00' );
 
$query $this->db->get ();
 return 
$query->result_array ();
 } 

or some thing like this pleas help my !!

I think that you want execute the first query with codeigniter query builder (like second code). If you want to do this there isn't on duplicate key update sentence at codeigniter query builder. But you can execute it as following way:





PHP Code:
public function insert($name$timestamp)
{
  $sql "insert into files (created,name,timestamp)values (now(),'$name', '$timestamp')
             ON DUPLICATE KEY UPDATE created=NOW(),status='NEW';"
;
  return $this->db->query($sql);


But viewing your query my question is, I supose you use on duplicate key sentence for skip possible concurrence because your primary key is "created", why you don't use autoincrement int for primary key? then your sentence whould be like following:





Code:
insert into files (created,name,timestamp)values (now(), 'xxxx', '2015-08-26 00:00:00')

And with ci query builder:




PHP Code:
public function insert($name$timestamp)
{
  $data = array('name' => $name'timestamp' => $timestamp);
  return $this->db->insert('files' $data);

because I have filetable file id = autoincrement the other table called data I insert in this table data with the same key from file table