嗨,我正在尝试编写一个脚本,该脚本将使用 php 更新我的数据库
我需要合并数据库中的两列,删除它们的特殊字符和空格,然后将结果插入数据库的第三列
fn ln result
Super- Man sman
Bat& Man bman
Wonder - Woman wwoman
我似乎无法用我的脚本得到我想要的结果
<?php
include('connect2.php');
$query = mysql_query("SELECT * from alumni");
$query = mysql_fetch_array($query);
while($query){
$query[ln] = str_replace(" ", "", $query[ln]);
$query[fn] = str_replace(" ", "", $query[fn]);
$query[ln] = ereg_replace("[^A-Za-z0-9]", "", $query[ln]);
$query[fn] = ereg_replace("[^A-Za-z0-9]", "", $query[fn]);
$query[ln] = strtolower($query[ln]);
$query[fn] = strtolower($query[fn]);
$query[fn] = substr($query[fn],0,1);
printf($username = $query[fn] . $query[ln]);
mysql_query("UPDATE alumni SET username='$username' WHERE alumni_id = $query[alumni_id]") or die(mysql_error());
}
?>
谁能帮我?提前致谢
***编辑* **** 终于让它工作了
<?php
include('connect2.php');
$b = mysql_query("SELECT * from alumni");
while($a = mysql_fetch_array($b)){
//echo $a[fn];
$ln = str_replace(" ", "", $a[ln]);
$fn = str_replace(" ", "", $a[fn]);
$ln = ereg_replace("[^A-Za-z0-9]", "", $ln);
$fn = ereg_replace("[^A-Za-z0-9]", "", $fn);
$ln = strtolower($ln);
$fn = strtolower($fn);
$fn = substr($fn,0,1);
printf($username = $fn . $ln);
mysql_query("UPDATE alumni SET username='$username' where alumni_id = $a[alumni_id]") or die(mysql_error());
}
?>