Warning: A non-numeric value encountered PHP MySQL

Warning: A non-numeric value encountered PHP MySQL

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

Stefan Bogdanescu

Founder & Senior Architect

Warning: A non-numeric value encountered PHP MySQL

You are seeing this warning because one of your variables, specifically $cartTotal, is holding a string instead of a float during the calculation. When you initialize $cartTotal as an empty string ("") and then immediately try to add a numeric result to it in the line $cartTotal = $pricetotal + $cartTotal;, PHP attempts to perform arithmetic on a string. While PHP's loose typing allows it to coerce the string into a number, it triggers an E_WARNING if that string contains any characters other than digits (like the HTML tags you are appending later, or simply because of how the empty string is handled in specific contexts depending on the version).

The root cause in your code is twofold: improper initialization and mixing data types with HTML.

First, you initialize $cartTotal as an empty string. You should initialize it as a float or integer set to zero:

$cartTotal = 0;

Second, you are appending HTML directly into the variable that holds the running total calculation logic. In your code, $cartTotal is used for math, but at the end of the loop, you overwrite it with an HTML string before the warning even has a chance to fully resolve in some configurations, or more likely, the initial addition fails because the first iteration adds a number to an empty string (which usually works fine), but if $cartTotal ever receives a non-numeric value from another source or if the locale settings interfere with the float representation, you get the error.

However, looking closely at your specific logic, there is a more subtle issue involving setlocale. You call setlocale(LC_MONETARY, "en_EU") inside your loop. This changes how PHP formats numbers and currency symbols based on the server's locale settings. If the locale configuration expects a different decimal separator (e.g., a comma instead of a dot) or adds specific formatting characters, $pricetotal might become a string formatted for display rather than a raw float.

When you subsequently do $cartTotal = $pricetotal + $cartTotal, if $pricetotal has been implicitly converted to a locale-formatted string (like "10,50 €"), adding that to your total will fail with a non-numeric value warning because the comma is not a valid numeric character for PHP's arithmetic engine.

To fix this, ensure your math happens on raw floats and only format the final output. Move the setlocale call outside of your calculation loop or remove it entirely if you are formatting manually. Here is the corrected approach:

$cartTotal = 0; // Initialize as a number, not an empty string

foreach ($_SESSION["cart_array"] as $each_item) { 
    // ... fetch data ...
    
    // Ensure calculation uses raw floats
    $pricetotal = (float)$kaina * (int)$each_item['quantity'];
    $cartTotal += $pricetotal;

    // Remove setlocale from inside the loop to prevent locale-dependent formatting on the float
}

// Only format after all calculations are complete
setlocale(LC_MONETARY, "en_US"); 
$cartTotalFormatted = number_format($cartTotal, 2, '.', ',');
$cartTotal = "<div style='font-size:18px; margin-top:12px;' align='right'>Krepšelio suma : ".$cartTotalFormatted." EUR</div>";

Security and Best Practices

While fixing the warning is immediate relief, the code snippet contains significant security vulnerabilities that should be addressed to prevent future headaches. You are currently concatenating user input ($item_id) directly into SQL queries:

SELECT * FROM prekes WHERE id='$item_id' LIMIT 1

This exposes your database to SQL injection attacks. An attacker could supply a malicious $item_id payload to read or modify data they shouldn't access. You must use prepared statements. In MySQLi, this looks like:

$stmt = $con->prepare("SELECT * FROM prekes WHERE id=? LIMIT 1");
$stmt->bind_param("i", $item_id); // 'i' for integer
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();

Additionally, you are echoing user data directly into HTML without sanitization: $pavadinimas, $gamintojas, and $product_id_array. If a product name contains <script> tags or quotes, your site will render broken HTML or become vulnerable to Cross-Site Scripting (XSS). Always escape output with htmlspecialchars():

'<td>'. htmlspecialchars($pavadinimas) .'</td>'

Moving Beyond Legacy PHP

The code you provided relies heavily on string concatenation ($cartOutput .= ...) to build HTML tables. This approach makes debugging difficult and increases the risk of introducing syntax errors or breaking the layout if a product name contains special characters. Modern PHP projects often utilize templates engines like Twig, Blade (Laravel), or Smarty. These tools handle escaping automatically and allow for much cleaner separation of logic and presentation.

If you are maintaining an older codebase and cannot refactor to a templating engine immediately, stick to the rules: initialize numbers correctly, keep math separate from formatting, and sanitize every string before it hits an HTML tag. If your project is new or undergoing a rewrite, consider migrating to Laravel for better security defaults and maintainability. You can find more guidance on modernizing legacy PHP applications at https://laravelcompany.com.

Our network