我正在使用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>   <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>
由于我没有收到错误消息,我知道我的连接正常,但是目前,当我选择按钮时,数据没有按预期显示...请帮忙!感谢
答案 0 :(得分: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功能在这里至关重要。