Corfitz.
Corfitz.

Reputation: 1884

Update sql with a loop (PDO)

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

Upvotes: 0

Views: 349

Answers (2)

Jalpesh Patel
Jalpesh Patel

Reputation: 3188

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..

Upvotes: 4

Brian Adkins
Brian Adkins

Reputation: 666

Try this:

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

No need for a loop...

Upvotes: 2

Related Questions