Skip to content
Security2 min read

What is SQL injection?

Short answer

SQL injection is a vulnerability that occurs when code puts user input straight into a database query, letting an attacker write their own SQL to read, change or delete data. The core defence is prepared statements (parameterised queries).

ArabyHost teamLast updated: ShareWhatsAppXLinkedIn
Contents
  1. Example: vulnerable code
  2. The fix: prepared statements
  3. Extra layers of protection
  4. On ArabyHost

SQL injection is one of the oldest web vulnerabilities and still one of the most dangerous, because the database holds everything: users, password hashes, orders, customer details. One badly written search field is enough to expose it.

Example: vulnerable code

$id = $_GET['id'];
$result = $db->query("SELECT * FROM products WHERE id = " . $id);

When a visitor opens product.php?id=5, it works as expected. But an attacker types something else instead of the number:

What the attacker puts in id Resulting query Effect
5 OR 1=1 ... WHERE id = 5 OR 1=1 Returns every product
5 UNION SELECT user_login, user_pass FROM wp_users Merges the users table into the result Exposes usernames and password hashes
5; DROP TABLE orders A second command after the first Deletes the orders table if the driver allows multiple statements

The fix: prepared statements

$stmt = $pdo->prepare('SELECT * FROM products WHERE id = ?');
$stmt->execute([$_GET['id']]);
$product = $stmt->fetch();

The query goes to the database first with a ? placeholder, and the value is sent separately. Whatever the attacker types is treated as a value, never as a command.

In popular systems:

  • WordPress: use $wpdb->prepare() with %d for numbers and %s for strings.
  • Laravel: Eloquent and the query builder bind values automatically, but beware of concatenating input into whereRaw or DB::raw.

Extra layers of protection

  1. Least privilege: the site’s database user usually doesn’t need permission to drop tables.
  2. Hide error messages from visitors, because they reveal your table structure to attackers.
  3. A web application firewall (WAF) to block known attempts.
  4. Keep plugins and libraries updated.

On ArabyHost

Imunify360 security on ArabyHost servers blocks many known injection patterns before they reach your site, and CageFS isolates every account so an attacker can’t reach other accounts’ databases. Fixing the code is still your job, and with daily JetBackup backups you can restore the database if it’s tampered with. For more, follow the protect your site from hacking guide.

FAQ

Isn't addslashes or input sanitising enough?

No. Hand-rolled escaping leaves gaps in edge cases such as unquoted numbers or character encodings. Prepared statements keep data separate from the command itself, which is the reliable fix.

Is WordPress vulnerable to SQL injection?

WordPress core uses wpdb->prepare, but badly written plugins and themes can contain the flaw. Keep plugins updated and delete the ones you don't use.

Does a WAF protect against SQL injection?

It blocks known patterns and lowers the risk, but an attacker may find a payload the rules don't match. A WAF is an extra layer; the real fix is in the code.

Useful? Share it with someone who needs it:WhatsAppXLinkedIn

Read next

Ready to put your idea online?

Fast, isolated hosting, support that understands you, and an AI assistant that fixes problems with you.