我有两张桌子即。 y2011_2012和y2012_2013在我的数据库fyproject ...
现在我创建了一个包含以下代码的搜索框:
<form action="includes/usn_search.php" method="post">
<input type="search" id="usn_search" name="usn_search">
</form>
现在我正在尝试使用php使用此代码从两个表中检索数据:
//1.Create a database connection
$connection = mysql_connect("127.0.0.1","root",DB_PASS);
if(!$connection)
{
die("Database connection failed: " . mysql_error());
}
//2.Select a database to use
$db_select = mysql_select_db("fyproject",$connection);
if(!$db_select){
die("Database selection failed: " . mysql_error());
}
//load
$usn = strtoupper($_POST["usn_search"]);
if($usn == ($result1= mysql_query("SELECT usn1 OR usn2 OR usn3 OR usn4 FROM y2011_2012 WHERE usn1=\"$usn\" OR usn2=\"$usn\" OR usn3=\"$usn\" OR usn4=\"$usn\"", $connection)))
{
$result1 = mysql_query("SELECT * FROM y2011_2012 WHERE usn1=\"$usn\" OR usn2=\"$usn\" OR usn3=\"$usn\" OR usn4=\"$usn\"", $connection);
if(!$result1){
die("Database query failed: ". mysql_error());
}
while($row = mysql_fetch_array($result1)){
echo "<head><title>".$row["filename"]."</title></head>";
echo "<body>";
echo "<h2><center>".$row["filename"]."</center></h2>";
echo $row["usn1"]." ".$row["name1"]."<br />";
echo $row["usn2"]." ".$row["name2"]."<br />";
echo $row["usn3"]." ".$row["name3"]."<br />";
echo $row["usn4"]." ".$row["name4"]."<br />";
echo "<h3><center>"."Abstract"."</center></h3>"."<br />";
echo $row["content"]."<br />";
echo "</body>";
}
}
else if($usn == ($result2= mysql_query("SELECT usn1 OR usn2 OR usn3 OR usn4 FROM y2012_2013 WHERE usn1=\"$usn\" OR usn2=\"$usn\" OR usn3=\"$usn\" OR usn4=\"$usn\"", $connection)))
{
$result2 = mysql_query("SELECT * FROM y2012_2013 WHERE usn1=\"$usn\" OR usn2=\"$usn\" OR usn3=\"$usn\" OR usn4=\"$usn\"", $connection);
if(!$result2){
die("Database query failed: ". mysql_error());
}
while($row = mysql_fetch_array($result2))
{
echo "<head><title>".$row["filename"]."</title></head>";
echo "<body>";
echo "<h2><center>".$row["filename"]."</center></h2>";
echo $row["usn1"]." ".$row["name1"]."<br />";
echo $row["usn2"]." ".$row["name2"]."<br />";
echo $row["usn3"]." ".$row["name3"]."<br />";
echo $row["usn4"]." ".$row["name4"]."<br />";
echo "<h3><center>"."Abstract"."</center></h3>"."<br />";
echo $row["content"]."<br />";
echo "</body>";
}
}
else {
echo "Not found!";
}
现在每当我尝试输入第一个表中存在的USN时,我得到了我想要的无缝输出...但是假设我把第二个表中存在的值我得到一个空白页面..来自php的错误页面或者说“未找到”的最后一个回声..我猜这个值没有传递给第二个如果条件但是我不知道为什么?请帮忙
答案 0 :(得分:1)
我真的不明白if子句在你的代码中是如何工作的。在if子句中,您将变量与一个永远不会相同的关联数组进行比较。从技术上讲,它应该不起作用。我稍微修改了你的代码,你可以尝试运行这个
<?php
//1.Create a database connection
$connection = mysql_connect("127.0.0.1","root",DB_PASS);
if(!$connection){
die("Database connection failed: " . mysql_error());
}
//2.Select a database to use
$db_select = mysql_select_db("fyproject",$connection);
if(!$db_select){
die("Database selection failed: " . mysql_error());
}
//load
$usn = strtoupper(mysql_real_escape_string($_POST["usn_search"]));
$result1 = mysql_query("SELECT * FROM y2011_2012 WHERE usn1=\"$usn\" OR usn2=\"$usn\" OR usn3=\"$usn\" OR usn4=\"$usn\"", $connection);
$result2 = mysql_query("SELECT * FROM y2012_2013 WHERE usn1=\"$usn\" OR usn2=\"$usn\" OR usn3=\"$usn\" OR usn4=\"$usn\"", $connection);
if(mysql_num_rows($result1)>0){
while($row = mysql_fetch_array($result1)){
echo "<head><title>".$row["filename"]."</title></head>";
echo "<body>";
echo "<h2><center>".$row["filename"]."</center></h2>";
echo $row["usn1"]." ".$row["name1"]."<br />";
echo $row["usn2"]." ".$row["name2"]."<br />";
echo $row["usn3"]." ".$row["name3"]."<br />";
echo $row["usn4"]." ".$row["name4"]."<br />";
echo "<h3><center>"."Abstract"."</center></h3>"."<br />";
echo $row["content"]."<br />";
echo "</body>";
}
}
else if(mysql_num_rows($result2)>0){
while($row = mysql_fetch_array($result2)){
echo "<head><title>".$row["filename"]."</title></head>";
echo "<body>";
echo "<h2><center>".$row["filename"]."</center></h2>";
echo $row["usn1"]." ".$row["name1"]."<br />";
echo $row["usn2"]." ".$row["name2"]."<br />";
echo $row["usn3"]." ".$row["name3"]."<br />";
echo $row["usn4"]." ".$row["name4"]."<br />";
echo "<h3><center>"."Abstract"."</center></h3>"."<br />";
echo $row["content"]."<br />";
echo "</body>";
}
}else {
echo "Not found!";
}
?>