php - 将长名称列表读入 mysql 并查询名称

标签 php mysql sql

我有一个制表符分隔的文件,其中包含 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/

相关文章:

java - 将 Java Applet 上传到网页中

php - SELECT * FROM table WHERE(单元格中的值作为数组)= $searchid

mysql - 计数非零的 SQL

sql - 如何终止或回滚事件事务?

php - 在 Zend Framework 的 2 InputFilter 中有条件地需要

mysql - sql触发两个表不为空

java - 通过glassfish服务器访问mysql数据库时访问被拒绝

mysql - 如何在 MySQL 中选择具有给定标签的帖子的所有标签?

mysql - 从MySQL中的一个结果集中的多个表中获取数据

PHP header Location 在 HTML 中间工作