0

If somebody would please take a look at my code and tell what to do with it. I thought it was right but obviously it is not.

<? require_once("dblogin.php");
$sth = $conn->prepare("SELECT * FROM country_city_zip WHERE country = US");
$sth->execute();
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) { 

$sql ="UPDATE country_city_zip SET uni = '".$row['country']."-".$row['zip']."' WHERE country = '".$row['country']."' AND zip = '".$row['zip']."'"; 
$count = $conn->exec($sql);

} ?>

I want to create a unique ID based on the country code and the zip code: ex. US-28172

2 Answers 2

4

Try this query

$sql= "UPDATE country_city_zip SET uni = '".concat($row['country'],"-",$row['zip'])."' WHERE country = '".$row['country']."' AND zip = '".$row['zip']."'";

I assume that you have problem in the sql query and based on i will post my answer

I hope problem is solved and you get any problem then let me know..

Sign up to request clarification or add additional context in comments.

3 Comments

I don't seem to get any problems, but it still won't update the data in the table.
please echo your query and check what value are come and still you are not get then put the query over here @Philip Jens Bramsted
it doesn't do anything.. but it works when I just use the sentence in the SQL field in the Database instead... No script needed. Thanks anyway..
2

Try this:

UPDATE country_city_zip SET uni = Concat(country,'-',zip)

No need for a loop...

4 Comments

So basically you just want me to do like this: $sql = "UPDATE country_city_zip SET uni = Concat(country,'-',zip)"; $count = $conn->exec($sql);
Yep... Never test out proposed solutions on real data though... Or at least have a backup available... Cant be too safe.
This was actually the solution.. somehow.. I choose not to create a script to do it for me.. Because when I just typed that sentence you wrote - into the SQL field in the database it did exactly what I wanted.. i t just seems to be difficult if you want a script to do it for you... Thanks!
The query suggested by @BrianAdkins will update all the rows in your table. In the select statement above you seem to fetch records for which the value of the country column is "US", so I would suggest that you add a where clause to the query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.