我需要循环遍历特定字段的每一行,获取其内容,通过解析器运行它,并将结果放回字段中。我很少需要钻研这么远的东西,所以我的工作示例将非常感激。这是我到目前为止的进展(请不要笑!):
<?
function parse_string($text){
// do something with $text...
return $text
}
$username="abcd";
$password="1234";
$database="abcdefg";
$table="table2"
$field="field3"
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
// And for the part I'd like help with...
// Repeat through each row of $table, parsing the content of $field
// via the function parse_string($text) and placing the result back
// into the database (replacing $field)
mysql_close();
?>
function parse_string($text){
// do something with $text...
return $text
}
$result = mysql_query("SELECT * FROM [ TABLE NAME ]");
while(($row = mysql_fetch_assoc($result)){
$myresult[] = $row;
}
array_walk( $myresult, 'parse_string' );
我建议在你的更新查询中使用"CASE THEN"语句(它只是一个查询,而不是像下面这样的几个查询)!
UPDATE [ TABLE NAME ] SET [FIELD] = CASE `id` WHEN 1 THEN XXXXX ...... END,
[FIELD] = CASE `id` WHEN 1 THEN XXXXX ...... END,
.
.
.
.
WHERE ..........
下面是一个简单的例子:
$result = mysql_query("SELECT * FROM mytable;");
while(($row = mysql_fetch_array() != null){
echo $row["table_column"];
}
如果你想有效地改变表的值,你必须在查询
UPDATE mytables SET mycolumn = mycolumn + 1 WHERE mycolumn < 10;