1

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>
3
  • This has been awhile and sorry I didn't get back but I did your suggestion and everything is fine now. Thanks, how can I vote/thank you? Commented May 1, 2018 at 13:52
  • Replaced my comment by answer, so you can react if you would like. Commented May 1, 2018 at 14:04
  • I did thanks much. Commented May 1, 2018 at 14:22

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.