An error when I add a variable to a string

An error when I add a variable to a string

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

Stefan Bogdanescu

Founder & Senior Architect

String Concatenation Breaks Your SQL Query When Using Variables

You are encountering a classic runtime issue caused by how your variables interact with string concatenation in PHP, specifically when constructing dynamic SQL queries. The error message Error: SELECT ID, ListStID, ListEmail, Title FROM WHERE ID = '4' reveals the root cause immediately: the table name is missing.

In your provided code snippet, you are using direct string concatenation to build the query:

$sql = "SELECT ID, ListStID, ListEmail, Title FROM $entry_database WHERE ID = '". $ReqBookID ."'";
$result = mysqli_query($conn, $sql);

While this approach works in a local environment where $entry_database is correctly defined, it fails when that variable contains an empty string or is undefined. If $entry_database is empty, the resulting string becomes SELECT ... FROM WHERE .... In SQL syntax, the keyword FROM must always be followed by a table name. When the table name is missing, the parser sees FROM immediately followed by WHERE, which is invalid. The database engine throws a syntax error because it expects a column list or a subquery after FROM, not another clause.

This issue highlights a critical distinction between static and dynamic SQL construction. When you concatenate strings, you are trusting that every variable inserted into the string has a valid value at runtime. If $entry_database is set to an empty string in your online environment—perhaps due to different configuration files or session handling compared to your local setup—the query breaks instantly.

Beyond fixing the immediate syntax error, there is a far more pressing concern: SQL Injection. Your current code concatenates user input ($ReqBookID) directly into the query string without sanitization. If a user submits ' OR '1'='1 as their ID, your code generates:

SELECT ID, ListStID, ListEmail, Title FROM $entry_database WHERE ID = '' OR '1'='1'

This bypasses authentication and allows data theft. You must never concatenate raw user input into SQL strings.

The industry-standard solution is to use prepared statements. This technique separates the SQL logic from the data, ensuring that user input is always treated as a string value rather than executable code. With mysqli, you would rewrite your query like this:

$sql = "SELECT ID, ListStID, ListEmail, Title FROM ? WHERE ID = ?";
$stmt = $conn->prepare($sql);

// Bind the placeholders to the variables
// 's' stands for string, which is appropriate for IDs that might be text
$stmt->bind_param("ss", $entry_database, $ReqBookID);

// Execute the query
$stmt->execute();
$result = $stmt->get_result();

Prepared statements handle the escaping of quotes automatically. Whether $ReqBookID contains a single quote ', a number 4, or text, the database engine knows to treat it as data, not a command. This also fixes the table name issue; you simply pass the variable into the placeholder for the table name, and the binding ensures it is treated as an identifier if your specific driver supports it (though often table names require careful handling depending on the context).

If you are working within the Laravel framework, this pattern is abstracted away. You would use Eloquent or raw query bindings:

// Using Eloquent
$book = Book::where('ID', $ReqBookID)->get();

// Or using a raw query with bindings
$sql = "SELECT ID, ListStID, ListEmail, Title FROM ? WHERE ID = ?";
$stmt = DB::select($sql, [$entry_database, $ReqBookID]);

The trade-off of using prepared statements is slightly more verbose code and the need to manage statement execution manually in raw PHP. However, the security benefit is absolute, and it eliminates runtime syntax errors caused by malformed table names or unexpected characters in your data. Always prioritize prepared statements over string concatenation when handling user input.

Our network