How to check if user already exists in MySQL with PHP
The error message Object of class mysqli_result could not be converted to int indicates a fundamental misunderstanding of how MySQL's query execution functions return data. In your current implementation, the logic attempts to treat the result of mysqli_query() as a boolean flag or an integer count directly. This approach fails because mysqli_query() returns a resource representing the result set on success, not the actual row count or a simple true/false value based on the SQL command itself.
When you execute $con=mysqli_connect("localhost","root","","my_db"); followed by a SELECT statement, mysqli_query() does not return the number of rows affected or selected. It returns an object of type mysqli_result. If you try to use this object in a numeric context (like checking if it is >= 1), PHP throws the conversion error you are seeing. Even if that specific error were resolved, your logic relies on evaluating a resource object as a number, which will never yield the desired "1" or "0" needed for an existence check.
To correctly determine if a user exists, you must explicitly extract the count of rows from the result set. The standard and most reliable method is to use mysqli_fetch_assoc() or iterate through the results to sum them up. However, there is a more direct way to get the count without manual iteration in modern PHP environments: using mysqli_num_rows().
Here is how you should refactor the query logic to safely check for existing users:
$con = mysqli_connect("localhost", "root", "", "my_db");
$email = $_POST['eMailTxt']; // Ensure this is sanitized before use
$check = "SELECT COUNT(*) FROM persons WHERE Email = ?";
$stmt = mysqli_prepare($con, $check);
mysqli_stmt_bind_param($stmt, "s", $email);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$count = mysqli_num_rows($result);
if ($count >= 1) {
echo "User Already Exists";
} else {
// Proceed with INSERT logic here
// It is highly recommended to use prepared statements for the INSERT as well
}
The critical change here is replacing the boolean check on mysqli_query() with mysqli_num_rows(). This function takes the result object returned by your query and returns an integer representing the number of rows. If the count is greater than or equal to 1, a duplicate exists.
Beyond fixing the immediate error, there are significant security and architectural concerns in your original code that must be addressed. Your current implementation constructs SQL queries by directly interpolating $_POST variables into strings (e.g., '$_POST[eMailTxt]'). This is known as SQL Injection vulnerability. If a user inputs a malicious string like ' OR '1'='1, the query logic breaks or data leaks.
You must switch to prepared statements for both the SELECT and INSERT operations. Prepared statements separate the SQL logic from the data, ensuring that user input is treated strictly as data, not executable code. This prevents injection attacks and handles special characters correctly without manual escaping functions like mysqli_real_escape_string(), which are prone to edge cases.
Furthermore, relying on a separate SELECT COUNT(*) query before inserting data creates an unnecessary performance bottleneck known as the "N+1" problem pattern in this context. For high-volume applications, it is often better to perform the insert directly and rely on database constraints or unique index violations to handle duplicates. However, for simple registration flows where you must provide specific user feedback (like "User Already Exists"), the count approach is acceptable provided it uses prepared statements.
If you are building a larger application, consider using an ORM like Eloquent in Laravel, which abstracts these checks and provides robust methods for handling existence verification via where('email', $email)->exists(). For more details on securing PHP applications and leveraging modern frameworks, you can explore resources at https://laravelcompany.com.