将文件加载到数组中
问题描述:
对PHP很新颖,遇到了一些麻烦,并想知道是否有人可以提供帮助。我在查找数据库中的一组名称时遇到问题。当我只使用名称数组时,它会被正确处理。当我尝试阅读这些相同名称的文件时,只显示姓氏。我的问题是如何获得文件阵列显示的所有名称?将文件加载到数组中
请参阅下面的示例和代码。谢谢你的时间。
//This works -- Sample 1
$x = array("joe", "paul", "tom");
//This does not -- Sample 2
$x = file("name.txt"); //Same names from the array above are in this file
以下是下面的完整代码。
<html>
<head>
<title>Name Check</title>
</head>
<body>
<?php
$con = mysql_connect("localhost", "xxxxxxxx", "xxxxxxxx");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("xxxxxxxxxx", $con);
// $x=array("joe", "paul", "tom"); //displays all names correctly in Sample 1 below
$x = file("name.txt"); // only displays the last name in the file. Sample 2 below
foreach ($x as $test) {
$result = mysql_query("SELECT * FROM categories WHERE Name='$test' Limit 0, 1");
while ($row = mysql_fetch_array($result)) {
echo $row['Name'];
echo " " . $row['Sport'];
echo "<br />";
}
}
?>
</body>
</html>
样品1输出
joe basketball
paul basketball
tom baseball
样品2输出
tom baseball
这里是内容name.txt,这是要求。谢谢!
joe
paul
tom
你能提供的示例文件内容是什么? – 2011-03-03 23:38:23
请发布文件的格式。 – 2011-03-03 23:38:30