returning result from database in drop down list with parse error

我想从数据库中的另一个表中检索下拉列表中的字段值形式的数据库,但我不断收到解析错误:语法错误,意外的T_IF

 <?php
      $result1 = mysql_query("SELECT `CompanyID`, `Name` FROM `company`") or trigger_error(mysql_error());

      while($row1 = mysql_fetch_array($result1)) { 
           foreach($row1 AS $key1 => $value1) {
                $row1[$key1] = stripslashes($value1);
           } 

           echo "<option value=" . nl2br( $row1['CompanyID']) . " ". if($row['CompanyID'] == $Merchant) echo 'selected = "selected"'
        . ">" . nl2br( $row1['Name']) . "</option>";
        } 
 ?>

Inside a echo statement you should use a ternary operator, its basically a shortened IF/Else statement.

Here is the code with a ternary operator:

<?php
  $result1 = mysql_query("SELECT `CompanyID`, `Name` FROM `company`") or trigger_error(mysql_error());

  while($row1 = mysql_fetch_array($result1)) {
      foreach($row1 AS $key1 => $value1) {
          $row1[$key1] = stripslashes($value1);
      }

      echo "<option value='" . nl2br( $row1['CompanyID']) . "' ". (($row['CompanyID'] == $Merchant)?' selected ':''). ">" . nl2br( $row1['Name']) . "</option>";
    }
?>

You can read more about ternary operators in PHP's documentation over here.


try this

if($row['CompanyID'] == $Merchant) {echo 'selected = "selected"';} to me it looks like you forgotten your curly brackets in your if statement


Please check this thread and do as follows if block inside echo statement?

in your case it should be something like this

. if($row['CompanyID'] == $Merchant) echo 'selected

changed to this

.(($row['CompanyID']==$Merchant)?'selected
链接地址: http://www.djcxy.com/p/69820.html

上一篇: 如何在HTML块内跳转PHP代码中的MySQLi行

下一篇: 从下拉列表中返回数据库结果,并带有解析错误