MySQL QUOTE() function

QUOTE() function

MySQL QUOTE() produces a string which is a properly escaped data value in an SQL statement, out of a user supplied by the string as an argument.

The function achieves this by enclosing the string with single quotes, and by preceding each single quote, backslash, ASCII NUL and control-Z with a backslash.

If the string passed as argument is NULL, the function returns a word NULL.




Name Description
str A string.

Syntax Diagram:

MySQL QUOTE() Function - Syntax Diagram

MySQL Version: 5.6

Video Presentation:

Example of MySQL QUOTE() function

The following MySQL statement returns a string 'w3re\'source'.


SELECT QUOTE('w3re''source');

Sample Output:

mysql> SELECT QUOTE('w3re''source');
| QUOTE('w3re''source') |
| 'w3re\'source'        | 
1 row in set (0.03 sec)

Example of MySQL QUOTE() function using table

The following statement returns the pub_name and pub_name enclosed with a single quote for those publishers who belong to the ‘USA’.


SELECT pub_name, QUOTE(pub_name)
FROM publisher 
WHERE country='USA'; 

Sample table: publisher

Sample Output:

mysql> SELECT pub_name, QUOTE(pub_name)
    -> FROM publisher 
    -> WHERE country='USA';
| pub_name                 | QUOTE(pub_name)            |
| Jex Max Publication      | 'Jex Max Publication'      | 
| Mountain Publication     | 'Mountain Publication'     | 
| Summer Night Publication | 'Summer Night Publication' | 
3 rows in set (0.04 sec)

PHP script:

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>example-quote-function - php mysql examples | w3resource</title>
<meta name="description" content="example-quote-function - php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>A list of Publishers those who belong to USA. Right column shows the name of the publisher enclosed with single quotes:</h2>
<table class='table table-bordered'>
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT pub_name, QUOTE(pub_name)
FROM publisher
WHERE country="USA"') as $row) {
echo "<tr>";
echo "<td>" . $row['pub_name'] . "</td>";
echo "<td>" . $row['QUOTE(pub_name)'] . "</td>";
echo "</tr>";

View the example in browser

JSP script:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
try {
String Host = "jdbc:mysql://localhost:3306/w3resour_bookinfo";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
connection = DriverManager.getConnection(Host, "root", "datasoft123");
statement = connection.createStatement();
String Data ="SELECT pub_name, QUOTE(pub_name) FROM publisher WHERE country='USA'";
rs = statement.executeQuery(Data);
<TABLE border="1">
<tr width="10" bgcolor="#9979">
while (rs.next()) {
<%   }    %>
} catch (Exception ex) {
out.println("Cant connect to database.");

Online Practice Editor:

All String Functions

MySQL String Functions, slide presentation

Previous: POSITION

Follow us on Facebook and Twitter for latest update.