Bug #13922 Updated: FORMAT() output in MySQL can't use str_replace
| From: | ron at imperators dot ca | Date: | Sun, 04 Nov 2001 21:19:42 +0000 |
| Subject: | Bug #13922 Updated: FORMAT() output in MySQL can't use str_replace | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-69615@lists.php.net to get a copy of this message | ||
ID: 13922
User updated by: ron@imperators.ca
Reported By: ron@imperators.ca
Status: Bogus
Bug Type: MySQL related
Operating System: Redhat 7.1
PHP Version: 4.0.5
New Comment:
Oops. Not enough coffee. ;)
Previous Comments:
------------------------------------------------------------------------
[2001-11-04 16:15:49] jimw@php.net
$TimeIndex = str_replace(",","",$TimeIndex);
str_replace doesn't modify the string you give it.
------------------------------------------------------------------------
[2001-11-04 16:13:45] ron@imperators.ca
TO reproduce the problem:
Run a mysql query with the following:
SELECT FORMAT((1004763843/1800),0) as TimeIndex
This will yeild 558,202. (Including the comma).
As output from the sample script shows (see below for script):
Before str_replace = 558,202
This needs to be converted to type int, but doing a settype to integer will return only 558. So any
resourceful (pronounced "lazy") php programmer would suggest using str_replace to remove
the comma. This does not work:
After str_replace = 558,202
So doing the settype still yeilds this:
After settype = 558
Is there a way to have settype deal with commas (depending on the locale of course)? Of course the
real solution is to use the MYSQL FLOOR, CEILING and ROUND functions, which I have done, but it was
still perplexing as to why str_replace wouldn't work correctly on this string. The database
library used here is phpDB.
Script:
$query = "SELECT FORMAT((1004763843/1800),0) as TimeIndex";
echo $query."<br>\n";
$rs = $db->execute($query);
$TimeIndex = $rs->fields["TimeIndex"];
echo "Before str_replace = $TimeIndex <BR>\n";
str_replace(",","",$TimeIndex);
echo "After str_replace = $TimeIndex <BR>\n";
settype($TimeIndex,"integer");
echo "After settype = $TimeIndex <BR>\n";
------------------------------------------------------------------------
Edit this bug report at http://bugs.php.net/?id=13922&edit=1