You do not need to use quotes around some data types in SQL, integers being one of those types. That is why the first block of code works. It is a perfectly valid SQL statement.
You do need to quote strings in SQL. That is why your second block of code does not work. The correct form would be:
$all_pages = $wpdb->get_results(
'SELECT post_title, guid FROM wp_11_posts WHERE post_title = "Office Home"',
OBJECT
);
If your data is in any way subject to user input you should use prepare()
.
$string = "Office Home"; // if coming from $_POST or $_GET data or other user manipulatable sources
$sql="SELECT post_title, guid FROM wp_11_posts WHERE post_title = %s";
$sql = $wpdb->prepare($sql,$string);
$all_pages = $wpdb->get_results($sql,OBJECT);
prepare()
will add the quotes as needed based on the data type indicated by the placeholder– %s
.
Related Posts:
- MySQL Error: : ‘Access denied for user ‘root’@’localhost’
- MySQL Error: : ‘Access denied for user ‘root’@’localhost’
- What’s the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN? [duplicate]
- Cannot delete or update a parent row: a foreign key constraint fails
- Cannot delete or update a parent row: a foreign key constraint fails
- Rename a column in MySQL
- Cannot delete or update a parent row: a foreign key constraint fails
- How do I restore a dump file from mysqldump?
- How do I import an SQL file using the command line in MySQL?
- #1273 – Unknown collation: ‘utf8mb4_unicode_520_ci’
- What does SQL Select symbol || mean?
- What is the definition of cardinality in SQL
- When to use SELECT … FOR UPDATE?
- SQL Query Where Field DOES NOT Contain $x
- Column ‘user_id’ in field list is ambiguous
- How to join two tables by multiple columns in SQL?
- mysql Foreign key constraint is incorrectly formed error
- Selecting data from two different tables without using joins
- 1052: Column ‘id’ in field list is ambiguous
- 1052: Column ‘id’ in field list is ambiguous
- MySQL: Invalid use of group function
- difference between primary key and unique key
- Getting “Lock wait timeout exceeded; try restarting transaction” even though I’m not using a transaction
- Simple Random Samples from a Sql database
- ERROR 1452: Cannot add or update a child row: a foreign key constraint fails
- What does SQL clause “GROUP BY 1” mean?
- SQL ZOO List each continent and the name of the country that comes first alphabetically
- How can I prevent SQL injection in PHP?
- Fatal error: Please read “Security” section of the manual to find out how to run mysqld as root
- Generating a random & unique 8 character string using MySQL
- SQL select only rows with max value on a column
- Unknown Column In Where Clause
- SQL statement not working – “Operand type clash: date is incompatible with int’
- Convert partially non-numeric text into number in MySQL query
- Repair all tables in one go
- How can I search (case-insensitive) in a column using LIKE wildcard?
- #1062 – Duplicate entry for key ‘PRIMARY’
- How to do a batch insert in MySQL
- ERROR 1115 (42000): Unknown character set: ‘utf8mb4’
- SQL – IF EXISTS UPDATE ELSE INSERT INTO
- Remove Primary Key in MySQL
- Is merge statement available in MySQL
- Error: Duplicate entry ‘0’ for key ‘PRIMARY’
- How to get the count of each distinct value in a column?
- How do you properly prepare a %LIKE% SQL statement?
- What SQL Query to do a simple find and replace
- Optimize WordPress Query that take 5 seconds to execute
- Scanning Database for malicious Data
- wpdb->prepare function remove single quote for %s in SQL statment
- “#1067 – Invalid default value for ‘post_date'” when trying to reset AI after backup
- Extending the database
- How to find meta entries with their posts missing via SQL?
- SQL Database, repeated tables?
- Code to remove authors with no posts connected to them
- SQL command to convert all tags in lowercase?
- Which is better and faster ? WordPress Queries or SQL Query
- SQL Query in WordPress – Getting Impossible WHERE error
- How to get INSERT errors from $wpdb?
- MySQL query for taxonomy-meta
- How can I write a formal sql query to get data from a custom table
- latency problems with wordpress install (from SQL import)
- Data is not constatntly loading from custom wordpress table
- Lock wait timeout exceeded; try restarting transaction
- Find locations of all featured images of draft posts via SQL
- SQL query to change custom field in WordPress database
- How to show result of sql query in a page?
- update_option returns false in AJAX, how to debug?
- Improving performance of SQL sub-queries in WordPress
- An SQL query to find orphaned image attachments and their meta
- WPDB delivers wrong results from complex queries
- How to change link in all posts?
- Display MySQL as part of a Page
- $wpdb post type and term query only works when there are no dashes or spaces in the term slug and title
- SQL error on restoring database
- optimise SQL wordpress call
- Query sql for truncate post_content in wp_posts table
- Woocommerce: Grab total revenue of a product over all orders
- Delete posts with word count less than x number of words
- SQL – Pull products of particular category from a remote WordPress woocommerce db
- SQL command to export post_content from wp_posts using phpMyAdmin
- SQL get last entry of a specific gravity form
- Is there an equivalent of MySQL’s SHOW CREATE TABLE in Postgres?
- Possible SQL injection. How to locate and fix?
- Unknown column in ‘field list’ error on MySQL Update query
- Cannot add or update a child row: a foreign key constraint fails
- What’s the difference between VARCHAR and CHAR?
- ERROR 2003 (HY000): Can’t connect to MySQL server on localhost (10061)
- #1055 – Expression of SELECT list is not in GROUP BY clause and contains nonaggregated column this is incompatible with sql_mode=only_full_group_by
- How to export a mysql database using Command Prompt?
- Multipart/formatted MySQL query problem
- Universal problem: first request after ~25 second inactivity always slower (~1 second) than subsequent requests (~1/10sec)
- Backing up and restoring multiple sites efficiently
- Are these WordPress tables safe to add indexes to?
- WordPress doesn’t save changes some times
- How I can change the condition or compare operator for WP_Query in pre_get_posts
- Custom SELECT Query not returning the_title and the_permalink
- Memcaching recurring SQL Queries
- How to install WordPress from hosting service with mysql database from Compute Engine GCP?
- Update WooCommerce stock status with SQL query if stock value is greater than 0
- SELECT statement wrong?