I want to create migration for updating table. So I want to check if columns exist or not in db table before executing to remove error of duplicate column. Is there any to check using db forge in CodeIgniter? Thank you for help.
Check if multiple column exist in table using CodeIgniter database forge
270 views Asked by Bijay Shrestha At
2
There are 2 answers
0
Devart
On
To check the presence of several columns in a table with CodeIgniter Database Forge, use the below code:
$this->load->dbforge();
$table_name = 'your_table_name';
$columns = array('column1', 'column2', 'column3');
$table_fields = $this->db->list_fields($table_name);
$missing_columns = array_diff($columns, $table_fields);
if (!empty($missing_columns)) {
echo ""The following columns are missing: "" . implode(', ', $missing_columns);
} else {
echo ""All columns exist in the table."";
}
Note the following prerequisites:
• Download the dbforge library with the help of the $this→load→dbforge() function.
• Specify the name of the table you want to check for the presence of columns using $table_name
• Create the data set for the column names you want to check using $columns
• fetch the data set of all table fields using $table_fields = $this→db→list_fields($table_name);
• Apply array_diff() to compare the $columns data set to the $table_fields data set. This way you can check the missing columns
If the columns are missing, the output message will define which exactly columns are absent. If all columns are present in the table, the output message will state all columns existing.
Related Questions in PHP
- How to add the dynamic new rows from my registration form in my database?
- Issue in payment form gateway
- How to create a facet for WP gridbuilder that displays both parent and child custom fields?
- Function in anonymous Laravel Blade component
- How to change woocomerce or full wordpress currency with value from USD to AUD
- General questions about creating a custom theme Moodle CMS
- How to add logging to an abstract class in php
- error 500 on IIS FastCGI but no clue despite multiple error loggings activated
- Composer installation fails and reverts ./composer.json and ./composer.lock to original content
- How to isolate PHP apps from each other on a local machine(Windows or Linux)?
- Laravel: Using belongsToMany relationship with MongoDB
- window.location.href redirects but is causing problems on the webpage
- Key provided is shorter than 256 bits, only 64 bits provided
- Laravel's whereBetween method not working with two timestamps
- Implementing UUID as primary key in Laravel intermediate table
Related Questions in SQL
- SQL schema for a fill-in-the-blank exercise
- Hibernate: JOIN inheritance question - why the need for two left joins
- What's supposed to be the problem in this query?
- Compare fields in two tables
- How to change woocomerce or full wordpress currency with value from USD to AUD
- Dynamic query creation with Array like implementation
- SQL query to get student enrolled in this month in a course - Moodle
- SQL LAG() function returning 0 for every row despite available previous rows
- Convert C# DateTime.Ticks to Bigquery DateTime Format
- Use row values from another table to select them as columns and establish relations between them (pivot table)
- SQL: Generate combination table based on source and destination column from same table
- how to use system's environnement variables in sql script
- PHP fetchAll on JOIN
- Multitable joining in Sql
- How to display name starting from 'z' by using BETWEEN cmd only?
Related Questions in DATABASE
- How to add the dynamic new rows from my registration form in my database?
- How to store a date/time in sqlite (or something similar to a date)
- Problem with add new attribute in table with BOTO3 on python
- When an E-R attribute should be perceived as a relationship attribute or as an entity set attribute?
- SQLAlchemy: efficient relationship loading in 3-way many-to-many relationship
- Cannot connect to Postgres Database when running Quarkus Tests with Gitlab ci
- Local or remote database with react-native?
- I want to edit a specific row in database
- How to enter data in mongodb array at specific position such that if there is only 2 data in array and I want to insert at 5, then rest data is null
- Open Web Library
- database login.py and register.py error showing 404 file not found and doesn't work
- SQL71561: SqlComputedColumn: When column selected
- Liquibase as SaaS To Configure Multiple Database as Dynamic
- Updated max input vars but table still shows error
- Spring does not map set of roles
Related Questions in CODEIGNITER
- Overlay text on image not renedered correctly when using custom hindi font with GD library functions
- PHP CodeIGniter 3
- domPdf is showing blank page
- Is there a way to create connection pooling in codeigniter 3 using custom mysqli driver?
- Codeigniter 4 - MySQL Duplicate Entry exception not working
- How can I reconfigure CodeIgniter (v4) to serve with a new sub-directory path (via nginx)?
- How to logout with public function
- How to autoload a user-defined class in CI4?
- passing object to a view within a foreach loop in CodeIgniter
- Function in controller does not receive data from ajax on button [SOLVED]
- Create REST API Endpoints from an Existing PHP CodeIgniter Project
- Message: Invalid argument supplied for foreach() while using foreach
- CodeIgniter 4 migration fails but the equivalent MySQL query does not
- CodeIgniter 4 with hmvc
- Zend Barcode label distance ajustment
Related Questions in DBFORGE
- Cannot add or update child row, foreign key constraint fails, Calling stored procedure from another procedure
- Xampp not connecting to DBForge what to do?
- Character encoding problem when importing SQL with DBForge and PowerShell into MySQL
- Managing incremental DB migrations with environment hotfixes (Codeigniter 3)
- Is there a way to delete data in a DB1 only that is equal to the data in DB2 with DbForge Data Compare for Oracle?
- dbForge Studio for MySql - wrong key format error
- MySQL User Can't View, Edit or Debug Stored Procedures (Amazon RDS)
- Codeigniter 3 DBFORGE, #1064 -You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
- How to continuous deploy EF Migrations using Jenkins?
- Single MySQL Database Migration Between Amazon RDS With Same Specifications
- An item with the same key has already been added - mscorlib
- Mysql Database Stored Procedure Return after Drop
- Check if multiple column exist in table using CodeIgniter database forge
- MySQL Continue Error Handler for missing table when calling PREPARE
- How to remove encryption from all objects in SQL Server?
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
ALTER TABLE table_name DROP COLUMN IF EXISTS col1, COLUMN IF EXISTS col2; dbforge used in Codeigniter to add or modify the tables.
$this->dbforge->add_column('table_name', $fields);$this->dbforge->drop_column('table_name','column_name');