How to pull the results with ODBC_RESULT in a PHP Variable

Posted on 2012-04-13
Medium Priority
Last Modified: 2012-06-27
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 PHP.net and a few other places but to no avail.  Any help on this would be great.


Question by:Sqlspider
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 84

Accepted Solution

Dave Baldwin earned 2000 total points
ID: 37845799
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.  http://us3.php.net/manual/en/function.odbc-result-all.php  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

LVL 111

Expert Comment

by:Ray Paseur
ID: 37846163
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.

Author Comment

ID: 37855634
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.
LVL 111

Expert Comment

by:Ray Paseur
ID: 37855785
Yeah, we've got that pollen here, too.  No rain in weeks.  All the cars are turning green.  Hope you're feeling better soon!

Author Comment

ID: 37856467
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.

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question