WordPress uses its own set of database tables, but during customization you may need to create your own table and insert data into it. WordPress provides the global $wpdb object for all database operations.
The example below inserts a row into a custom table named my_custom_table with id, name, and date columns.
<?php
global $wpdb;
// Register the custom table name on the $wpdb object
$wpdb->my_custom_table = 'my_custom_table';
$query = $wpdb->prepare("
INSERT INTO $wpdb->my_custom_table
(id, name, date)
VALUES ('', 'Neel', '$date')
");
$results = $wpdb->query($query);
?>
How It Works
global $wpdb— brings the WordPress database object into scope.$wpdb->my_custom_table = 'my_custom_table'— registers your table name as a property on$wpdb, respecting any WordPress table prefix automatically.$wpdb->prepare()— safely prepares the SQL statement (use%s,%dplaceholders for actual variables to prevent SQL injection).$wpdb->query()— executes the query and returns the number of rows affected.
Hope this tutorial is useful for you. Keep following PHP Tutorial for Beginners for more help.