Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
How To Quote Text Values in SQL Statements
How To Quote Text Values in SQL Statements? - PHP Script Tips - Working with MySQL Database
✍: FYIcenter.com
Text values in SQL statements should be quoted with single quotes ('). If the text value contains a single quote ('), it should be protected by replacing it with two single quotes (''). In SQL language syntax, two single quotes represents one single quote in string literals. The tutorial exercise below shows you two INSERT statements. The first one will fail, because it has an un-protected single quote. The second one will be ok, because a str_replace() is used to replace (') with (''):
<?php
include "mysql_connection.php";
$notes = "It's a search engine!";
$sql = "INSERT INTO fyi_links (id, url, notes) VALUES ("
. " 201, 'www.google.com', '".$notes."')";
if (mysql_query($sql, $con)) {
print(mysql_affected_rows() . " rows inserted.\n");
} else {
print("SQL statement failed.\n");
}
$notes = "It's another search engine!";
$notes = str_replace("'", "''", $notes);
$sql = "INSERT INTO fyi_links (id, url, notes) VALUES ("
. " 202, 'www.yahoo.com', '".$notes."')";
if (mysql_query($sql, $con)) {
print(mysql_affected_rows() . " rows inserted.\n");
} else {
print("SQL statement failed.\n");
}
mysql_close($con);
?>
If you run this script, you will get something like this:
SQL statement failed. 1 rows inserted.
2007-04-19, 4938👍, 0💬
Popular Posts:
How does multi-threading take place on a computer with a single CPU? The operating system's task sch...
Can Sub Procedure/Function Be Called Recursively? - Oracle DBA FAQ - Creating Your Own PL/SQL Proced...
What is the output of printf("%d")? 1. When we write printf("%d",x); this means compiler will print ...
If locking is not implemented what issues can occur? IFollowing are the problems that occur if you d...
Can you prevent a class from overriding ? If you define a class as “Sealed” in C# and “NotInheritab...