如何使用条件语句将两个搜索结果语句作为一个语句?

时间:2013-11-19 13:07:58

标签: php html mysql sql

我需要将这两个单独的语句作为一个语句使用条件ifelseif,或者甚至是任何其他条件语句来执行任何两个查询并呈现结果。

<?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);

?>

2 个答案:

答案 0 :(得分:0)

差异是

$doctor = $_GET["txt_doctor"];

您可以先检查$_GET["txt_doctor"];,然后使用下面的代码

if(isset( $_GET["txt_doctor"]))
{
  // second statement
}else
{
// first statement

}

答案 1 :(得分:0)

你的问题很难理解。 格式化您的代码帮助我更了解您的问题,但是......

第一次查询

    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过滤器在那里两次。

问题 - 看起来你真的不想加入两个查询,你希望它们根据某些内容单独执行。我需要知道决定运行哪个查询的内容。

  If ($query = "run query 1")  { // run query 1... } 
                          else { // run query 2... }

您没有在初始帖子中提供足够的信息来编写该逻辑。