简体   繁体   中英

MySQl leading zeros get stored but wrong display

I am trying many typed of fields in MySQL to store numbers only, tried with INT, BIGINT with leading zeros, CHAR and VARCHAR to store INVOICE NUMBERS

I need the invoice numbers to be start with 0000000001, I stored it manually in PHPmyadmin

Now I want to display it and I dont get the leading zeros ....

Here is the database

field "folio" CHAR 15 stored I have manually did 0000000001 it displays fine on phpmyadmin

but here is the problem

<?php $maxprod=mysqli_query($datacenter, 
"SELECT * FROM ventas 
WHERE documento = 'boleta' 
ORDER BY id DESC LIMIT 1");
while($lastcode=mysqli_fetch_assoc($maxprod)){?>


<input type="text" value="<?php echo $lastcode['folio']+1?>">
   <?php }?>

the result of the query is 1 just 1 it does not display all other zeros

Any idea why?

echo $lastcode['folio'] should show you a result with the leading zeroes, but not $lastcode['folio']+1 .

As soon as you do that +1 , the result is no longer a string. The $lastcode['folio'] variable is converted to a number in order to do the arithmetic operation on it.

The leading zeroes are just formatting and don't need to be stored with the number. If you need an autoincrementing number, just use an autoincrement integer in MySQL, and format the number with leading zeroes when you print it out.

PHP automatically converts string into number if you are performing any numerical operation on it. But you can keep the order number in integer form and pad it with zeroes when necessary:

str_pad($lastcode['folio']+1, 15, "0", STR_PAD_LEFT);

This will retrieve the number as a string to display it as text.
Later you can manipulate it on your coding environment.

SELECT CAST(document_number AS CHAR) FROM ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM