How to pull the results with ODBC_RESULT in a PHP Variable

Hello all,

I am trying to figure out a problem that I am having with this PHP section of code.

$connection = odbc_connect( $connection_string, $U, $P ); 

$USERID = strtoupper($_POST['USER']);
            $result = odbc_exec($connection, $sql); 
            $res_rows = odbc_result_all($result,'FACILITY');

// Want result to be stored as a variable
$FACILITY = $res_rows;
                        echo $res_rows['FACILITY'];
// echo the variable
                       echo $FACILITY;

Open in new window

When I run this in a debugger I get the following:

Name                  Value            Type
$Facility                0                  long
$res_rows             0                  long
$result                  3                  odbc result  

I have tried ODBC_RESULT as well with the same end result.
Problem. I notice when i remove in the SQL statement the variable '$USERID' that the debugger pulls information for both ODBC_RESULT and ODBC_RESULT_ALL.  Why is the variable causing this issue.

This code is used to validate a user based on the facility and Workgroup so that they are able to view information dependent on those items.  

I have been all over and a few other places but to no avail.  Any help on this would be great.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
odbc_result_all($result,'FACILITY'); is not what you want.  odbc_result_all() returns an HTML table with the results and the second param is supposed to be an attribute for the table.  You probably want...

$res_rows = odbc_fetch_array($result); // followed by
$FACILITY = $res_rows['FACILITY'];

Here is an example of a simple page that is working on my computer to access an SQL 2005 EXPRESS database table.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

<title>PHP ODBC</title>
<h1>PHP ODBC</h1>
// MS SQL server 2005 using PHP 5.2 or lower and php_odbc.dll driver
// DB configuration
$serverName = "DBSERVER\SQLEXPRESS";     // Your database server instance
$dbuser = "Yourusername";      // Your db username
$dbpass = "Yourpassword";      // Your db password
$dbname = "Yourdatabase";      // Your database name
$dbtable = "Yourtablet";  // the table we're using

$conn = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$serverName;Database=$dbname;", $dbuser, $dbpass);
if($conn == 0)
	 die('Unable to connect to the ODBC datasource.');
$idresult = odbc_exec($conn,"SELECT DisplayName, ent_num FROM $dbtable WHERE ent_num = 7");
$idrows = odbc_num_rows($idresult);
$arr = odbc_fetch_array($idresult);
echo $serverName." - ";
echo $arr['DisplayName'];
// Close the ODBC connection.


Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
I think DaveBaldwin has your central issue well in hand here.  The online PHP man pages document the functions so it is always worth reading them to be sure the script passes the correct parameters and tests the function return values to see if the function worked as expected.  It looks to me like you want these functions in this order:

You can use var_dump() to print out the values of data elements, including returns from function calls.  Often this can give you a strong hint as to what the server is doing with your code and data.

You might want to add some sanity checks to protect your data base from the external post data in a query.  Also, if you expect one row, you might want to add LIMIT 1 to the query, to avoid a table scan.
SqlspiderAuthor Commented:
Ok thanks for the responses give me a few hours to catch up I have been under the weather with allergies. Love the season hate the pollen.
Ray PaseurCommented:
Yeah, we've got that pollen here, too.  No rain in weeks.  All the cars are turning green.  Hope you're feeling better soon!
SqlspiderAuthor Commented:
Ok as always you guys are worth your weight in gold.  I changed my test code to the following:


$USERID = strtoupper($_POST['USER']);
[b]$DBTABLE = "[db].[dbo].[CLUSER]";            [/b]

if($CONNECTION == 0)
     die('Unable to connect to the ODBC datasource.');

$idrows = odbc_num_rows($RESULT);
$arr = odbc_fetch_array($RESULT);
// echo the variable            

Open in new window

I tried to run the script as is, with the proper odbc functions but it was not until I added the $dbtable = variable did I finally get the results for the odbc functions to echo.  Also before addding the $dbtable variable I removed my $USERID variable from the sql select and it ran with results.  I did do as you suggested Ray and that helped with the Limit 1.  I just did not think I needed to make a variable for the table.  Odd, but still learning.  One day when I grow up I will be as good as you guys .

Again thanks for the input.  Sorry for the delay on getting back to the forum to see your comments.  

Dave good show on your comment that put me on the right track. Debugging this PHP was giving me grief.
Now I have to correct the rest of the PHP's that I have inherited.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.