Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert Degree Symbol in database through PHP

Hi StackOverflow friends,

I am storing temperature in my database but I cannot add the degree symbol. My code is:

$que = "Insert into tblTempForecast (DateTimePosted, Now, Tomorrow) VALUES(now(),'$CloseLow1 ° - $CloseHi1 °','$CloseLow2 ° - $CloseHi2 °')";
$insertTemp = mysql_query($que);

My $CloseLow1 is equal to 15 and $CloseHi1 is 20. $CloseLow2 is 18 and $CloseHi2 is 22.

However, with my code above, it only inserts the $CloseLow1 and CloseLow2. It is supposed to insert the ff on the database:

ID (auto)              DateTimePosted             Now            Tomorrow
1                      2013-11-18 15:25:48       15°-20°         18°-22°
like image 364
Sarah Avatar asked Dec 20 '22 21:12

Sarah


1 Answers

  1. Don't use mysql_* functions, because they're deprecated. Learn about PDO instead, which is the way to do things nowadays.
  2. Do not save HTML-encoded characters. Always store them as UTF-8 (or your preferred charset, even if i suggest you UTF-8 anyway)
  3. If you store them as plain UTF-8 characters this issue will solve itself at once. Just remember that you need to call htmlentities or htmlspecialchars on the fetched values.
  4. Why are you storing your values as strings, rather than 4 separated fields? You'll be able to have much more flexibility (you can now make averages and all sorts of calculations with integers that you can't with strings) and you can leave the ° issue to your page rendering script rather than wrestle with DB character encodings.

Sorry if I don't solve directly your issue, but I think that you'll be better off in the long run if you take the high road and tackle the root of the problem rather than the superficial issue.

Expecially n° 4. Really, consider that.

like image 194
STT LCU Avatar answered Dec 24 '22 00:12

STT LCU