Pergunta

I tried switching over to PHP5 i think it is, with the use of mysqli prepared statements. It works fine, with everything else, but when it comes to numbers, for some reason it decides to delete the zero on the end of the value of a number. For example in the database, the value for record is 18.50, and when the query is run, with the use of mysqli prepared statements, it returns 18.5, it also does the same for when a record is 0.00, it returns 0. These values are for prices, so therefore it is important to show £18.50 or £0.00. Can anyone direct me in the right path please.

The php is:

    $sql = "SELECT eventID, eventTitle, eventStartDate, eventEndDate, venueID, catID, eventPrice, eventDescription FROM te_events ORDER BY eventTitle ASC";

    $stmt = mysqli_prepare($conn, $sql);

    mysqli_stmt_execute($stmt);

    mysqli_stmt_store_result($stmt);

    mysqli_stmt_bind_result($stmt, $event_id, $event_title, $event_start_date, $event_end_date, $venue_id, $cat_id, $event_price, $event_desc);

    while (mysqli_stmt_fetch($stmt)) {
    ..........
    }
Foi útil?

Solução

Well, 18.50 is 18.5 numerically. You'd handle this on the display end with something like http://php.net/money_format or http://php.net/number_format

Outras dicas

Your issue is that mysqli returns a float instead of a string like the traditional mysql functions did, you can restore the old behaviour of returning strings by casting the type before you return it from mysql or using number_format:

$sql = "SELECT
   eventID,
   eventTitle,
   eventStartDate,
   eventEndDate,
   venueID,
   catID,
   CONVERT(VARCHAR(30), eventPrice) eventPrice,
   eventDescription
 FROM
   te_events
 ORDER BY
   eventTitle ASC";

OR inside the while loop:

$event_price = number_format ($event_price, 2, ".", "");

Numeric values are always going to do that. You have two choices, either store your value in your database as a string value, or (recommended) format the integer output using money_format() or a string formatter.

http://www.php.net/manual/en/function.money-format.php

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top