Creating Custom Database Tables plugin or theme using WordPress

Creating Custom Database Tables plugin or theme using WordPress

In this Post We Will Explain About is Creating Custom Database Tables plugin or theme using WordPress With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to how to fetch data from database in wordpress pluginExample

In this post we will show you Best way to implement Creating Custom Database Tables for Your WordPress Plugins, hear for Connecting your plugin to the WordPress Databasewith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Table Names into the SQL Query

global $wpdb;
$response = $wpdb->get_responses('SELECT * FROM ' . $wpdb->prefix . 'posts LIMIT 25');

$wpdb->posts simple will correspond to wordpress wp_posts table
$wpdb->postmeta simple will correspond to wordpress wp_postmeta table
$wpdb->users simple will correspond to wordpress wp_users table

global $wpdb;
$response = $wpdb->get_responses('SELECT * FROM ' . $wpdb->posts . ' LIMIT 25');

Use Specific Helper Methods for Database Operations

$global wpdb;

$postId    = $_POST['post_id']; // Get P
$metakeyData   = $_POST['meta_key'];
$metaDatavalue = $_POST['meta_value'];

$wpdb->query("INSERT INTO  $wpdb->postmeta
	( post_id, meta_key, meta_value )
	VALUES ( $postId, $metakeyData, $metaDatavalue )"
);


$global wpdb;

$postId    = $_POST['post_id'];
$metakeyData   = $_POST['meta_key'];
$metaDatavalue = $_POST['meta_value'];

$wpdb->insert(
	$wpdb->postmeta,
	array(
		'post_id'    => $_POST['post_id'],
		'meta_key'   => $_POST['meta_key'],
		'meta_value' => $_POST['meta_value']
	)
);

Properly Debugging Database Queries

To turn on the all the error reporting feature And to all the turn it off:

$wpdb->show_errors();

$wpdb->hide_errors();

or

$wpdb->print_error();

To retrieve this data

print_r( $wpdb->queries );

The prepare statement method supports both all the syntax of sprintf and vsprintf using wordpress.
%s for string using WordPress
%d for integer using WordPress
%f for float using WordPress

Connecting to Separate Databases

$mydb = new wpdb( 'yourusername', 'yourpass', 'createmy_database', 'severhost' );
$mydb->query('DELETE FROM your external WHERE id = 1');
$wpdb->select('my_simple_database');

Using Custom Database Tables

register_activation_hook( __FILE__, 'prefix_create_table' );

 function prefix_create_table() {
        global $wpdb;

        $charset_collate = $wpdb->get_charset_collate();

        $my_query = "CREATE TABLE my_custom_table (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            client_name varchar(255) NOT NULL,
            post_name varchar(255) NOT NULL,
            age varchar(255) NOT NULL,
            UNIQUE KEY id (id)
        ) $charset_collate;";

        if ( ! function_exists('dbDelta') ) {
            require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        }

        dbDelta( $my_query );
    }

Updating the Table Schema

add_action( 'plugin_loaded', 'prefix_update_table' );

Example

I hope you have Got What is How To Make Connection To WordPress Data Base In A Plugin And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.

Leave a Comment