What is the syntax for inserting data from one table to another table, using codeigniter active record syntax? I tried the usual mysqli query and it works, but I want to use CodeIgniter Active Record syntax for consistency.
I tried playing with these CodeIgniter Active Record queries but still no luck:
function insert_into()
{
$this->db->insert('table1');
$this->db->set('to_column');
$this->db->select('from_column');
$this->db->from('table2');
}
I think the best thing to accomplish that is fetching the data you want from one table using the get method, then using one of the query results grabber functions (like result() ), iterate over the rows one by one using the insert() method.
Putting this in code:
$query = $this->db->get('table1');
foreach ($query->result() as $row) {
$this->db->insert('table2',$row);
}
Of course, i suppose that table1 has exactly th same structure as table2 (the same column names and data types for each column). If that is not the case, you will have to map the columns from one table to the another using assignments, but if that is the case your code will be more wide.
Copies $source_table into $archive_table:
if(($this->db->table_exists($source_table) && ($this->db->table_exists($archive_table)){
if ($this->db->query("CREATE TABLE $archive_table LIKE $source_table")){
if($this->db->query("INSERT $archive_table SELECT * FROM $source_table")){
echo 'copied ok';
}
}
}
Neater than looping over the entire result set and inserting one row at a time.
Related
I am using the pecee-pixie library.
I am trying to update a column from one table based on values from another table however when i ran my query i simply updated the values with a text table2.values rather with the actual values.
This is my query:
$table1 = $query_builder->table('table1');
$table1
->leftJoin("table2", "table1.id", "=", "table2.join_id")
->whereNotNull("table2.values")
->update(['table1.values' => 'table2.values']);
I would mind using raw queries but i'd prefer if i didnt have to write the whole query raw. Anybody has any ideas?
I have been trying to figure out how to check if any of one column's items present in another column in Codeigniter....
I have Bike,Car,Bus in a column(Vehicles) in a table1...
I also have Bus,Helicopter,Ship,Car in a column(Interested) in table2..
How to check if any of table2 interested column's items present in table1 vehicles column...
I tried like this...
$query = $this->db->get('table2');
foreach($query->result() as $row)
{
$a = explode(',', $row->interested);
$this->db->where_in('Vehicles', $a);
$query = $this->db->get('table1');
foreach($query->result() as $row2)
{
echo $row2->ID;
}
}
Could it be done ?? Please Help me... Thanks in advance....
It is very hard to help you without seeing the tables and how you are relating one to the other (such as user_id).
What you should do is two queries. One query to get the data from table 1 of the vehicles you are interested in. Then run through the result set and create a simple array of those vehicles in php. Then do a second query using where_in to select all the rows that are in the chosen vehicles array.
Your method is bad because the number of db queries depends on the size of the result set of your first query. You might find your code trying to do hundreds of database queries.
If you show a diagram of your tables, I could write some example code for you.
Here are the docs for where_in: https://www.codeigniter.com/user_guide/database/query_builder.html#CI_DB_query_builder::where_in
I have a database that contains 4 relationship tables used to construct the content of a page:
content rel theme theme_meta
The rel table matches the contentID from the content table to the corresponding rel field of the theme table. theme_meta has a field called themeID that links it to the theme table.
So
When constructing a page at the moment I JOIN the content table to the del table, joining that to the theme table and that to the theme_meta table.
It gives me around 24 rows for each matched row of the content table.
I then use a some php foreach loops to restructure the results into multidimensional arrays per content row.
Is that efficient? Would it be faster and more efficient to make 2 calls to the database, one for content and one for theme. This would produce far fewer rows and be easier to work with but require a second call to the database.
As mentioned above, an approach that uses a single query is usually the best way (since database queries incur a lot of overhead).
Indeed, it sounds as though your alternative approach would loop over the results of one query (on the content table) each time calling some other query (on the other tables) to fetch the joined data: such an approach will prove very costly in the long-term and will not scale well.
Therefore, to assemble a multi-dimensional array from the data, you merely need sort the joined results accordingly and keep track of the last seen identifier as you loop over the resultset (in order to detect when one needs to traverse up a level within the resulting array):
$qry = $dbh->query('
SELECT *
FROM content
JOIN rel USING (contentID)
JOIN theme USING (rel)
JOIN theme_meta USING (themeID)
ORDER BY contentID
');
$arr = array();
$row = $qry->fetch();
while ($row) {
array_push($arr, array());
$cid = $row['contentID'];
do {
array_push(end($arr), $row);
} while ($row = $qry->fetch() and $row['contentID'] == $cid);
}
echo var_export($arr);
I would however caution that it is often unnecessarily costly to build such a PHP data structure from the results of a database query, as one can might be able to build and dispatch the requisite output whilst reading the resultset.
I'm currently trying to write a simple ORM with PHP and mysql. I want the orm class to be able to work with joined tables.
So here's my problem, the following code shows how I map the data the query yields into an array.
public function execute_query($db_connection)
{
$query = '';
foreach($this->sql_query as $query_part)
$query .= $query_part;
$result = $db_connection->query($query);
while($row = $result->fetch_assoc())
{
array_push($this->m_Data, $row);
}
}
db_connection is a mysqli object.
sql_query contains all the different query parts (e.g. sql_query['join'] etc.).
m_Data is the array that contains the data read from the db.
My specific problem now is when I'm using a join statement in my query this function will just override fields with the same name in my m_Data array. Also if I dont save the name's of the table the specific field data is coming from, I later can't update the tables with the same join statement.
tl,dr. I need to be able to not only save the table data like this: m_Data{ 'field_name' => 'value' } but I also need to save the table name the field is selected from. I could then save the data like this m_Data{ 'table_name.field_name' => 'value' } which enables me later to generate a query to update the joined tables successfully.
I cant seem to find any information on how to get the origin table name for each field I pull out of the result.
If it isnt possible with mysqli I'd much appreciate it if you point me in the right direction.
extra short problem statement:
I need to get a result set and read each row seperatly. For each row I need the following information for every field selected: field_name, table_name, value.
There must be a simple answer to this but I seem to be searching for the wrong keywords to find a solution.
I hope I've written this understandable enough.
Seems to me that you should store table column values in an object, so if you have a related table, the column values would be stored in a separate object - and so would not interfere with the values in your primary table.
In general you might work with the ORM this way:
// Make joined query
$rows = ...
foreach($rows as $row)
{
// $row just refers to the primary table
echo $row->id;
// You get a many:1 related table this way
echo $row->getRelatedRow()->value;
// You get a 1:many rows this way
$rows = $row->getOtherRelatedRows();
}
Depending on how you set up your query options, getting related data may or may not initiate further SELECTs to get the required data.
mysqli_result::fetch_fields has useful things:
http://www.php.net/manual/en/mysqli-result.fetch-fields.php
table
orgtable
field type
etc
I was wondering if anyone knew how to get the metadata info from a Zend_Db_Table_Rowset class when using joins in the query that produced that result set? It's easy when there are no joins involved.. for example:
foreach ($rowset as $row) {
$info = $row->getTable()->info(Zend_Db_Table_Abstract::METADATA);
Zend_Debug::dump($info); // outputs array of column info including data type
}
But when I do that to a row that came from a query using joins I just get the data from the main table I was selecting from..
Jose, what kind of metadada info do you need? Maybe there is an alternate way to achieve what you want. Since the joins are always done from one table (and you join it to others) I think you will always get the metadata info for the first one.
By any chance are you doing dynamic joins?