Codeigniter Transfer data table to table - php

I'm trying to insert data from a table in MySql to other table , but I need to insert a new key before , so I tried this:
$this->db->trans_start();
$this->db->insert($this->table, $data);
$prodId = $this->db->insert_id();
$this->db->from('tmp_prod_icms');
$this->db->where('CADKEY', $this->session->userdata('key'));
$tmpICMS = $this->db->get()->result();
foreach($tmpICMS as $r) { // loop over results
$r["CADPROCOD"] = $prodId;
$this->db->insert('prod_icms', $r);
}
$this->db->trans_complete();
But when I try to insert the key "CADPROCOD" the app just stops, don't give even a error, just stops

Have you checked the logs? In the MySQL ones you should most likely find the error and why it fails.

I found the error, I use $tmpICMS = $this->db->get()->result();
And this return a object, so I can't manipulate this like a Array, so i just need to change for $tmpICMS = $this->db->get()->result_array();
So this work for me

Related

How to get all inserted records ids in codeigniter?

How to get all inserted records ids in codeigniter ?? I am using insert_batch function in for inserting multiple records but codeigniter $this->db->insert_id() function returns only last record id.
//But i need all inserted records ids. If any one has idea please help.
//In codeigniter this function returns only last inserted record id.
function insertStudent(){
$data[] = array('firstname'=>'firstname1','lastname'=>'lastname1');
$data[] = array('firstname'=>'firstname2','lastname'=>'lastname2');
$result = $this->db->insert_batch( 'student', $data );
return $this->db->insert_id();
}
This can be achieved by using first id and count.
Get count of data that you are inserting. (e.g. 2 in your example)
Get first insert id by $this->db->insert_id() (e.g. say 30)
Now get last insert recorded id by = first record id - (count - 1) (e.g. 30 + (2-1) = 31)
Now you can query to get data between id's 30 and 31.
If you absolutely must have the insert_id for each inserted row, the simplest most failproof way would be to loop multiple inserts rather than making one big fat insert.
Looping through multiple inserts will allow you to get all insert_ids. It will also allow you to not loose all the data if one of the rows fails inserting and the database rejects them all because of that.
My best solution is to update the DB_driver.php.
From
public function is_write_type($sql)
{
return (bool) (preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX)\s+/i', $sql);
}
To:
public function is_write_type($sql)
{
return (bool) (preg_match('/^\s*"?(SET|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|TRUNCATE|LOAD|COPY|ALTER|RENAME|GRANT|REVOKE|LOCK|UNLOCK|REINDEX)\s+/i', $sql) && !preg_match('/ RETURNING /i', $sql));
}
The really safest way is:
Add a column code in your table.
Create a code before batch inserting [id session user]-[current time stamp] should be enough.
Batch insert all elements with that code into code column
Query a Select searching for that code, you will have all elements inserted at batch insert
Here is an efficient method:
Since your records have result ID's, I'm going to assume they also auto-increment.
If this is the case, you do this and still use insert_batch.
Here is what you do:
You take a count of the items you are inserting:
$count = count($data);
Run your batch insert:
$this->db->insert_batch('student', $data);
Get the first inserted ID of your batch:
$first_id = $this->db->insert_id();
Add the count (minus 1) to your insert ID to get the last records ID.
$last_id = $first_id + ($count-1);
There you go! You now have first and last ID's of your inserted records, and by extension, everything else in between.
function insertStudent(){
$data[] = array('firstname'=>'firstname1','lastname'=>'lastname1');
$data[] = array('firstname'=>'firstname2','lastname'=>'lastname2');
$result = $this->db->insert_batch( 'student', $data );
$a= $this->db->get('student');
$data = $a->result_array();
echo($data[0]['id']);
}
Hope So this will help you.

Data returned from an ajax call has the names of the database columns

I need some help with returned data from Ajax call...
I'm using CodeIgniter framework for PHP and when I do an Ajax call, the response contains the column names of my database table...
Look:
How do I change this?
Below is my code:
Controller:
$logStuff = $this->vpn_model->get_vpn_log();
echo json_encode($logStuff);
Model:
$this->db->select('connection_logevent, connection_logdate, connection_logip');
$this->db->from("connection_log");
return $this->db->get()->result();
I think if you return as ARRAY they can also known the column name. You can try this code then no one will know the column name if they parse the JSON:
$this->db->select('connection_logevent AS con_log_evnt, connection_logdate AS AS con_log_date, connection_logip AS con_log_ip');
$this->db->from("connection_log");
return $this->db->get()->result();
So you need to iterate over you results and repack its data to new array:
$data = array();
foreach ($results as $row) {
$data[] = array($row['connection_logevent'], $row['connection_logdate'], $row['connection_logip']);
}
If you want to have it in the same structure but with different keys, you can also use aliases in query.
$this->db->select('connection_logevent AS field1, connection_logdate AS field2, connection_logip AS field3');
$this->db->from("connection_log");
return $this->db->get()->result();
Check documentation.

Update query insert zero in table in ci

Update query insert zero in table everytime.
I have prtinted the query.From phpmyadmin the lastquery working fine.updated with same value
But when db active query then it has updating 0.
tbl_setitbl
set_id(primary key)
reference(text)`
Here is my code.
public function edit_set($id,$setvalue)
{
$data = array('reference' => $setvalue);
$this->db->where('set_id', $id);
$this->db->update('tbl_setitbl', $data);
if($this->db->affected_rows())
return true;
else
return false;
}
I have tried this code also.
$this->db->where('set_id', $id);
$this->db->update('tbl_setitbl', array('reference' => $setvalue));
echo $this->db->last_query();
UPDATE tbl_setitbl SET reference = 'hhhhhhhh' WHERE set_id = 1
Sorry every one...
Get solved
actually the problem is in controller.
query has run two times as redirection has not doing properly
see the result by using $this->db->last_query() then verify the sql code if it is similiar to the sql code that you have tried in phpmyadmin

Query resulting empty in Codeigniter

I have piece of PHP code written in Codeigniter framework that returns nothing (an empty set).
Have a look at it and tell me what is wrong with it.
function sbsn($serial){
$this->db->select('asset_types.name as type_name,asset_brands.name as brand_name');
$this->db->from('asset_types,asset_brands,assets');
$this->db->where('assets.type_code','asset_types.code');
$this->db->where('assets.brand_code','asset_brands.code');
$this->db->where('serial_no',$serial);
$result = $this->db->get();
return $result;
}
hi with db get you are only getting result set not record to get results from result you have to the following thing
$this->db->get()->result(); // will return an array of objects
$this->db->get()->result_array(); //will return result in pure array
$this->db->get()->row() // just single row as object
$this->db->get()->row_array() // just single row as array
you can use $result to perform the same above things
$result->result();
$result->row();
for more information read generating result user guide
change:
$result = $this->db->get();
to
$result = $this->db->get()->row_array(); //to get single row
//OR
$result = $this->db->get()->result_array(); //to get multiple rows
OR try using JOIN, like
$this->db->select('asset_types.name as type_name,asset_brands.name as brand_name');
$this->db->from('assets');
$this->db->join('asset_types', 'asset_types.code = assets.type_code', 'left');
$this->db->join('asset_brands', 'asset_brands.code = assets.brand_code', 'left');
$this->db->where('assets.serial_no',$serial);
$result = $this->db->get()->result_array();
return $result;
The problem can be easily solved by joining the tables in the following way.
$this->db->select('at.name as type_name,ab.name as brand_name');
$this->db->from('asset_types as at,asset_brands as ab');
$this->db->join('assets as a', 'a.type_code = at.code and a.brand_code as ab.code');
$this->db->where('a.serial_no',$serial);
$result = $this->db->get()->result_array();
return $result;

Codeigniter return result and row

In the Model class I use return $query->row(); to return single rows and return $query->result(); when returning multiple rows.
On a single page I have to return single rows and multiple rows from 2 separate tables.
Table users contains general information like the user name, full name, and email address.
Table user_links contains links submitted by the respective user and has multiple rows for each user.
My query
$this->db->select('*');
$this->db->from('users');
$this->db->join('user_links', "user_links.user_id = users.user_id");
$this->db->where('users.user_id', $user_id);
$this->db->where('user_links.user_id', $user_id);
$query = $this->db->get();
return $query->row();
In my controller I load the query in my view by
$data['row'] = $this->User_model->user_read($user_id);,
$user_id being the 3rd URL segment containing the unique user id.
Finally, in my view I retrieve rows by echo $row->first_name;
This works for single rows but how can I create a foreach loop for user links? The goal is to avoid loops for single rows and use them just for retrieving multiple rows.
This is psuedo code but you can probobly do something like this
$this->db->select('*');
$this->db->from('users');
$this->db->join('user_links', "user_links.user_id = users.user_id");
$this->db->where('users.user_id', $user_id);
$this->db->where('user_links.user_id', $user_id);
$query = $this->db->get();
if ($query->num_rows() == 1)
{
return $query->row();
}
elseif ($query->num_rows() > 1)
{
return $query->result_array(); //This returns an array of results which you can whatever you need with it
}
else
{
//Add some logic to handle if there are zero results
}
If I understand your question correctly, you want to get both the user data as well as user_links data with a single query while avoiding iterating through it to get the user's data. While this may be possible using result_array, I would advise against it since you will get 0 results when there are no entries in user_links for that particular user.
My suggestion is that you use two queries, one to get the user from the user table, another to get user's links from user_links table. This will also help you avoid joins.
Hard to tell what you want.
You want a function that checks if you're passing a row() or a result() and treat them accordingly.
In my opinion your code would be easier to read (and maintain) if you just passed everything as a result(). And do check if the set is empty to show a nice message to the user.
Sounds like you're looking for some of the other features already provided by CI's Active Record: http://codeigniter.com/user_guide/database/results.html
For what you are doing it sounds like using the built in function result_array would work. You use it like so:
TAKEN FROM LINK ABOVE
$query = $this->db->query("YOUR QUERY");
foreach ($query->result_array() as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['body'];
}
You can just replace this line:
$this->db->join('user_links', "user_links.user_id = users.user_id");
With this one:
$this->db->join('user_links', "user_links.user_id = users.user_id", 'left outer');
Join in codeigniter uses INNER JOIN by default but in some cases if there is no data in user_links it returns nothing so u can use LEFT JOIN in your frame work CI it used like i mentioned above.

Categories