How to include a PHP variable inside a MySQL statement

How to include a PHP variable inside a MySQL statement

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

How to include a PHP variable inside a MySQL statement

You cannot simply paste a PHP variable directly into a SQL string because the database engine expects a literal value, not a code snippet to be evaluated. In your specific case, $type is a string containing 'testing', but when you write VALUES($type, ...), the resulting query sent to the server looks like this:

INSERT INTO contents (type, reporter, description) VALUES(testing, 'john', 'whatever')

The database interprets testing as an identifier or a reserved word depending on context, not as the string "testing". Furthermore, if $type contained user input, this approach would be an immediate security vulnerability. To make this work correctly and safely, you must separate the logic of data handling from the structure of the query.

The Problem with String Interpolation

The core issue is that SQL is not a programming language like PHP or Python; it does not support variable interpolation. When you use double quotes around your PHP string, the parser looks for variables to replace them, but standard SQL keywords don't work that way inside the query text. You are essentially trying to execute code inside a static command structure.

If you attempt to concatenate the variable without quotes, you risk syntax errors if the value is numeric versus string. If you wrap it in quotes manually—"VALUES(\"$type\", ...)"—you create a different problem: SQL Injection. An attacker could pass $type = "1; DROP TABLE contents--";, and your query would execute destructive commands instead of inserting data.

Using Prepared Statements

The industry standard solution, and the one you must use in modern PHP development, is prepared statements. This technique sends two distinct parts to the database: a query template with placeholders, and the actual parameters separately. The database validates the structure first, then binds the values safely without ever parsing them as SQL code.

Here is how you should refactor your original example using the legacy mysql_* functions (which are deprecated but relevant to your source context):

$type = 'testing';
$sql = "INSERT INTO contents (type, reporter, description) VALUES (?, ?, ?)";
$query = mysql_query($sql, array('john', $type, 'whatever'));
// Note: The legacy mysql_query does not support named parameters or direct arrays easily in older versions.
// You would typically use mysql_real_escape_string for escaping if not using prepared statements,
// but the preferred approach is switching to mysqli or PDO.

However, relying on mysql_* functions is a red flag. These extensions were deprecated in PHP 5.5 and removed in PHP 7.0. The correct modern implementation uses either mysqli or PDO.

Using PDO, the solution looks like this:

$type = 'testing';
$pdo = new PDO('mysql:host=localhost;dbname=your_db', $user, $pass);
$stmt = $pdo->prepare("INSERT INTO contents (type, reporter, description) VALUES (:type, :reporter, :description)");
$stmt->execute(array(
    ':type' => $type,
    ':reporter' => 'john',
    ':description' => 'whatever'
));

With mysqli, you can achieve similar results with prepare():

$type = 'testing';
$stmt = $mysqli->prepare("INSERT INTO contents (type, reporter, description) VALUES (?, ?, ?)");
$stmt->bind_param('sss', $type, 'john', 'whatever');
$stmt->execute();

Why This Approach Matters

Prepared statements solve the immediate syntax error by telling the database that $type is purely data. They also handle escaping automatically. If $type happens to be 'testing', the database receives it exactly as a string literal, regardless of special characters like single quotes or semicolons inside the value.

This separation of concerns ensures that your application logic remains secure even when dealing with unpredictable user inputs. It prevents SQL Injection attacks where malicious scripts could alter your query structure. While some legacy tutorials suggest using mysql_real_escape_string, that method is error-prone and requires you to manually track which parameters need escaping, leading to forgotten cases in complex queries.

For developers moving forward, adopting PDO or the MySQLi extension is not just a recommendation; it is a requirement for maintaining secure, compatible codebases. If you are building a new application or refactoring an existing one, consider leveraging a framework like Laravel, which handles these database interactions elegantly through its Eloquent ORM, ensuring that variable binding happens automatically behind the scenes.

The transition from string concatenation to prepared statements is the single most important change you can make to your data access layer. It fixes the immediate "variable not recognized" error while simultaneously hardening your application against one of the most common security failures in web development.

Our network