• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need help vor mySQL Query!!

#1
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 !!
Reply

#2
(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);

Greetings.
Reply

#3
(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
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.