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.'°'.$min.'’'.$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
Related
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
I am using mysql and php (Laravel) and facing a very strange situation .
I am trying get an encrypted user type and user id and decrypt it . and then find the records for that user id .
my code is like this . i am using Laravel framework .
$key = Config::get('app.key');
$decodedUserIDwithType = base64_decode($encryptedUserIDwithType);
$decryptedUserIDwithType = mcrypt_decrypt(XXXXX , $key, $decodedUserIDwithType, XXXXXX);
$userIDwithType = $decryptedUserIDwithType;
I am expecting the decoded value to be something like this
id#100
so i will explode it by # and find the user id , here it is 100 .
for testing i have changed the encrypted value by manually adding one or two characters . when i decrypt i got something like
id#100������������������������]u甀�+&�fj�W�ZЪS��d��]3�]"
and after i explode this i will get the id as 100���]u甀�+&�fj�W�ZЪS��d��]3�]"
now i select all raws with same id using .
SELECT * FROM table WHERE id=$id
it will select the recordes with id = 100 even the actual id is 100���]u甀�+&�fj�W�ZЪS��d��]3�]"
so weird . the type of the id column is INT may be that is why it is matched .
but from my point of view it is very bad , because my whole logic got incorrect because of this .
I checked this query both in Laravel and raw MySQL Query , the results are the same .
Any Ideas , Thanks in advance .
UPDATE
I understand the point mentioned by Shadow, but how to handle a
scenario like this , from decryption i expect id like 100 , but if i
got something like 1ASASAS, if you cast it to int it will be 1 (this
is just for example) . now the problem is my database has a user id 1
also , so now you can see how much trouble i am in because i will get an
incorrect user , sadly this is related to payment :P . some incorrect
users wallet will be topped up . ha ha .how to handle this
This is not a bug, this is a feature in MySQL described in Type Conversion in Expression Evaluation section of the MySQL manual:
When an operator is used with operands of different types, type conversion occurs to make the operands compatible. Some conversions occur implicitly. For example, MySQL automatically converts numbers to strings as necessary, and vice versa.
When MySQL converts a string to a number, it evaluates the characters starting from the left as long as the characters can be interpreted as part of a number and stops, if it encounters a character that cannot be considered as part of a number. In case of 100���]u甀�+&�fj�W�ZЪS��d��], the first � cannot be interpreted as a part of the number, so MySQL stops after 100. The characters after 100 seem to be some kind of garbage anyway and you should check your php code why it produces that garbage.
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
i have a row in my database with name "active_sizes" and i want filter my website items by size, for this, i use LIKE Condition in php :
AND active_sizes LIKE '%" . $_GET['size'] . "%'
but by using this code i have problem
for example when $_GET['size']=7.0 this code shows items that active_sizes=17.0
my active_sizes value looks like 17.0,5.0,6.5,7.5,,
thanks
Using comma-separated values in a single field in a database is indicative of bad design. You should normalize things, and have a seperate "item_sizes" table. As it stands now, you need a VERY ugly where clause to handle such sub-string mismatches:
$s = (intval)$_GET['size'];
... WHERE (active_sizes = $s) // the only value in the field
OR (active_sizes LIKE '$s%,') // at the beginning of the field
OR (active_sizes LIKE '%,$s,%') // in the middle of the field
OR (active_sizes LIKE '%,$s') // at the end of the field
Or, if you normalized things properly and had these individual values in their own child table:
WHERE (active_sizes_child.size = $s)
I know which one I'd choose to go with...
You don't state which DB you're using, but if you're in MySQL, you can temporarily accomplish the same thing with
WHERE find_in_set($s, active_sizes)
at the cost of losing portability. Relevant docs here: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set
You Have % signs around your $_GET value. Combined with LIKE, this means that any string that simply contains your get value will be retuned. If you want an exact match, use the = operator instead, without the percentage signs.
This will solve your immediate issue:
AND active_sizes LIKE '" . mysql_real_escape_string($_GET['size']) . "%'
If you are using the database other than MySQL, use corresponding escape function. Never trust input data.
Besides, I'd suggest using numeric field (DECIMAL or NUMERIC) for active_sizes field. This will accelerate your queries, will let you consume less memory, create queries like active_sizes BETWEEN 16.5 AND 17.5, and generally this is more correct data type for a shoe size.
I have a data set that is generated by a Zip Code range search:
$zips:
key -> value
11967 -> 0.5
11951 -> 1.3
The key is the Zip Code (Which I need to query the Database for), and the value is the miles from the user entered zip code. I need to take the key (Zip Code) and search the database, preferably using a MySQL query similar to my current one:
$getlistings = mysql_query("SELECT * FROM stores WHERE zip IN ($zips)");
The other alternative is to change the array somehow in my code. I tried looking in code for where the array is generated originally but I couldn't find it. Any help would be greatly appreciated!! Thanks :)
You could convert the array keys to a SQL-compatible string. For example:
'11967', '11951'
and then use the string in the query.
Since the SQL query doesn't know what a php array is and there's no good way (that I know of) to extract just the keys and surround them in quotes, so this may be your best bet.
EDIT: As Ionut G. Stan wrote (and gave an example for), using the implode and array_map functions will get you there. However, I believe the solution provided will only work if your column definition is numeric. Character columns would require that elements be surrounded by apostrophes in the IN clause.
array_keys should be what you're looking for.
$zip = array_keys($zips); # gives you simple array(11967, 11951);
implode(', ', $zip); # results in: '11967, 11951'
Cannot comment the other answers, so one additional remark from my side. Depending on the country you are in and what you do with the data... In Germany there are ZIP-Codes starting with "0" so you should make sure that you either do not store them as numerical value if you want to compare them to other data (e.g. ZIP <-> geocoord-mappings) or make sure that you convert them to int everywhere and use filtering on the output.
Old ZIP codes had four numbers, new ones have five. So displaying a new ZIP with four numbers because the leading 0 is missing will lead to confusion.
Regarding use of a temporary table i would say it depends on the size of the table and how many zip codes are used in the query.
This should do it:
// array_map sanitizes the data
$zip_codes = implode(', ', array_map('intval', array_keys($zips)));
$getlistings = mysql_query("SELECT * FROM stores WHERE zip IN ($zip_codes)");
For best performance, you should create a temporary table, fill it with your ZIP codes and query like this:
SELECT *
FROM stores
JOIN temptable
ON zip = tempvalue
Of course this will be more efficient only if you ZIP column is indexed.
I just want to throw in that the previous code snippets gave me some syntax errors and the database just spitted out one entry instead of all relevant data. The following snippet worked for me:
implode("','", $zipNumbers);