PHP switch case 问题,总是returns 一个错误

PHP Switch case problem, always returns an error

我有一个网页,我在其中输入一个字符串,然后通过 $POST 它转到 PHP 脚本,在那里执行 SQL select 和 returns页面上的数据。对于一个 select 查询工作正常。但我也试图包含其他 Selects 语句,以便使用其他值进行搜索。

所以当我 运行 我的 php,(来自网页)没有开关时,它总是按照我的预期执行。没有错误。

 <html>
<!-- some style ... -->
<?php

$HOST="10.133.96.132";

// Database user
$DBUSER="tibco";

// Database password
$PASS="suyash123";

// Database name
$DB="VFD2TEST";

// Database Error - User Message
$DB_MSG_ERROR='Could not connect!<br />Please contact the site\'s administrator.';


############## Make the mysql connection ###########

$conn = oci_connect ($DBUSER, $PASS, "//10.133.96.132:7041/VFD2TEST");

if (!$conn) {
   $m = oci_error();
   echo $m['message'], "\n";
   exit;
}
$select123 = $_POST['value'];
$output123 = shell_exec("cat data.lst |grep $select123| awk -F: '{print }'");

switch ($output123) 
{
     case "QUERY1":
        $c = "SELECT * FROM EAITUXSERVICES WHERE TUX_ADAPTERS='".$_POST['value']."'";
         $result = oci_parse($conn, $c);
         oci_execute($result);
         break;
     case "QUERY2":
            $c = "SELECT * FROM EAITUXSERVICES WHERE COMPONENT='".$_POST['value']."'";
            $result = oci_parse($conn, $c);
            oci_execute($result);
         break;
     case "QUERY3":
        $c = "SELECT * FROM EAITUXSERVICES WHERE TUX_SERVICES='".$_POST['value']."'";
            $result = oci_parse($conn, $c);
            oci_execute($result);
         default:
         $c = "SELECT * FROM EAITUXSERVICES WHERE STATION_NAME='".$_POST['value']."'";
         $result = oci_parse($conn, $c);
         oci_execute($result);
         break;
 }

echo '<TABLE>';
echo '<thead>';
echo '<tr>
    <th>ADAPTERS</th>
    <th>SERVICE</th>
        <th>CALLING_APP</th>
        <th>BP_NAME</th>
        <th>STATION_NAME</th>
         <th style="padding-right:20px;">COMPONENT</th>
        </tr>';
echo '</thead>';
while (($row = oci_fetch_array($result, OCI_BOTH)) != false)
{

  echo '
  <tr>
    <td>'.$row[0].'</td>
    <td>'.$row[1].'</td>
    <td>'.$row[3].'</td>
    <td>'.$row[4].'</td>
    <td>'.$row[5].'</td>
    <td>'.$row[6].'</td>

</tr>';

}


echo '</TABLE>';

oci_free_statement($result);
oci_close($conn);

?>

</html>

以下输出为:

 $select123 = $_POST['value'];
    $output123 = shell_exec("cat data.lst |grep $select123| awk -F: '{print }'");

是 QUERY1

所以它应该切换

=================================编辑=========== =========================

我刚刚尝试使用 if else 语句,第一个查询无论如何都不会获取数据,尽管默认列出的查询工作正常并且是相同的查询。

============================================= ================================

================================EDIT2============ =============================

现在我已经修剪了变量,所以没有空格或特殊字符的问题

<html>
<!-- some style ... -->
<?php

$HOST="10.xxx.xxx.132";

// Database user
$DBUSER="tibco";

// Database password
$PASS="xxxxx";

// Database name
$DB="TEST";

// Database Error - User Message
$DB_MSG_ERROR='Could not connect!<br />Please contact the site\'s administrator.';


############## Make the mysql connection ###########

$conn = oci_connect ($DBUSER, $PASS, "//10.xxxx.96.xx:xxx/xxxx");

if (!$conn) {
   $m = oci_error();
   echo $m['message'], "\n";
   exit;
}
$select123 = $_POST['value'];
$output123 = shell_exec("cat data.lst |grep $select123| awk -F: '{print }'");
$output234 = trim($output123);
echo $output234;
if ($output234=="QUERY3")
{
$c = "SELECT * FROM EAITUXSERVICES WHERE COMPONENT='".$_POST['value']."'";  
}
else if ($output234=="QUERY1")
{

$c = "SELECT * FROM EAITUXSERVICES WHERE TUX_ADAPTERS='".$_POST['value']."'";
}   
$result = oci_parse($conn, $c);
oci_execute($result);

echo '<TABLE>';
echo '<thead>';
echo '<tr>
    <th>ADAPTERS</th>
    <th>SERVICE</th>
        <th>CALLING_APP</th>
        <th>BP_NAME</th>
        <th>STATION_NAME</th>
         <th style="padding-right:20px;">COMPONENT</th>
        </tr>';
echo '</thead>';
while (($row = oci_fetch_array($result, OCI_BOTH)) != false)
{

  echo '
  <tr>
    <td>'.$row[0].'</td>
    <td>'.$row[1].'</td>
    <td>'.$row[3].'</td>
    <td>'.$row[4].'</td>
    <td>'.$row[5].'</td>
    <td>'.$row[6].'</td>

</tr>';

}


echo '</TABLE>';

oci_free_statement($result);
oci_close($conn);

?>

</html>

现在我的输出分别是 QUERY1 和 QUERY3,但结果不存在,之前查找 STATION_NAME 的查询正在运行,现在查找 COMPONENT 的查询正在运行,而不是站名查询。真奇怪。

您可能错过了 $select123 中的 ['value'] 和 case "QUERY3 " 中的中断。

然后你可以像这样简化你的代码:

$conn = oci_connect ($DBUSER, $PASS, "//YY.XXX.ZZ.132:XXXX/TEST");

if (!$conn) {
   $m = oci_error();
   echo $m['message'], "\n";
   exit;
}
$select123 = $_POST;
$output123 = shell_exec("cat data.lst |grep $select123| awk -F: '{print }'");
$c = "SELECT * FROM EAITUXSERVICES WHERE ";
switch ($output123) {
     case "QUERY1 ":
        $c .= "TUX_ADAPTER";
     break;
     case "QUERY2 ":
        $c .= "COMPONENT";
     break;
     case "QUERY3 ":
        $c .= "TUX_SERVICES";
     break;
     default:
         $c .= "STATION_NAME";
     break;
 }
$c .= "='".$_POST['value']."'";
$result = oci_parse($conn, $c);
oci_execute($result);

echo '<TABLE>';
echo '<thead>';
echo '<tr>
    <th>ADAPTERS</th>
    <th>SERVICE</th>
        <th>CALLING_APP</th>
        <th>BP_NAME</th>
        <th>STATION_NAME</th>
         <th style="padding-right:20px;">COMPONENT</th>
        </tr>';
echo '</thead>';
while (($row = oci_fetch_array($result, OCI_BOTH)) != false)
{

  echo '
  <tr>
    <td>'.$row[0].'</td>
    <td>'.$row[1].'</td>
    <td>'.$row[3].'</td>
    <td>'.$row[4].'</td>
    <td>'.$row[5].'</td>
    <td>'.$row[6].'</td>

</tr>';

}


echo '</TABLE>';

oci_free_statement($result);
oci_close($conn);

?>

</html>

我无法 运行 使用 Switch 语句,但可以借助 if...else

这是代码

if (!$conn) {
   $m = oci_error();
   echo $m['message'], "\n";
   exit;
}
$select123 = $_POST['value'];
$output123 = shell_exec("cat data.lst |grep $select123| awk -F: '{print }'");
$output234 = trim($output123);
if ($output234=="QUERY3")
{
        $c = "SELECT * FROM EAITUXSERVICES WHERE COMPONENT='".$_POST['value']."'";  
        $result = oci_parse($conn, $c);
        oci_execute($result);
} 
//else if ($output234=="QUERY1")

    //{
        //$c = "SELECT * FROM EAITUXSERVICES WHERE TUX_ADAPTERS='".$_POST['value']."'"; 
        //$result = oci_parse($conn, $c);
        //oci_execute($result);

    //}

else if ($output234=="QUERY2")

    {
        $c = "SELECT * FROM EAITUXSERVICES WHERE TUX_SERVICES='".$_POST['value']."'";   
        $result = oci_parse($conn, $c);
        oci_execute($result);

    }

else

    {
        $c = "SELECT * FROM EAITUXSERVICES WHERE STATION_NAME='".$_POST['value']."'";   
        $result = oci_parse($conn, $c);
        oci_execute($result);

    }   

echo '<TABLE>';
echo '<thead>';
echo '<tr>
    <th>ADAPTERS</th>
    <th>SERVICE</th>
        <th>CALLING_APP</th>
        <th>BP_NAME</th>
        <th>STATION_NAME</th>
         <th style="padding-right:20px;">COMPONENT</th>
        </tr>';
echo '</thead>';
while (($row = oci_fetch_array($result, OCI_BOTH)) != false)
{

  echo '
  <tr>
    <td>'.$row[0].'</td>
    <td>'.$row[1].'</td>
    <td>'.$row[3].'</td>
    <td>'.$row[4].'</td>
    <td>'.$row[5].'</td>
    <td>'.$row[6].'</td>

</tr>';

}


echo '</TABLE>';

oci_free_statement($result);
oci_close($conn);

?>

</html>

希望这对某人有所帮助

我觉得真正的答案应该是 trim:

$output123 = trim(shell_exec("cat data.lst |grep $select123| awk -F: '{print }'"));

switch ($output123) 
{
...
}