Notice: Array to string conversion in

Notice: Array to string conversion in

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

Stefan Bogdanescu

Founder & Senior Architect

Notice: Array to string conversion in

When you encounter a Notice: Array to string conversion error in PHP, it is almost always a mismatch between the data type your code expects and the data type you actually have. In the specific scenario provided, the developer is attempting to fetch a database value using mysql_fetch_assoc() and immediately echo that variable as if it were a scalar string. The root cause lies in how mysql_fetch_assoc() constructs its return value: it returns an associative array containing columns from the current row of the result set.

If your query selects only one column, such as SELECT money FROM players..., the resulting array will look like this: ['money' => 150]. While this holds just one element, PHP still treats it strictly as an array object. When you execute echo $money;, PHP attempts to convert that array into a string for output by default. Since there is no explicit conversion logic defined for arrays in the echo statement, PHP triggers the notice and outputs the string representation of the array keys, usually resulting in something like "Array".

To fix this immediately, you must access the specific key within the associative array. You should echo $money['money'] instead of $money. This ensures that the scalar integer or float value is extracted before being sent to the browser. However, relying on mysql_fetch_assoc() with the deprecated mysql_* extension is a significant technical debt. The mysql_* functions were removed in PHP 7.0 due to security vulnerabilities and lack of support for prepared statements.

A modern approach involves using MySQLi or PDO, which enforce stricter typing and offer better security practices. Consider rewriting the logic using PDO with prepared statements to prevent SQL injection and handle data types more gracefully:

try {
    $pdo = new PDO("mysql:host=localhost;dbname=game", "user", "pass");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $username = $_SESSION['username'];
    
    // Use a prepared statement to safely interpolate the username
    $stmt = $pdo->prepare("SELECT money FROM players WHERE username = :username");
    $stmt->execute([':username' => $username]);
    
    // Fetch as an associative array
    $result = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($result) {
        // Access the specific column directly
        echo "<p id='status'>" . htmlspecialchars($username) . "</p>";
        echo "Money: " . $result['money'] . "</p>";
    } else {
        // Handle the case where the user is not found
        echo "<p id='status'>User not found.</p>";
    }
} catch (PDOException $e) {
    echo "Database error: " . $e->getMessage();
}

In this implementation, fetch(PDO::FETCH_ASSOC) returns the same structure as the old function, but the subsequent logic explicitly accesses $result['money']. This prevents the accidental echoing of the entire array. Additionally, using htmlspecialchars() on the username before outputting it mitigates Cross-Site Scripting (XSS) vulnerabilities, which is a critical oversight in the original snippet where raw session data was echoed directly into an HTML tag.

If you are maintaining legacy code that must continue to use the mysql_* functions for some reason, you can cast the variable to a string explicitly using (string)$money['money']. While this suppresses the notice, it does not solve the underlying architectural issue of relying on deprecated functions or assuming that a single-column select returns a scalar value rather than an array. Always verify the structure of your data before attempting to render it in a view layer.

Our network