简体   繁体   中英

PHP DB2 odbc_exec() errors

I'm writing a little web app for a price checker device but stuck at trying to retrieve the data using ODBC via a DB2 database in PHP.

I'm getting these errors (mainly concerned about the bolded/first warning):

Warning: odbc_exec(): supplied resource is not a valid ODBC-Link resource in C:\\xampp\\htdocs\\db2\\findme.php on line 43

Warning: odbc_fetch_row() expects parameter 1 to be resource, boolean given in C:\\xampp\\htdocs\\db2\\findme.php on line 46

Warning: odbc_num_rows() expects parameter 1 to be resource, string given in C:\\xampp\\htdocs\\db2\\findme.php on line 53

And below is the code I have:

<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>

Have you tried to keep the connection opened? Your code shows an odbc_close($conn); after you verify a successful connection. You should remove that line to keep the connection open otherwise $conn is not valid for the subsequent odbc_exec()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM