OldWest Posted November 19, 2010 Share Posted November 19, 2010 It's pretty simple to see what I am trying to do here. For some reason all results in the table are the same exact cityName replacing all existing records. The echoed results are correct. I've include a small dump of my table as well. $query = "SELECT cityName FROM sys_city_dev_2"; $resource = mysqli_query($cxn, $query) or die("MySQL error: " . mysqli_error($cxn) . "<hr>\nQuery: $query"); while($result = mysqli_fetch_assoc($resource)) { $nox = $result['cityName']; $toUpper = ucfirst($nox); echo "$toUpper" . "<br />"; $setString = "UPDATE sys_city_dev_2 SET cityName = '" . $toUpper ."' WHERE cityName != ''"; mysqli_query($cxn,$setString); } 100 Records of table dump (pre running my script above): -- -- Table structure for table `sys_city_dev_2_backup` -- CREATE TABLE IF NOT EXISTS `sys_city_dev_2_backup` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `Mid` int(11) NOT NULL DEFAULT '0', `cityName` varchar(30) NOT NULL DEFAULT '', `forder` int(4) NOT NULL DEFAULT '0', `disdplay` int(4) NOT NULL DEFAULT '0', `cid` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`ID`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=113970 ; -- -- Dumping data for table `sys_city_dev_2_backup` -- INSERT INTO `sys_city_dev_2_backup` (`ID`, `Mid`, `cityName`, `forder`, `disdplay`, `cid`) VALUES (84010, 1, 'dothan', 0, 0, 0), (84011, 1, 'alabaster', 0, 0, 0), (84012, 1, 'birmingham', 0, 0, 0), (84013, 2, 'flagstaff', 0, 0, 0), (84014, 1, 'auburn', 0, 0, 0), (84015, 1, 'florence', 0, 0, 0), (84016, 1, 'gadsden', 0, 0, 0), (84017, 1, 'huntsville', 0, 0, 0), (84018, 1, 'mobile', 0, 0, 0), (84019, 1, 'montgomery', 0, 0, 0), (84020, 1, 'tuscaloosa', 0, 0, 0), (84021, 2, 'mohave valley', 0, 0, 0), (84022, 2, 'phoenix', 0, 0, 0), (84023, 2, 'prescott', 0, 0, 0), (84024, 2, 'sierra vista', 0, 0, 0), (84025, 2, 'tucson', 0, 0, 0), (84026, 2, 'yuma', 0, 0, 0), (84027, 3, 'bakersfield', 0, 0, 0), (84028, 3, 'chico', 0, 0, 0), (84029, 3, 'fresno / madera', 0, 0, 0), (84030, 3, 'gold country', 0, 0, 0), (84031, 3, 'humboldt county', 0, 0, 0), (84032, 3, 'imperial', 0, 0, 0), (84033, 3, 'inland empire', 0, 0, 0), (84034, 3, 'los angeles', 0, 0, 0), (84035, 3, 'alhambra', 0, 0, 0), (84036, 3, 'merced', 0, 0, 0), (84037, 49, 'fayetteville', 0, 0, 0), (84038, 49, 'fort smith', 0, 0, 0), (84039, 49, 'jonesboro', 0, 0, 0), (84040, 49, 'little rock', 0, 0, 0), (84041, 49, 'arkadelphia', 0, 0, 0), (84042, 49, 'texarkana', 0, 0, 0), (84043, 3, 'modesto', 0, 0, 0), (84044, 3, 'alta sierra', 0, 0, 0), (84045, 3, 'alpine', 0, 0, 0), (84046, 3, 'pedley', 0, 0, 0), (84047, 3, 'redding', 0, 0, 0), (84048, 3, 'alondra park', 0, 0, 0), (84049, 3, 'sacramento', 0, 0, 0), (84050, 4, 'canon city', 0, 0, 0), (84051, 3, 'san luis obispo', 0, 0, 0), (84052, 3, 'santa barbara', 0, 0, 0), (84053, 3, 'stockton', 0, 0, 0), (84054, 3, 'aliso viejo', 0, 0, 0), (84055, 3, 'visalia', 0, 0, 0), (84056, 3, 'yuba city', 0, 0, 0), (84057, 4, 'boulder', 0, 0, 0), (84058, 4, 'colorado springs', 0, 0, 0), (84059, 4, 'denver', 0, 0, 0), (84060, 4, 'applewood', 0, 0, 0), (84061, 4, 'pueblo', 0, 0, 0), (84062, 4, 'air force academy', 0, 0, 0), (84063, 5, 'avon', 0, 0, 0), (84064, 5, 'hartford', 0, 0, 0), (84065, 5, 'new haven', 0, 0, 0), (84066, 5, 'ansonia', 0, 0, 0), (84067, 5, 'fairfield', 0, 0, 0), (84068, 7, 'daytona beach', 0, 0, 0), (84069, 7, 'sebastian', 0, 0, 0), (84070, 5, 'wallingford center', 0, 0, 0), (84071, 8, 'belvedere park', 0, 0, 0), (84072, 7, 'sarasota springs', 0, 0, 0), (84073, 7, 'sandalfoot cove', 0, 0, 0), (84074, 7, 'san carlos park', 0, 0, 0), (84075, 7, 'st. augustine', 0, 0, 0), (84076, 7, 'tallahassee', 0, 0, 0), (84077, 7, 'safety harbor', 0, 0, 0), (84078, 7, 'ruskin', 0, 0, 0), (84079, 8, 'athens-clarke county', 0, 0, 0), (84080, 8, 'atlanta', 0, 0, 0), (84081, 8, 'augusta-richmond county', 0, 0, 0), (84082, 8, 'brunswick', 0, 0, 0), (84083, 8, 'columbus', 0, 0, 0), (84084, 8, 'americus', 0, 0, 0), (84085, 8, 'acworth', 0, 0, 0), (84086, 8, 'valdosta', 0, 0, 0), (84087, 10, 'boise', 0, 0, 0), (84088, 10, 'ammon', 0, 0, 0), (84089, 10, 'moscow', 0, 0, 0), (84090, 10, 'blackfoot', 0, 0, 0), (84091, 10, 'twin falls', 0, 0, 0), (84092, 10, 'meridian', 0, 0, 0), (84093, 10, 'jerome', 0, 0, 0), (84094, 10, 'idaho falls', 0, 0, 0), (84095, 11, 'addison', 0, 0, 0), (84096, 10, 'garden city', 0, 0, 0), (84097, 10, 'eagle', 0, 0, 0), (84098, 10, 'chubbuck', 0, 0, 0), (84099, 10, 'caldwell', 0, 0, 0), (84100, 12, 'bloomington', 0, 0, 0), (84101, 12, 'evansville', 0, 0, 0), (84102, 12, 'fort wayne', 0, 0, 0), (84103, 12, 'indianapolis', 0, 0, 0), (84104, 12, 'muncie / anderson', 0, 0, 0), (84105, 12, 'lafayette / west lafayette', 0, 0, 0), (84106, 12, 'south bend / michiana', 0, 0, 0), (84107, 12, 'terre haute', 0, 0, 0), (84108, 12, 'northwest indiana', 0, 0, 0), (84109, 13, 'ames', 0, 0, 0); Quote Link to comment https://forums.phpfreaks.com/topic/219142-php-while-w-mysql-set-echoing-correct-but-not-setting-the-table-records-correct/ Share on other sites More sharing options...
PFMaBiSmAd Posted November 19, 2010 Share Posted November 19, 2010 Your WHERE condition in the UPDATE query is a bit generic and would match every row at once that is not an empty string. I suspect that you intended to use a unique id field from the SELECT query to match the correct row in the UPDATE query or perhaps the original value in the cityName column. Quote Link to comment https://forums.phpfreaks.com/topic/219142-php-while-w-mysql-set-echoing-correct-but-not-setting-the-table-records-correct/#findComment-1136417 Share on other sites More sharing options...
OldWest Posted November 19, 2010 Author Share Posted November 19, 2010 Your WHERE condition in the UPDATE query is a bit generic and would match every row at once that is not an empty string. I suspect that you intended to use a unique id field from the SELECT query to match the correct row in the UPDATE query or perhaps the original value in the cityName column. I think I get what you meant.. I got it working and I think the $id was the ticket. I guess on each iteration it checking the next id compared to the previous The purpose of the script was to make each word of the city name start with a capital letter. I am sure my script could have been written a lot tidier and more elegant with for loops and more arrays.. Can anyone make some recommendation to cut my script in half (in code size)? Here is what I have which IS working as expected: All cityNames are updated with ucfirst() first letter for and of 4 parts w/ explode(). I echoed it out to the screen to see my output when completed as well: $query = "SELECT id, cityName FROM sys_city_dev_2"; $resource = mysqli_query($cxn, $query) or die("MySQL error: " . mysqli_error($cxn) . "<hr>\nQuery: $query"); $i = 0; while ($result = mysqli_fetch_assoc($resource)) { $i++; $id = $result['id']; $cName = $result['cityName']; //$toUpper = ucfirst($cName); $pieces = explode(" ", $cName); if (isset($pieces['0'])) { $piece0 = ucfirst($pieces['0']); //$piece0 = $pieces['0']; //echo "$pieces[0]" . "<br />"; } else { $piece0 = ''; } if (isset($pieces['1'])) { $piece1 = ucfirst($pieces['1']); //echo "$pieces[1]" . "<br />"; } else { $piece1 = ''; } if (isset($pieces['2'])) { $piece2 = ucfirst($pieces['2']); //echo "$pieces[2]" . "<br />"; } else { $piece2 = ''; } if (isset($pieces['3'])) { $piece3 = ucfirst($pieces['3']); //echo "$pieces[3]" . "<br />"; } else { $piece3 = ''; } $fullString = "$piece0 $piece1 $piece2 $piece3"; echo "$fullString<br />"; $setString = "UPDATE sys_city_dev_2 SET cityName='$fullString' WHERE id='$id'"; mysqli_query($cxn, $setString); //echo "<pre>"; //print_r($pieces); //echo "</pre>"; } Quote Link to comment https://forums.phpfreaks.com/topic/219142-php-while-w-mysql-set-echoing-correct-but-not-setting-the-table-records-correct/#findComment-1136441 Share on other sites More sharing options...
seanlim Posted November 19, 2010 Share Posted November 19, 2010 $query = "SELECT id, cityName FROM sys_city_dev_2"; $resource = mysqli_query($cxn, $query) or die("MySQL error: " . mysqli_error($cxn) . "<hr>\nQuery: $query"); while($result = mysqli_fetch_assoc($resource)) { $fullString = implode(" ",(array_map("ucfirst", explode(" ", $result['cityName'])))); echo $fullString; $setString = "UPDATE sys_city_dev_2 SET cityName='$fullString' WHERE id='".$result['id']."'"; mysqli_query($cxn, $setString); } Quote Link to comment https://forums.phpfreaks.com/topic/219142-php-while-w-mysql-set-echoing-correct-but-not-setting-the-table-records-correct/#findComment-1136454 Share on other sites More sharing options...
OldWest Posted November 19, 2010 Author Share Posted November 19, 2010 $query = "SELECT id, cityName FROM sys_city_dev_2"; $resource = mysqli_query($cxn, $query) or die("MySQL error: " . mysqli_error($cxn) . "<hr>\nQuery: $query"); while($result = mysqli_fetch_assoc($resource)) { $fullString = implode(" ",(array_map("ucfirst", explode(" ", $result['cityName'])))); echo $fullString; $setString = "UPDATE sys_city_dev_2 SET cityName='$fullString' WHERE id='".$result['id']."'"; mysqli_query($cxn, $setString); } Man that is sweet and short. Very nice.. I have never used array_map() before, so going to study up on that. Quote Link to comment https://forums.phpfreaks.com/topic/219142-php-while-w-mysql-set-echoing-correct-but-not-setting-the-table-records-correct/#findComment-1136463 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.