![]() Using it ensures your queries will work regardless of the database prefix on the current website. This is actually how WordPress multi-site maintains many sub sites and the core site in one database. It also helps to keep separation between websites if the same database is used for multiple sites, provided the prefix is unique. Generally, it’s wise to use a database table prefix in WordPress as it will help mitigate compatibility issues with plugins and can obfuscate your full table names making it hard for attackers to inject functional SQL queries when attempting to attack your site. Note in the above example the use of $wpdb->prefix - this is a common prefix used for all core database tables in WordPress as well as any custom DB tables that use it to build the full table name. Hosted with ❤ by GitHub What is the $wpdb->prefix? The simplest SQL SELECT query is to simply return all data rows from a desired table as follows: Should enough people ask, however, I’ll add more to the article. I really just want to get you familiar with the basics and get you started. We’ll touch on using WHERE clauses in conjunction with AND, OR and NOT operators and also how to sort and order our data using ORDER BY, ASC, and DESC.Īt this stage, I won’t dive into joins or other topics beyond what I’ve mentioned here as there is plenty of information available online to show you how to take your basic SQL skills further - W3Schools is a great place to start. We’ll start by looking at basic SELECT queries then take a look at using the SQL LIKE operator for matching patterns (string starts with, ends with, contains, etc). As this is a beginner’s guide, we’re just going to look at the use cases that will apply to those of you using the ACF Custom Database Tables. We’re going to take a quick look at some simple SQL statements for selecting data from custom database tables. ![]() ![]() You can do a lot of damage in that environment so I can’t stress this enough - always develop/test in a non-production environment. Please avoid executing SQL statements immediately against your production database. To run them you either need to fire up MySQL on your command line or open up an SQL command area in a database client such as TablePlus, Sequel Pro, MySQL Workbench, or phpMyAdmin. We’ll then move on to using WordPress’ built in database access class to execute SQL statements and then dive into how to ensure your SQL statements remain safe from SQL injection attacks.Īll of the code examples you’ll see in the first section don’t have a PHP component as they are just SQL statements. We are going to start off with a brief rundown of the most common SQL statements you might use to retrieve data from your database - this section will be raw SQL and won’t run in PHP by itself. Note that this is aimed at fetching data from the database so if you are looking to update your tables via SQL statements, you should refer to ’s wpdb documentation which provides some example queries for tasks such as: inserting new rows, replacing existing rows, updating existing rows, and deleting existing rows. It’s not an extensive resource but will provide you with a good foundation to writing your own queries to suit your application’s requirements. The following information is presented as a beginner’s guide to writing SQL within the context of WordPress to query data. You may, however, be faced with situations where you need to write your own SQL in order to get the data you need - perhaps it’s more performant to use a custom SQL query or perhaps you are dealing with custom database tables, as may well be the case if you are using our ACF Custom Database Tables plugin to organise your Advanced Custom Fields data. For the most part, WordPress’ various query mechanisms such as WP_Query, WP_Term_Query, WP_User_Query, etc., save us the need to write our own SQL statements. There aren’t too many occasions in my career as a web developer where I’ve needed to query the core WordPress database tables directly using SQL. A beginner’s guide to using SQL to query the WordPress database ![]()
0 Comments
Leave a Reply. |