Jump to content

Cannot get SET to work properly w/ Array values


OldWest

Recommended Posts

I cannot get my script to update table: locations_all column city_state to the full state name.. Here's what I've been hacking at for 4.5 hours now! Ready to hit the sack.. Thanks for any input:

 

$state_list = array('AL' => "Alabama", 'AK' => "Alaska", 'AZ' => "Arizona", 'AR' => "Arkansas", 'CA' => "California", 'CO' => "Colorado", 'CT' => "Connecticut", 'DE' => "Delaware", 'DC' => "District Of Columbia", 'FL' => "Florida", 'GA' => "Georgia", 'HI' => "Hawaii", 'ID' => "Idaho", 'IL' => "Illinois", 'IN' => "Indiana", 'IA' => "Iowa", 'KS' => "Kansas", 'KY' => "Kentucky", 'LA' => "Louisiana", 'ME' => "Maine", 'MD' => "Maryland", 'MA' => "Massachusetts", 'MI' => "Michigan", 'MN' => "Minnesota", 'MS' => "Mississippi", 'MO' => "Missouri", 'MT' => "Montana", 'NE' => "Nebraska", 'NV' => "Nevada", 'NH' => "New Hampshire", 'NJ' => "New Jersey", 'NM' => "New Mexico", 'NY' => "New York", 'NC' => "North Carolina", 'ND' => "North Dakota", 'OH' => "Ohio", 'OK' => "Oklahoma", 'OR' => "Oregon", 'PA' => "Pennsylvania", 'RI' => "Rhode Island", 'SC' => "South Carolina", 'SD' => "South Dakota", 'TN' => "Tennessee", 'TX' => "Texas", 'UT' => "Utah", 'VT' => "Vermont", 'VA' => "Virginia", 'WA' => "Washington", 'WV' => "West Virginia", 'WI' => "Wisconsin", 'WY' => "Wyoming");
      foreach ($state_list as $key => $value) {
          // get all state abbr and name values.
          while ($row = mysqli_fetch_array($result)) {
              $query2 = "UPDATE locations_all SET city_state=$value WHERE city_state=$key";
              $result2 = mysqli_query($cxn, $query2) or die("MySQL error: " . mysqli_error($cxn) . "<hr>\nQuery: $query2");
          } //while ($row = mysqli_fetch_array($result))
      } //foreach ($state_list as $key => $value)

 

I cannot get my script to update table: locations_all column city_state to the full state name.. Here's what I've been hacking at for 4.5 hours now! Ready to hit the sack.. Thanks for any input:

 

$state_list = array('AL' => "Alabama", 'AK' => "Alaska", 'AZ' => "Arizona", 'AR' => "Arkansas", 'CA' => "California", 'CO' => "Colorado", 'CT' => "Connecticut", 'DE' => "Delaware", 'DC' => "District Of Columbia", 'FL' => "Florida", 'GA' => "Georgia", 'HI' => "Hawaii", 'ID' => "Idaho", 'IL' => "Illinois", 'IN' => "Indiana", 'IA' => "Iowa", 'KS' => "Kansas", 'KY' => "Kentucky", 'LA' => "Louisiana", 'ME' => "Maine", 'MD' => "Maryland", 'MA' => "Massachusetts", 'MI' => "Michigan", 'MN' => "Minnesota", 'MS' => "Mississippi", 'MO' => "Missouri", 'MT' => "Montana", 'NE' => "Nebraska", 'NV' => "Nevada", 'NH' => "New Hampshire", 'NJ' => "New Jersey", 'NM' => "New Mexico", 'NY' => "New York", 'NC' => "North Carolina", 'ND' => "North Dakota", 'OH' => "Ohio", 'OK' => "Oklahoma", 'OR' => "Oregon", 'PA' => "Pennsylvania", 'RI' => "Rhode Island", 'SC' => "South Carolina", 'SD' => "South Dakota", 'TN' => "Tennessee", 'TX' => "Texas", 'UT' => "Utah", 'VT' => "Vermont", 'VA' => "Virginia", 'WA' => "Washington", 'WV' => "West Virginia", 'WI' => "Wisconsin", 'WY' => "Wyoming");
      foreach ($state_list as $key => $value) {
          // get all state abbr and name values.
          while ($row = mysqli_fetch_array($result)) {
              $query2 = "UPDATE locations_all SET city_state=$value WHERE city_state=$key";
              $result2 = mysqli_query($cxn, $query2) or die("MySQL error: " . mysqli_error($cxn) . "<hr>\nQuery: $query2");
          } //while ($row = mysqli_fetch_array($result))
      } //foreach ($state_list as $key => $value)

 

To clarify, I am simply trying to swap all state abbreviations with the state full names e.g: AL would be SET to Alabama in the city_state field of locations_all table..

Oh, and I just noticed. What is the while loop there for?

 

Thanks for the tips on this.. I'm at the point in my php knowledge where I'm just about very dangerous :) .. Don't know why I am doing what I am doing all of the time!

 

I updated the SET to include the ' 's around the variable names. That did not seem to change anything. I'm going to keep hacking on it until I get a new error  :confused:

I forgot to include my db connection in my last try

 

In case you haven't seen it posted in the forum, you should be learning php, developing php code, and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON (set in your master php.ini when ever possible) so that all the php errors will be reported and displayed. You will save a ton of time. There would have been errors alerting you that no connection existed.

 

 

I forgot to include my db connection in my last try

 

In case you haven't seen it posted in the forum, you should be learning php, developing php code, and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON (set in your master php.ini when ever possible) so that all the php errors will be reported and displayed. You will save a ton of time. There would have been errors alerting you that no connection existed.

 

I have it set that way (actual read an earlier reply from you to another I believe), and I realized after my last post I was loading the wrong file in my local host! So it was dual errors on my end that lead to total confusion! Thanks for reinforcing good practice!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.