dousha7645 2013-04-12 14:49
浏览 36

PHP / SQL - 读取文本文件如果一行等于数据库值echo用户名?

I have a text file file over 10k lines of random names (names.txt), and a Database containing user information (user_info)

The user_info database is structured similar to this:

user_id, name,           email,             DoB,           gender

   1,    John,           email@email.com,   01/01/1990       M
   2,    Tim,            email2@email.com,   01/01/1991       M

And so on..

What I'm basically trying to achieve is a name search using a txt document.

so when you upload the text document if a a name in there is 'Tim' it'll echo all the users called Tim in the user_info database along with their email, Date of Birth and gender.

(I have no Idea how to do this but I'm guessing something like this)

<form action="search.php" method="post" >
                         <input type="hidden" name = "submit" value="Search" />
                </form>


 <?php

 $namestxt = file("/names.txt");

     if (isset($_POST['submit']))
{


      $getUsers = mysql_query("SELECT * FROM `user_info` WHERE `name` = '".$namestxt."'");

  while ($userInfo = mysql_fetch_assoc($getUsers))
  {

   echo $userInfo['name'].' . $userInfo['email'] . $userInfo['DoB']  . $userInfo['gender']<br/>';

  }


}


  ?>

I know this code is probably way off, but hopefully you can understand what I am trying to do here.

Any help, thanks.

  • 写回答

3条回答 默认 最新

  • doulan8330 2013-04-12 14:57
    关注

    $namestxt is an array of strings read from your file. You can address elements by index: $namestxt[0] (first line in file). Or iterate them with foreach. You could then compose a SQL query select * from user_info where name in('Name1','Name2').

    评论

报告相同问题?