Skip to content
-
Subscribe to our newsletter & never miss our best posts. Subscribe Now!
Technical Personnal Blog

Technical Personnal Blog

Technical Personnal Blog

Technical Personnal Blog

  • Tech
  • Blog
  • Open Source
  • VEGAS
  • CARD
  • Favorite Link
  • Shop
  • Cart
  • Checkout
  • My account
  • Tech
  • Blog
  • Open Source
  • VEGAS
  • CARD
  • Favorite Link
  • Shop
  • Cart
  • Checkout
  • My account
Close

Search

  • https://www.facebook.com/
  • https://twitter.com/
  • https://t.me/
  • https://www.instagram.com/
  • https://youtube.com/
Subscribe
Tech

Use the PHP addslashes function to fix SQL INSERT problems

By techsupport
August 4, 2018 2 Min Read
Comments Off on Use the PHP addslashes function to fix SQL INSERT problems

More

PHP MySQL FAQ: Can you share an example of how to use the PHP addslashes function when creating a SQL INSERT statement?

When you’re inserting records into a database table with plain PHP (as opposed to using a PHP framework), some of the fields you’re inserting may have characters in them that will cause problems during the SQL INSERT process. Characters that will cause problems on database INSERT statements include the single quote, double quote, backslash, and NULcharacters.

I ran into this problem yesterday when I wrote a SQL batch-insert program in PHP. This program reads thousands of records from a file, and inserts them into a MySQL database. The problem I ran into is that two of the fields had these offending characters in them, and my SQL INSERT statements kept blowing up. That’s when I found what I needed: The PHP addslashes function.

The PHP addslashes function

Here’s a nice description of the PHP addslashes function from the addslashes manual page:

Returns a string with backslashes before characters that need to be quoted in database queries etc. These characters are single quote (‘), double quote (“), backslash (\) and NUL (the NULL byte).

An example use of addslashes() is when you’re entering data into a database. For example, to insert the name O’reilly into a database, you will need to escape it. It’s highly recommeneded to use DBMS specific escape function (e.g. mysqli_real_escape_string() for MySQL or pg_escape_string() for PostgreSQL), but if the DBMS you’re using does’t have an escape function and the DBMS uses \ to escape special chars, you can use this function. This would only be to get the data into the database, the extra \ will not be inserted. Having the PHP directive magic_quotes_sybase set to on will mean ‘ is instead escaped with another ‘.

A PHP addslashes function example

Here’s a snippet of code that shows how I used the PHP addslashes function with my SQL INSERT statement. As you can see from the code below I first convert my strings ($uri and $title) with the addslashes function, and then run my SQL INSERT statement:

# titles and malformed URIs can have all sorts of characters in them; escape them
$uri = addslashes($uri);
$title = addslashes($title);

// do the mysql insert
$result = mysql_query(" INSERT INTO logfile_records (uri, title, date) " .
                      " VALUES ('$uri', '$title', '$the_date')");

// die if the insert doesn't succeed
if (!$result) {
  die('mysql query error: ' . mysql_error());
}

Before using this PHP addslashes function, my queries failed often, but after adding it in, they all worked great.

The PHP mysqli_real_escape_string function

Unfortunately I didn’t read the addslashes function manual page until after my program was working, but since I’m using a MySQL database, it looks like it’s preferable to use the PHP mysqli_real_escape_string function instead of the addslashes function, but for my simple batch-processing program, the PHP addslashes function has worked fine on hundreds of thousands of records, so I’m just going to stick with it for now. I’ll try the mysqli_real_escape_string function on my next program and report the results back here.

Tags:

php
Author

techsupport

Follow Me
Other Articles
Previous

NO Cache option

Next

How to Use Google’s SMTP Service with Opencart

Categories

  • Android
  • Blog
  • Favorite Link
  • linux
  • Open Source
  • opencart
  • social
  • Tech
  • Uncategorized

Archives

  • January 2025
  • March 2024
  • August 2023
  • March 2023
  • February 2023
  • November 2021
  • August 2021
  • April 2021
  • March 2021
  • December 2020
  • October 2020
  • September 2020
  • July 2020
  • June 2020
  • May 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • September 2019
  • August 2019
  • June 2019
  • October 2018
  • August 2018
  • May 2018
  • April 2018
  • March 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • June 2017
  • March 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • January 2016
  • December 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
Copyright 2026 — Technical Personnal Blog. All rights reserved. Blogsy WordPress Theme