我正在为价格检查设备编写一个小型Web应用程序,但坚持尝试使用ODBC通过PHP中的DB2数据库检索数据。
我收到这些错误(主要是关注加粗/第一次警告):
警告:odbc_exec():提供的资源不是有效的ODBC-Link 第43行的C:\ xampp \ htdocs \ db2 \ findme.php中的资源
警告:odbc_fetch_row()期望参数1是资源,布尔值 在第46行的C:\ xampp \ htdocs \ db2 \ findme.php中给出
警告:odbc_num_rows()期望参数1是资源,字符串 在第53行的C:\ xampp \ htdocs \ db2 \ findme.php中给出
以下是我的代码:
<html>
<head><title>Searching for item...</title>
</head>
<body bgcolor=#ffffff>
<?php
$username = "admin";
$password = "admin";
$hostname = "localhost";
$port = "50000";
$database="HLDB";
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;"."HOSTNAME=$hostname;PORT=$port;PROTOCOL=TCPIP;UID=$username;PWD=$password";
$conn = odbc_connect($conn_string, '', '');
echo "<h2>Search Results:</h2><p>";
//Retrieve input from find field on previous page and store in a variable
$find = $_POST['find'];
echo "User searched for: " . $find . "<br><br>";
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>Please scan for an item!";
exit;
}
// Otherwise we connect to our Database
if ($conn){
echo "Connection succeeded. \n";
odbc_close($conn);
}
else{
echo "Connection failed. \n";
}
//Now we search for our search term, in the field the user specified
$sql = "SELECT * FROM NULLID.ITEMS WHERE 'ITEM NO.' LIKE '%$find%'";
$rs=odbc_exec($conn,$sql);
//fetch tha data from the database
while(odbc_fetch_row($rs))
{
//display the data
echo odbc_result($rs, "Description 1"), "\n";
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=odbc_num_rows($sql);
if ($anymatches == 0)
{
echo "Sorry, but we are unable to find this product...<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
?>
</body>
</html>
答案 0 :(得分:0)
您是否尝试过打开连接?你的代码显示了odbc_close($ conn);验证连接成功后。您应该删除该行以保持连接打开,否则$ conn对后续odbc_exec()无效