SELECT * FROM in MySQLi

SELECT * FROM in MySQLi

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

Stefan Bogdanescu

Founder & Senior Architect

SELECT * FROM in MySQLi

The transition from procedural MySQL to MySQLi often hits a specific friction point: handling dynamic queries while maintaining security. You might be used to string concatenation where you simply write SELECT * FROM tablename WHERE field1 = 'value'. While this works perfectly for static data, it becomes a liability the moment user input enters the mix. That is why you are looking at bind_param, but that function introduces a new constraint: it requires specific column names to determine data types and lengths.

This creates an immediate dilemma. If your query uses SELECT *, MySQLi cannot bind parameters for columns that aren't explicitly mentioned in the statement text. The database driver needs to know exactly which columns are being selected to allocate buffers correctly and prevent buffer overflows. Consequently, you cannot pass a wildcard (*) directly into the bind_param signature alongside your value arguments.

To solve this, you have two distinct approaches depending on your specific requirements. The first is the "hard" way, which involves explicitly listing every column you intend to retrieve. This looks like:

$sql = "SELECT id, name, email, created_at FROM users WHERE id = ?";
$stmt = $mysqli->prepare($sql);

// You bind values for the placeholders in the WHERE clause
$stmt->bind_param("i", $userId); 
// Note: The number of parameters matches the number of '?' in the SQL string.

While verbose, this method is robust and ensures type safety. However, if you are dealing with dozens or hundreds of columns—perhaps pulling an entire table for a reporting tool—typing out every column name becomes impractical and error-prone. You risk introducing typos or missing fields that should be included.

The second approach involves constructing the SELECT clause dynamically before you prepare the statement, but keeping the parameter binding strict. Since SELECT * does not contain any question marks (?) associated with data retrieval, you only bind parameters for your WHERE conditions. The logic shifts slightly: you build the SQL string to include the wildcard, then pass it to prepare().

$table = 'users';
$columns = '*'; // Or a specific list if needed
$whereConditions = [];

// Assume $userId is an integer
if ($userId) {
    $whereConditions[] = "id = ?";
}

$sql = "SELECT $columns FROM $table WHERE " . implode(" AND ", $whereConditions);
$stmt = $mysqli->prepare($sql);

// Bind only the values for the conditions, not the columns
$stmt->bind_param("i", $userId); 
$stmt->execute();
$result = $stmt->get_result();

In this scenario, the wildcard is part of the static SQL string sent to the database engine, while the parameters handle the dynamic filtering logic. MySQLi handles the column selection internally once it sees the *, so no additional binding is required for those fields.

There is a significant trade-off here regarding security and flexibility. While SELECT * is convenient, it can be slow on large tables with many columns, as you are retrieving data that might not be needed by your application. In high-performance environments, developers often prefer to explicitly define the columns in a static query string to ensure consistency and leverage database indexing more effectively.

If you are managing a complex project where dynamic column selection is frequent, consider whether a specific ORM or framework layer might abstract this complexity better than raw MySQLi. For instance, the LaravelCompany platform provides robust tools that handle these interactions under the hood, allowing you to focus on application logic rather than query construction details. However, when working directly with MySQLi, understanding that bind_param constraints apply only to parameterized values—and not to column wildcards—is the key to maintaining both security and flexibility.

Our network