1

我正在使用 ODBC 连接而不是 MySQL。我有一个搜索功能,我或多或少地复制了它以应用于我的 ODBC 连接,但是它们不起作用。这是我的代码,除了连接:

<!doctype html>
<html>
<title> Quoting System </title>
<head>
</head>
<body>

<form class="form" method="POST" action='try31.php'>
        Quote Number <input  class="form-control" type="text" name="quote" id="quote" placeholder="Enter Quote Number">
        <br> &nbsp <input class="btn btn-default" type="submit" name="search" value="Search">
</form>     

<table>
    <tr>
        <th>Company Name</th>
        <th>Address1</th>
        <th>Address2</th>
    </tr>   

<?php

  if (!$conn){
    if (phpversion() < '4.0'){
      exit("Connection Failed: . $php_errormsg" );
    }
    else{
      exit("Connection Failed:" . odbc_errormsg() );
    }
}

if(isset($_POST['search'])){ 
$quote = $_POST['quote'];
$query = "SELECT * FROM dbo.tblVersions2 WHERE QuoteNumber LIKE '".$quote."'";
}

$result = odbc_exec($conn,$query);

    while($row =odbc_fetch_row($result)){
        echo "<tr>";
            echo "<td>".$row[2]."</td>";
            echo "<td>".$row[3]."</td>"; 
            echo "<td>".$row[4]."</td>";
        echo "</tr>";
}



// Disconnect the database from the database handle.
//odbc_close($conn);

?>
</table>
</body>
</html>

由于我没有收到错误消息,我知道我的连接正常,但是目前,当我选择按钮时,数据没有按预期显示...请帮助!谢谢

4

1 回答 1

0

发现使用 MS SQL ODBC 连接查询语法与 MySQL 不同。我更改了在表中调用列的位置:

$result = odbc_exec($conn,$query);

while($row =odbc_fetch_row($result)){
    echo "<tr>";
        echo "<td>".$row[2]."</td>";
        echo "<td>".$row[3]."</td>"; 
        echo "<td>".$row[4]."</td>";
    echo "</tr>";

}

对此:

 $result = odbc_exec($conn, $stmt);

  while (odbc_fetch_row($result)) // while there are rows
  {
     echo "<tr>";
        echo "<td>" . odbc_result($result, "CompanyName") . "</td>";
        echo "<td>" . odbc_result($result, "Address1") . "</td>";
     echo "</tr>";
  }

odbc_result 函数在这里至关重要。

于 2016-07-14T08:53:51.547 回答