0

I'm trying to assign a field called 'uniqueid' to each row present in my database. There are roughly 188,000 rows available.

Here is the code I am using:

$connection = mysql_connect("localhost","root","root");
mysql_select_db("comics",$connection) or die ("Database not found");

$query = mysql_query("select * from comic_issues"); 

if ($query){
  $rows = mysql_num_rows($query);

  foreach($rows as $row){
    $str = strtolower($row['series'].$row['volume'].$row['issue']);
    $str = preg_replace('/[^A-Za-z0-9]/', '', $str);

    $update = "update comic_issues set uniqueid='" . $str . "' where id='" . $row['ID'] . "'";
    mysql_query($update);

    exit();
  }
}

What is happening is that every row gets updated with the same uniqueid, which seems to be a different value each time I run the script.

4

2 回答 2

1

代替

foreach($rows as $row){

你需要做

while ($row = mysql_fetch_row($query)) {

在您的代码$rows中是一个数字而不是一个数组,因此您不能对其进行 foreach 。

于 2013-10-24T21:13:58.587 回答
1

让 MySQL 为你做这一切:

UPDATE comic_issues SET uniqueid = LOWER(CONCAT(series,volume,issue))

如果您还必须去除所有非字母数字字符,您可以编写存储函数或使用 UDF。

于 2013-10-24T21:12:06.433 回答