Object of class mysqli_result could not be converted to string
You are encountering this error because your code attempts to treat a database query result object as a simple string variable. In PHP, mysqli_query() returns a mysqli_result object when a SQL statement returns data, rather than the raw rows themselves. When you place $result directly into an HTML attribute like data/$result.php, PHP tries to cast that object into a string automatically. Since the mysqli_result class does not implement the __toString() magic method by default, the engine throws a fatal "Object of class mysqli_result could not be converted to string" exception.
The root cause lies in how you are accessing the data from your query. The variable $result currently holds the entire dataset returned by the SQL statement:
$result = mysqli_query($con, "SELECT classtype FROM learn_users WHERE username='abcde'");
echo "my result <a href='data/$result.php'>My account</a>";
In this specific context, you are assuming that $result contains the actual value of classtype or a path string. However, it contains a cursor pointing to a set of rows. Even if the query returns exactly one row, PHP will not automatically extract the first column's value and hand it to you as a string. You must explicitly fetch the data from the result object before using it in your HTML generation.
To resolve this, you need to retrieve the specific row you want. If you know the query returns only a single record, fetch_assoc() is the most convenient method. It converts an associative array representation of the first row, allowing you to access the classtype column by name:
$result = mysqli_query($con, "SELECT classtype FROM learn_users WHERE username='abcde'");
if ($result && $row = mysqli_fetch_assoc($result)) {
$classtype = $row['classtype'];
// Now we can safely use the string value in the href
echo "my result <a href='data/$classtype.php'>My account</a>";
} else {
// Handle cases where no user is found or the query fails
echo "User not found";
}
This approach separates the data retrieval logic from the presentation logic. By assigning $row['classtype'] to a variable, you ensure that only a string (or potentially null) is passed to the href attribute. This prevents PHP from attempting an invalid type conversion on the result object itself.
There are important trade-offs and pitfalls to consider when working with raw mysqli results in this manner. First, security is paramount. Your original SQL query uses direct interpolation of the username variable ('abcde'). If that variable comes from user input without sanitization or parameterization, you are vulnerable to SQL injection attacks. Modern best practices dictate using prepared statements:
$stmt = $con->prepare("SELECT classtype FROM learn_users WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
Second, relying on implicit casting with mysqli_fetch_row() or similar methods can lead to type juggling issues if the column in your database is defined as a string but contains numeric values. While fetch_assoc helps by using named keys, always validate that the fetched value exists and is of the expected type before concatenating it into URLs.
Finally, consider the performance implications of fetching data. If you are building a dynamic link structure where the filename must match the database content, ensure that the generated path (e.g., data/classname.php) actually corresponds to an existing file on the server. PHP will not throw the "object could not be converted to string" error again if the data is fetched correctly, but it might result in a 404 error or a security risk if the application attempts to include non-existent files dynamically.
If you are migrating away from procedural mysqli syntax toward Laravel, you will find that the Eloquent ORM handles this extraction for you automatically when building relationships or querying models. However, understanding the underlying behavior of mysqli_result is essential for debugging issues in legacy codebases or when writing custom database logic where you need full control over the raw result set.