如何使用条件语句将两个搜索结果语句合二为一?
Posted
技术标签:
【中文标题】如何使用条件语句将两个搜索结果语句合二为一?【英文标题】:How can I put two search results statement as one using a conditional statement? 【发布时间】:2013-12-03 01:21:49 【问题描述】:我需要这两个单独的语句成为一个语句,使用条件 if
或 elseif
或什至任何其他将执行两个查询中的任何一个并呈现结果的条件语句。
<?php
require ($_SERVER["DOCUMENT_ROOT"]."/medikconfig/db_medicpharm.php");
$connection = @mysql_connect($db_host, $db_user, $db_password) or die("error connecting");
mysql_select_db($db_name, $connection);
$doctor = $_GET["txt_doctor"];
$len = strlen($doctor);
$states = $_GET["txt_state"];
$specialize = $_GET["txt_specialize"];
if ($len > 0)
$email = $_GET["txt_email"];
$hospital = $_GET["txt_hospital"];
$state = $_GET["txt_state"];
$org = $_GET["txt_org"];
$link = $_GET["txt_web"];
$query = "INSERT INTO doctor_locator (autoID, `Name of Doctor`, `Email`, `Hospital of Practise`,`State`,`Specialization`, `Professional Organization`, `webpage`)
VALUES (NULL,'$doctor','$email','$hospital','$state','$specialize','$org', '$link')";
mysql_query($query, $connection) or die (mysql_error());
?>
第一句话
<?php
$specialize = $_GET["txt_specialize"];
$states = $_GET["txt_state"];
$link = $_GET["txt_web"];
$sql = "SELECT `autoID`, `Name of Doctor`, `Email`, `Hospital of Practise`, `State`,
`Specialization`, `Professional Organization`, `webpage`
FROM `doctor_locator`
WHERE state = '$states' AND
specialization = '$specialize' ";
$retval = mysql_query( $sql);
if (!$retval)
die('Could not get data: ' . mysql_error());
while($row = mysql_fetch_array($retval))
echo "$row['autoID']. ".
"Name of Doctor: <a href = \"$row['webpage']\">$row['Name of Doctor'] </a><br>"
."Email: $row['Email'] <br>".
"Hospital of Practise: $row['Hospital of Practise']
<br>".
"State: $row['State'] <br>" .
"Specialization: $row['Specialization'] <br> ";
mysql_free_result($retval);
echo "Fetched data successfully\n.<br/>";
echo "<a href=home.php>go back to homepage</a>";
mysql_close($connection);
?>
第二个陈述
<?
$specialize = $_GET["txt_specialize"];
$states = $_GET["txt_state"];
$link = $_GET["txt_web"];
$sqldoc = "SELECT `autoID`, `Name of Doctor`, `Email`, `Hospital of Practise`, `State`,
`Specialization`, `Professional Organization`, `webpage`
FROM `doctor_locator`
WHERE `name of doctor` LIKE 'Dr%' AND state = '$states'";
$retvaldoc = mysql_query( $sqldoc);
if (!$retvaldoc)
die('Could not get data: ' . mysql_error());
while($row = mysql_fetch_array($retvaldoc))
echo "$row['autoID']. "."Name of Doctor: <a href = \"$row['webpage']\">$row['Name of Doctor'] </a><br>"
."Email: $row['Email'] <br>". "Hospital of Practise: $row['Hospital of Practise']
<br>". "State: $row['State'] <br>" . "Specialization: $row['Specialization'] <br> ";
mysql_free_result($retvaldoc);
echo "Fetched data successfully\n.<br/>";
echo "<a href=home.php>go back to homepage</a>";
mysql_close($connection);
?>
【问题讨论】:
我不知道应该检查哪个条件:P 此代码中可能存在 SQL 注入漏洞。 我有两个声明,我希望它们只是一个声明。但我不知道用于使两个查询都起作用的条件语句。 您需要加入查询吗? 是的,我需要加入查询。 【参考方案1】:区别是
$doctor = $_GET["txt_doctor"];
您可以先检查$_GET["txt_doctor"];
,然后使用下面的代码
if(isset( $_GET["txt_doctor"]))
// second statement
else
// first statement
【讨论】:
$doctor = $_GET["txt_doctor"];不是代码的一部分,我的错误。请你给我一个解决方案而不参考 $doctor = $_GET["txt_doctor"];谢谢 你如何决定运行 whixh 查询? 我希望它以我的方式运行,如果我查询这两个语句中的任何一个,它都会给我那些单独的结果。【参考方案2】:你的问题很难理解。 格式化您的代码帮助我更深入地理解了您的问题...
第一次查询
SELECT `autoID`, `Name of Doctor`, `Email`, `Hospital of Practise`, `State`,
`Specialization`, `Professional Organization`, `webpage`
FROM `doctor_locator`
WHERE state = '$states' AND
specialization = '$specialize'
第二次查询
SELECT `autoID`, `Name of Doctor`, `Email`, `Hospital of Practise`, `State`,
`Specialization`, `Professional Organization`, `webpage`
FROM `doctor_locator`
WHERE `name of doctor` LIKE 'Dr%' AND state = '$states'
此查询仅过滤两件事。医生的名字以Dr
开头,以$states
开头。这对我来说似乎是错误的......但这是您提供的代码。似乎应该有一个您要过滤的名称。
合并
$sql = "SELECT `autoID`, `Name of Doctor`, `Email`,
`Hospital of Practise`, `State`,
`Specialization`, `Professional Organization`, `webpage`
FROM `doctor_locator`
WHERE state = '$states' AND
specialization = '$specialize' AND
`name of doctor` LIKE 'Dr%'
$states
过滤器在那里出现了两次。
question - 似乎您并不真的希望这两个查询加入,您希望它们根据某些情况单独执行。我需要知道是什么决定了运行哪个查询。
If ($query = "run query 1") // run query 1...
else // run query 2...
您在最初的帖子中没有提供足够的信息让我编写该逻辑。
【讨论】:
非常感谢,但我仍然需要您的帮助。您给我的答案仅适用于一部分,但另一部分未提供所需的结果。 WHERE 子句适用于 state = '$states' AND specialization = '$specialize' 它不适用于哪一部分?根据您问题中提供的代码,我不知道还要添加什么。你需要澄清你的问题。我不明白你最初的帖子或你的要求。如果你更清楚,我可以给你更好的答案。以上是关于如何使用条件语句将两个搜索结果语句合二为一?的主要内容,如果未能解决你的问题,请参考以下文章