Call to a member function bind_param() on a non-object
You are encountering the error Call to a member function bind_param() on a non-object because $qSelect is not a valid prepared statement object at the moment you attempt to call the method. The root cause lies in the lifecycle of the PDO object within your scope, specifically regarding where and when the prepare() method is executed versus where the error occurs.
In your provided code snippet, the logic flow appears to be triggered by a check on the first page, which then invokes a function named check on the second page. Inside that function, you retrieve a database handle using getDBH(). You then prepare a statement and immediately attempt to bind parameters:
$DBH = getDBH();
$qSelect = $DBH->prepare("SELECT * FROM users WHERE username = ?");
$qSelect->bind_param("s", $username); // Error occurs here
The PHP error message explicitly states that $qSelect is not an object. While you might assume $DBH is valid because getDBH() returns it, the specific failure point is the interaction between PDO and the parameter binding method. Here is the critical distinction: bind_param() is a MySQLi-specific function, not a PDO function.
PDO uses a different API for prepared statements. When you call $DBH->prepare(), you receive a PDOStatement object. This object does not have a bind_param() method. Attempting to call a method that exists on a MySQLi statement object (mysqli_stmt) against a PDO statement object (PDOStatement) results in the fatal error you are seeing.
To resolve this, you must align your binding syntax with the driver you are using. If you are using PDO (which is standard for modern Laravel applications), you should use bindValue or bindParam. Here is how to correct the code while keeping the same logical flow:
$DBH = getDBH();
$qSelect = $DBH->prepare("SELECT * FROM users WHERE username = ?");
// Use bindParam for PDO, noting the order of arguments differs slightly from MySQLi
$qSelect->bindParam(1, $username, PDO::PARAM_STR);
Alternatively, if you prefer not to specify the parameter index explicitly, bindValue is often preferred in dynamic contexts:
$qSelect->bindValue(1, $username, PDO::PARAM_STR);
The trade-off between bindParam and bindValue is important. bindParam binds a variable by reference, meaning the value of $username must be set before the binding occurs. If you assign a value to $username after calling bindParam, the query will execute with an empty string or NULL. Conversely, bindValue copies the current value immediately.
Another potential pitfall involves the execution order. In your code, the function check is called from "Page One," but the logic resides on "Page Two." If these are separate PHP scripts loaded via include/require or if you are dealing with a multi-step form submission, ensure that $username is not undefined when the binding happens. However, the primary issue remains the method mismatch.
If you were indeed using MySQLi (new mysqli()), your code would be correct, but you would need to instantiate the connection differently and handle exceptions via mysqli_error() or exception handling enabled on the connection. Since the error message implies a missing object property rather than a syntax error in the function call itself, switching from bind_param (MySQLi) to bindParam (PDO) is the necessary fix.
For projects migrating from procedural PHP or older MySQLi patterns to modern frameworks like Laravel, this is a common stumbling block. The framework defaults to PDO for database connections, and assuming bind_param works universally leads to runtime crashes. Always verify the object returned by your connection handler before invoking driver-specific methods. If you require further assistance with database abstraction layers in specific contexts, resources like https://laravelcompany.com can provide guidance on best practices for handling data persistence.