numbers issue after export mysql data to CSV - php

when i get data from database and export it as a csv file i have following issues :
the leading zero of the numbers dosnt show
big numbers shows like this -> 1E+12
how to force MS Excel to show all of my data as string by PHP

You can use double quotes contain the numbers, like this:
if(is_numeric($column)) $column = '"'.$number.'"';

This isn't a MySQL issue, it's an Excel thing.
This... might help: http://answers.microsoft.com/en-us/office/forum/office_2007-excel/disabling-scientific-notation/943b8103-8c50-451d-8037-c697422e2307
But this question is more MS Office related than programming.
(or as comments suggested, use a file format that carries display formatting info, such as .xls)

I format it as a string by concatenate it with spaces at its start and end.
CONCAT(" ", database_number, " ") AS "Number For Excel".

Kai's answer didn't quite work for me. I ended up CONCATing an equals sign (still with the double quotes on either end) to the front and got the desired result. Our users wanted to be able to copy and paste the number straight out of Excel into our UI.
CONCAT('="',too_long_number_field,'"')

You can use number_formate() function to convert ...
View link
Function : number_format()
Syntax : number_format ( float $number , int $decimals = 0 , string $dec_point = ‘.’ , string $thousands_sep = ‘,’ )
Example to convert 6.90743E+11 to number use below code
number_format(6.90743E+11,0,'','') // outputs 690743000000

Related

How to prevent mysql from adding 2 values with each other in php?

So i want to add some info to the existing info in mysql, so i use this kind of code:
SET date = $alp + number
WHERE id=15 ');
$ES->execute();
$ES->close();
here i got 2 little problems:
When i type an integer, it adds them together. Date is preset as an integer, and i type $alp as 3 for example.
Instead of showing 3 4, it shows like 7. It adds 2 numbers. How do i prevent that? I just want them to stay side by side. Anohter problem when i try to put string, that is kind of fair because letter plus number doesnt work. But how do i make the command so the sql gets it as putting it together and not adding them (doing math i mean). would appriciate answers!
Use CONCAT to "string" the number together in MySQL.. Like so:
SET date = CONCAT($alp, ' number')
Your output will be 3 4
And to concatenate a string in php you would do something like:
$alp = '3';
$alp .= ' 4';
echo $alp;
// output: 3 4

Inserting symbols in mysql using php

I'm using php and mysql and I have a problem with inserting latitude and longitude values along with the degree, min and sec symbols. that is (°,',") symbols.
I have referred several sites and tried different ways but i just can't solve it.
First i tried copying symbols and concatenating with the 3 user input values.
$latitude=$degree.'°'.$min.'''.$sec.'?'.'N';
Then after executing the insert query,the $latitude variable value is inserted in database, but the symbols are replaced by random characters as shown here.
2˚3ʼ4ˮN (data stored in database)
Then i tried using html character code instead of symbols.
$latitude=$degree.'&deg'.$min.'&#8217'.$sec.'#8221'.'N';
But it doesn't work.It displays the same html characters in databse.
I don't know whether it is a problem with html entities or something.
In my opinion such values should be stored without including its ( DMS : Deg Min Sec ) formatting .
In other words , I might store latitude/longitude ( for example ) as a float value and would apply required formatting when displaying .
Just for example :
You would store a date as 2015-04-23 but might display it as 4th April, 2015 .
You might store an amount as 10.50 but might display it as $10.50 USD .
Coming to latitude/longitude considering 36°19'11.46" N as example , one of these ways might apply to your situation :
Store it as 36.31985 but use required conversion to display it as 36°19'11.46" N .
Store it as 36D19M11.46S N but use required string manipulation to display it as 36°19'11.46" N .
Store it as four parts lat_deg , lat_min , lat_sec , lat_dir but use concatenation to display it as 36°19'11.46" N .
Links that might help :
mysql-convert-degree-minutes-seconds-to-degree-decimal
latitude-and-longitude-datatype-and-storage-format
convert-dd-to-dms-in-mysql
converting-latitude-and-longitude-coordinates-between-decimal-and-degrees-minutes-seconds
whats-the-best-way-to-store-co-ordinates-longitude-latitude-from-google-maps
Try this function:
$result = mysqli_real_escape_string();
refer following link
http://php.net/manual/en/mysqli.real-escape-string.php

How to change decimal separator cleanly in outputs in mysql - php combo

I was trying to export database query to CSV and the need arised to use different decimal comma. It seems impossible to change the decimal comma in MySQL, so I tried in PHP:
setlocale(LC_NUMERIC, "cs_CZ");
But it seems that all the database functions like mysql_fetch_row and mysql_fetch_assoc are retrieving data of type string instead of double:
$res = mysql_query("select 50/3");
$row = mysql_fetch_row($res);
var_dump($row); // $row[0] is of type "string"
So in general PHP already doesn't have data of type double, but only strings!
So is there some general, clean way to specify output of the decimal point?
I ended up converting the strings using str_replace('.', ',', $row[0]) but this is ugly for two reasons:
you have to know which field is of type double
it's a dirty string job.
I don't know which database client/driver you're using but there is something like mysql_field_type­Docs which gives you the type based on it's offset, like 0.
This should do the job to find out if a certain column needs re-formatting or not.
To reformat, there is number_format­Docs.
With these you can do the conversion automatically.
Edit: Regarding your comments:
If you want to get PHP datatypes mapped, consider using the MySQL Native Driver­Docs. Use it together with PDO:
Advantages of using mysqlnd for PDO
mysqlnd returns native data types when using Server-side Prepared Statements, for example an INT column is returned as an integer variable not as a string. That means fewer data conversions internally. Source
So depending of what you try to achieve, use the right tool.
See as well the multiple options you have when fetching data from a PDO Statement­Docs.
// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
Try
number_format($row[0], 2, ",", ".");
That should change the format of the number. (german format)

How to store European Currency in MySQL?

On a new project I work on I have data in CSV format to import into a mysql table. One of the columns is a price field which stores currency in the european format ie. 345,83.
The isssue I have is storing this decimal seperator. In most European currencies the decimal seperator is "," but when I try to insert a decimal number into a field (ex. 345,83), I get the following error: "Data truncated for column 'column_name' at row 'row #'". If I use '.' instead of ',' it works fine. Could you please help me with, how to store this format in mysql?
you can store it as a regular decimal field in the database, and format the number european style when you display it
edit: just added an example of how it might be achieved
$european_numbers = array('123.345,78', '123 456,78', ',78');
foreach($european_numbers as $number) {
echo "$number was converted to ".convert_european_to_decimal($number)."\n";
// save in database now
}
function convert_european_to_decimal($number) {
// i am sure there are better was of doing this, but this is nice and simple example
$number = str_replace('.', '', $number); // remove fullstop
$number = str_replace(' ', '', $number); // remove spaces
$number = str_replace(',', '.', $number); // change comma to fullstop
return $number;
}
Use number_format or money_format, it's pretty much what you preffer.
It's worse than you think. The number 1234.56 may be written in Europe as:
1234,56
1 234,56 (space as a group separator)
1.234,56 (dot as a group separator)
In .net the number parser can works according to a given culture, so if you know the format it does the hard work for you. I'm sure you can find a PHP equivalent, it'd save you a lot of trouble.
You could import the currency field into a VARCHAR column and then copy this column into a DECIMAL column while replacing the , by a . in all rows using MySQL string-manipulation-functions.
UPDATE <<table>>
SET <<decimal-currency-col>> = REPLACE(<<varchar-currency-col>>, ',', '.');
Some data types do not have a direct
correlation between SQL Server or
Access and MySQL. One example would be
the CURRENCY data type: MySQL does not
(yet) have a CURRENCY data type, but
creating a column with the definition
DECIMAL(19,4) serves the same purpose.
While MSSQL defaults to Unicode
character types such as nCHAR and
nVARCHAR, MySQL does not so tightly
bind character sets to field types,
instead allowing for one set of
character types which can be bound to
any number of character sets,
including Unicode.
from http://dev.mysql.com/tech-resources/articles/migrating-from-microsoft.html
You could also consider multiplying it by 100 and storing it as INT.
Before inserting the price to the DB:
$price = (int)$price*100;
After receiving price from the DB:
$price = number_format($price, 2, ',', ' ');
Try replacing the "," with "."?
$price = str_replace(",", ".", $price);

How to add dots and commas to number in MySQL query

I have a cell with a great number, like 650400300. I need to SELECT this number and separate to receive next format - 650,400.300. Would you mind to help me, please. Thanks in advance! I'm using PHP and MySQL.
UPDATE: Now I think the correct way to do so is to use PHP, but thank you "hd1" your answer is perfectly fits and works correctly with what I asked here.
UPDATE2: I don't need to add zero's to the end, but I need to split the whole number into peaces with commas and dots.
Use the MySQL format function to do this:
MySQL> SELECT FORMAT(12332.2,0);
12,332
Let's assume you want php and not turning mysql into a gui tool, then
number_format($number)
Doing this with MySQL is not really nice, it is supposed to give you what is in the database. If the database holds an integer, MySQL should give you an integer.
You can do it with php, after you have fetched the data using number_format:
$formatted_number = number_format($number);
Note:
First, you must fetch that data:
Then use substr_replace to format the number
Code:
$number = "650400300";
$number = substr_replace($number, ",", 3, 0);
$number = substr_replace($number, ".", 7,0); /* RETURNS 650,400.300 */
Using number_format() to your given example (650400300) will give this result:
$number = number_format($number); /* 650,400,300 */

Categories