Error in data received in PHP

Asked

Viewed 59 times

0

This error appeared after hosting migration.

Warning: number_format() expects parameter 1 to be float, string given in /home/bramo472/public_html/evolucamp.com.br/doutor/_app/Helpers/Orcamento.class.php on line 105

Warning: number_format() expects parameter 1 to be float, string given in /home/bramo472/public_html/evolucamp.com.br/doutor/_app/Helpers/Orcamento.class.php on line 105

Warning: number_format() expects parameter 1 to be float, string given in /home/bramo472/public_html/evolucamp.com.br/doutor/_app/Helpers/Orcamento.class.php on line 105

Warning: number_format() expects parameter 1 to be float, string given in /home/bramo472/public_html/evolucamp.com.br/doutor/_app/Helpers/Orcamento.class.php on line 105

Warning: number_format() expects parameter 1 to be float, string given in /home/bramo472/public_html/evolucamp.com.br/doutor/_app/Helpers/Orcamento.class.php on line 105

Warning: number_format() expects parameter 1 to be float, string given in /home/bramo472/public_html/evolucamp.com.br/doutor/_app/Helpers/Orcamento.class.php on line 105
MENSAGEM
Erro ao cadastrar: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'como_conheceu' in 'field list

Database:

inserir a descrição da imagem aqui

File that is accused error:

  //Monta a tabela com os itens do carrinho
  private function AmmountCart() {
    if (isset($_SESSION['CARRINHO']) && !empty($_SESSION['CARRINHO'])):
      $this->Data['orc_cart'] = null;

      foreach ($_SESSION['CARRINHO'] as $IDPRO => $PRODUCT):
        $produtos .= "<tr><td> Código: </td> <td> <strong> {$PRODUCT['prod_codigo']} </strong> </td></tr>";
        $produtos .= "<tr><td> Produto: </td> <td> <strong> {$PRODUCT['prod_title']} </strong> </td></tr>";
        $produtos .= "<tr><td> Valor unitário: </td> <td> <strong> R$ ". number_format($PRODUCT['prod_preco'], 2, ',', '.')." </strong> </td></tr>";
        $produtos .= "<tr><td> Modelos: </td> <td> <strong>Quantidades:</strong> </td></tr>";
        foreach ($PRODUCT['modelos'] as $itens => $quantidade):
          $produtos .= "<tr><td> {$itens} </td> <td> <strong> {$quantidade} </strong> </td></tr>";
        endforeach;
        $produtos .= "<tr style='background: #eee; border-bottom:1px solid #ccc; padding: 2px; height:2px;'><td ></td><td></td></tr>";
      endforeach;
      $produtos .= "<tr><td>Valor total </td><td>R$ {$this->Total}</td></tr>";

      $this->Data['orc_cart'] = $produtos;
    endif;
  }

This is line of error:

$produtos .= "<tr><td> Valor unitário: </td> <td> <strong> R$ ". number_format($PRODUCT['prod_preco'], 2, ',', '.')." </strong> </td></tr>";
  • You used number_format($PRODUCT['prod_preco'], 2, ',', '.') and the error says: number_format expecting a float as input, but you passed a string. It follows, then, that $PRODUCT['prod_preco'] is a string and you need to convert to float.

  • Of a var_dump in the variable $PRODUCT['prod_preco'], some character is coming wrong, can even be a comma in place of dot or white spaces.

  • I gave one var_dump the result was NULL. How could you convert to float?

  • Is it something in the hosting? used Ocaweb and worked, now migrated the client to hostgator and generates this error

  • It’s more likely to be a logic error in your code. If you’re coming NULL and should not be null, you did wrong thing.

2 answers

1


Don’t need to be FLOAT no, number_format perfectly accepts string, see:

echo number_format('2.0', 2);

That is, as long as the format is "understandable" as number by PHP, will be valid.

The problem is that you have lines with null values, and null is not a valid format here, I do not know why this with null may be because you’re using JOIN in the query and this bringing something that should not, if not the case of JOIN then it is because in fact it has values such as NULL, then it is simply because no one has defined the unit value of the product yet and nor should it be displayed value, or display a message instead.

But from what I see you’re using SESSION for a shopping cart, the problem then probably is somewhere else, you must have a method called AddCart or AddProduct (hypothetical), at the time of adding the session you should not be correctly sending the value of the product, nor should be sending anything, so $PRODUCT['prod_preco'] probably doesn’t exist, and PHP converts to NULL

Out of that mistake:

Error registering: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'how you met' in 'field list

Your query is wrong, clearly you are trying to do a query with the name of a column that does not exist.

-1

It is possible that this new hosting is with the PHP 7 version and in this version the number_format function expects the first parameter to be a float. Makes a type casting using the floatvar function (http://php.net/manual/en/function.floatval.php). Another option is in the query you force the field when null returns 0. If using Mysql, use the IFNULL function (http://www.mysqltutorial.org/mysql-ifnull/).

I hope I’ve helped.

EDIT

Regarding the type received by the function, there really is no problem if the value is a string, according to @Guilherme Nascimento’s reply.

  • I changed the version of PHP, the error persists, as it was not I who made the system, just migrated from hosting, but I’m sure it is in hosting, in other old works normally.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.