What are some best practices for integrating a database with PHP in WordPress?
When integrating a database with PHP in WordPress, it is best practice to use the built-in WordPress functions for interacting with the database. This ensures compatibility with WordPress updates and security measures. Additionally, always sanitize user input to prevent SQL injection attacks.
global $wpdb;
// Insert data into the database
$wpdb->insert(
'wp_my_table',
array(
'column1' => 'value1',
'column2' => 'value2'
)
);
// Retrieve data from the database
$results = $wpdb->get_results(
"SELECT * FROM wp_my_table WHERE column1 = 'value1'"
);