我有一个制表符分隔的文件,其中包含 3000 个名字和姓氏
bob schmuckatelli
jane doe
然后我还有一个包含 300 万条记录的数据库
id Fname Lname addr something
214 jack frost 444 snow rd data data
我很确定最好的方法是使用 tmp db 来做到这一点,但我的 mysql 有点生锈了。 如何在数据库中查找姓名?我想找到它们而不是插入它们。
最佳答案
您要做的就是打开文件,然后检查数据库中的每个条目。大致如下:
$file = fopen('names.txt','r'); //change to whatever name it is
$firstname = ''; $lastname = ''; $index = -1; $id = -1;
$tab = ' '; //change this to the delimiter, of course
while ($line = fgets($file)) {
$index = strrpos($line, $tab);
$firstname = substr($line, 0, $index);
$lastname = substr($line, $index + strlen($tab));
$id = $sql->query("SELECT id FROM db_name WHERE Fname='" . $firstname . "' AND Lname='" . $lastname . "'");//query the firstname/lastname. Change db_name to whatever and I'm assuming you don't need to sanitize.
//do something with the id?
}
fclose($file);
关于php - 将长名称列表读入 mysql 并查询名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23001210/