str_replace and SQL Server queries with PHP

I used to do ASP (vbscript) Programming, and have recently tried to start using PHP. I used to have a function in ASP that allowed to to replace ' with ''.

This was used for queries with a SQL Server database. I was just wondering if it is as easy to use something like this for PHP. I am aware of addslashes and stripslashes but it doesn't really serve the exact purpose I want.

Say I had a simple query like:

$dbTABLE = "Table_Name";
$query_sql = sprintf("UPDATE %s SET DataText = ('%s') WHERE PageID = '%d'",
$dbTABLE,
$PageHTML,
$PageID);

Is there a way to wrap it in something like str_replace to tell it that all ' should be replaced with ''?

I know I could search for it with a SQL Server query, but it needs to be before the data from a textarea is put into the database.

Answers


Yes, you can use the str_replace function:

str_replace("'", "''", "Neil O'Brien")

The example code you posted would look like:

$dbTABLE = "Table_Name";
$query_sql = sprintf("UPDATE [%s] SET DataText = '%s' WHERE PageID = %d",
$dbTABLE,
str_replace("'", "''", $PageHTML),
$PageID);

I suggest using some kind of library instead of building queries yourself.


Need Your Help

Debugging Webkit in Visual Studio

visual-studio-2008 webkit

I am able to build webkit on windows7 under Visual Studio 2008..But for the purpose of debugging when I am putting breakpoints in source code, any of the breakpoints are not hitting. I am able to

Sort columns based on the order inside SQL in function

sql sql-server tsql

i have a query like the one below and i want records to be returned in the same order as the id's specified inside the 'in' function.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.