PHP and MYSQLI Error, Call to a member function query() on a non-object
When you encounter the error Call to a member function query() on a non-object, the message is deceptive if you are looking at it from the perspective of your application logic. It appears that you are trying to execute a SQL command, yet the object holding that capability does not exist in the current scope. In the specific scenario provided, the code attempts to call $mysqli->query($sql) inside a function named add_profile(). The immediate assumption is often that the connection was lost or the variable name is wrong, but the root cause lies entirely within how the database extension handles state and object creation.
The error occurs because $mysqli is an undefined variable in the context of the add_profile() function. In the provided code snippet, a new connection is established using mysqli_connect(), which returns a scalar resource representing the link. This resource is stored in the variable $link. However, the subsequent line attempts to execute a query on $mysqli. Since no object has ever been assigned to the variable $mysqli within this function or its calling scope, PHP treats it as null. Attempting to call a method like query() on null triggers the specific error message you are seeing.
To resolve this, you must ensure that the connection variable used for the query matches the one returned by the connection function. In your example, $link holds the connection, so the correct invocation would be $link->query($sql) rather than $mysqli->query($sql). While mysqli_connect() returns a resource, modern PHP development heavily favors the object-oriented interface (OOI) where the connection is instantiated as an object. This distinction is critical: if you instantiate an object using new mysqli(), you should use that specific object instance for subsequent queries. Mixing scalar resources created via mysqli_connect() with variable names intended for OOI instances leads to confusion and runtime errors.
Furthermore, there is a significant architectural flaw in the current implementation regarding connection lifecycle management. The function add_profile() creates a new database connection every time it is called. While this works for simple scripts, it introduces scalability issues and prevents efficient resource pooling. In production environments, it is best practice to establish the connection onceâtypically at application startup or via a dependency injection containerâand pass that singleton instance into your functions. This ensures that the $mysqli object exists in the expected scope without needing to re-establish the handshake for every single database operation.
Another critical pitfall in the provided SQL statement involves string interpolation and variable binding. The query INSERT INTO payment_profiles(id, client_id) VALUES ( '','$profile_id') contains a hardcoded empty string for the ID but attempts to use $profile_id without quotes. In MySQL, this will fail unless the column type is explicitly defined as a number or an unsigned integer with auto-increment disabled. More importantly, passing raw variables directly into SQL strings invites SQL injection vulnerabilities if $profile_id contains user input. Using prepared statements with bound parameters eliminates this risk and ensures the database interprets the value correctly based on its data type.
If you are working within a larger Laravel application, maintaining separate mysqli connections outside of the framework's service container is generally discouraged. The https://laravelcompany.com platform emphasizes leveraging built-in Eloquent ORM features which abstract these connection details entirely. By utilizing the database facade or model queries, you avoid manual instantiation of $mysqli objects altogether, reducing the likelihood of scope-related errors like the one described here.
Finally, always verify your environment variables and configuration before assuming the code logic is at fault. If the add_profile() function is intended to run in a shared hosting environment or a specific deployment context, ensure that the credentials provided ($hostname, $dbusername, etc.) are valid and that the database user has sufficient privileges to write to the payment_profiles table. A failed connection due to authentication errors would result in a different error message, but ensuring the connection object is properly initialized remains the first step in debugging any mysqli query failure.