简体   繁体   English

PHP函数/脚本,用于合并数据库中的2列并删除其特殊字符和空格

[英]php function/script for combining 2 columns in a database and remove their special characters and spaces

hi im trying to write a script that will update my database using php 嗨,我试图写一个脚本,将使用php更新我的数据库

i need to combine two columns from the database, remove their special characters and spaces and insert the result inside the third column of my database 我需要从数据库中合并两列,删除它们的特殊字符和空格,然后将结果插入数据库的第三列中

fn        ln       result
Super-     Man      sman
Bat&       Man      bman
Wonder -   Woman    wwoman

i cant seem to get the result i want with my script 我似乎无法通过脚本获得想要的结果

    <?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());    
    }

    ?>

can anyone help me? 谁能帮我? thanks in advance 提前致谢

*** EDIT * **** finally got it working *** 编辑 * ****终于可以使用了

    <?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());    
    }

    ?>

I see several issues in your code, but the biggest one to note is the following: 我在您的代码中看到了几个问题,但是要注意的最大问题是:

 $query = mysql_fetch_array($query);

The mysql_fetch_array function receives a result set and returns and array for the record to be fetched. mysql_fetch_array函数接收结果集,并返回要获取的记录的数组。 With this instruction, you are replacing your result set with the array, and if you lost the result set, you will not be able to fetch for further records. 通过此指令,您将用数组替换结果集,并且如果丢失结果集,则将无法获取进一步的记录。

By the way, you are not geting the next record within your loop. 顺便说一句,您没有在循环中获得下一条记录。 There's another problem I see. 我看到另一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM