First, prepare()
allows you to specify two different types of data:
%s
for String Types%d
for Integer/Numeric Types
The reason is simple: MySQL only knows two (and a half) data types: Strings and Numerics (and Date/Time).
So this way you specify what type it is and make sure only that type gets trough.
Still it’s important to get rid of possibly malicious parts of data by properly sanitize your inputs: esc_*()
functions.
Here’s your example:
global $wpdb;
// The actual statement - in a readable way
$SQL = <<<SQL
SELECT COUNT(*)
FROM {$wpdb->prefix}h_transactions
WHERE transactionid = %d
SQL;
$preparedSQL = $wpdb->prepare( $SQL, esc_attr( $_POST['transactionID'] ) );
// Let's run it
$registered = $wpdb->get_var( $preparedSQL );
Note: Make sure that you use your real names. I changed some for readability.
Related Posts:
- How to use $wpdb to delete in a custom table
- get_results on large datasets
- Is there a (better) way to access $wpdb results?
- Using $wpdb generates DB error
- How do you use prepare when asking for a list of id’s
- how to execute different sql query in non-sanitized $wpdb->get_results function
- WPDB update row with != in where clause
- How to update records using $wpdb?
- Need help writing a $wpdb query
- show badge with count for pending items in custom post type
- WP Sql query multiple where clause
- WordPress SQL query – returning ‘true’ ‘false’ or ‘null’
- Modify the structure of data returned by $wpdb
- Syntax for $wpdb->prepare when searching in two columns
- How to display user_nicename and usermeta values by custom query in WordPress?
- Optimizing WordPress Queries – Removing Group By ID
- How can I combine one field using wpdb and group by?
- $wpdb->prepare with ON DUPLICATE KEY UPDATE
- how to use $wpdb->prepare to update a custom table
- WPDB Placeholders and second argument for prepared statements
- Increment integer field in database when WHERE needs to be dynamic [closed]
- Custom SQL query ORDER BY term_order
- Custom $wpdb returns unexpected time based results
- How to left join meta in queries [closed]
- wpdb->get_row is selecting the variable as a column name
- CREATE TABLE with dbDelta does not create table
- $wpdb query outputs php code instead of executing it
- wpdb query not working
- WordPress wpdb->insert returns int(0) => doesn’t insert anything, no errors!
- WPDB SQL Ignore `post_status` Parameter
- how to list all post that are in the custom taxonomy using $wpdb
- WPDB SQL query SELECT from category
- How to use WHERE NOT EXISTS query to avoid duplicate entry using $wpdb to save in custom table?
- Creating an Angular factory from custom database table
- wpdb->update update the entire table instead of one row
- How do I update post based on meta_key in another table?
- How to set up prepared query using IN statement
- Custom database query to validate data
- Alter the main search query to search posts by coauthor user name
- Creates only one table and not the other
- Protect custom form from SQL injection
- looking for a way to allow users to backup the plugin db data(save as)
- SQL Query to select post title & post ID from a particular category
- Get comments after specific date
- query using wpdb in wordpress gets me no result
- Get count of rows based if column exists in two different tables
- query_vars doesn’t return query string (trying to get data from $wpdb)
- How do you properly prepare a %LIKE% SQL statement?
- How to parse row results from $wpdb -> get_results
- Pagination with custom SQL query
- $wpdb->insert() and Values for Datetime Columns?
- Change post status based on meta value
- Speed optimization of $wpdb->get_results
- $wpdb is get_results escaped
- wpdb prepare: passing varible number of fields as second argument
- Wpdb query for comment meta for current post
- How to Modify this $wpdb query to accept an array of post statuses
- Select from wp_post and multiple meta_value from wp_postmeta
- WPDB Update using Conditional Arrays
- How to correctly pass values to wpdb->prepare()?
- $wpdb->get_results returns empty but value exists
- Problem displaying inserted form
- Get published posts and pages?
- WordPress (on AWS ubuntu) database Fails “Connection refused in .. /wp-includes/wp-db.php on line 1531”
- How to get entry by title by using wpdb
- $wpdb not being defined in function: Fatal error: Call to a member function query() on a non-object
- WordPress is trimming leading 0s
- WPDB SQL query with prepare() returning variable, not db value
- get_results not returning anything
- Save data from a checkbox to a wpdb array
- WP-PostRatings: list current user’s rated posts
- WPDB Join with custom table
- $wpdb->prepare affecting the query?
- Getting values from wpdb
- Why is this $wpdb query looping 5 times?
- wpdb query not returning results containing single quotes
- duplicate entries in database while using save_post or wp_insert_post
- Unknown column ‘siteurl’ in ‘where clause’ WPDB outside
- $wpdb->get_results(…) with $wpdb->prepare(…) returns empty array despite correct query
- Passing a SQL query to the WP Query
- Creating multiple tables with Plugin
- sort title descending with title with number
- Escaping a WPDB Object in One Shot
- Correct and secure way to access a custom SQL database in a custom PHP template file
- Create a stored procedure on plugin activation
- Get last element from wpdb as a string
- $wpdb query for price in custom field value
- Object Cache – Avoid db queries totally
- Update all fields of table with ON DUPLICATE KEY UPDATE command
- WordPress – wpdb query does not list same result as sql query
- What is the best practice to initialise $wpdb by loading wp-load.php?
- $wpdb->get_results in not an array
- adding a log row log table, when the draft post publish
- How to make iteration on wpdb->update or query statement?
- Querying multiple meta_keys in WordPress SQL query
- Custom WordPress SQL Query 4 posts per custom taxonomy
- Jeditable Plugin working as it should – scope issue?
- wpdb LIKE request shows all database data
- How do I change the datetime format from ( ‘y-m-d’ ) to ( ‘d m y’ ) [closed]
- Can’t send form data to wpdb when URL has query string