Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help displaying query

Status
Not open for further replies.

benderulz

IS-IT--Management
Nov 2, 2010
43
US
Hello,

I'm new to using php and am in need of some guidance. Below is the code I'm trying to display. However, all it returns is a blank page. If I replace the SQL with a simple echo text for a good connection test it displays, but I cannot get it to display the query results. Is my SQL incorrect or is my display method incorrect?

<html>
<body>
<?php
$conn=odbc_connect("","","");
if($conn)
{
$sql="SELECT * FROM warehouse";
$results = odbc_execute($conn,$sql);
echo $results
}
else
echo "Database Connect Failed.";
}
odbc_close($conn);
?>
</body>
</html>
 
I am new to PHP too.


You missed the ; after echo $results

Should be echo $results;

 
I added the ; but still getting a blank screen.
 
Read the manual

1. Connect to the dB host
2. Select a database (if not included in the Dsn and if not using odbc)
3. Query the database. This returns a result set as a resource.
4. Iterate over the result set to obtain the rows.

So
Code:
$r=odbc_exec( query );
While ($row = odbc_fetch_array($r)):
print_r ($row);
Endwhile;
 
Sounds like you are getting an error, and your PHP configuration is not set to show them.

Either modify your PHP.ini file to set display_errors to on, or you can use the runtime methods directly in your script, though these only work if the errors are non fatal.

Code:
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
ini_set('display_errors',1);

If you modify the php.ini file make sure to restart the web server to refresh the loaded configuration settings,


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
the issue is simple. the code is just wrong as i pointed out above.

odbc_execute executes a prepared statement. there is none
odbc_execute returns boolean value and NOT a result set or resource. echoing this value will not be meaningful.


if you want to execute a query in odbc you either use odbc_prepare followed by odbc_execute or you just use odbc_exec. IMO the former is better practice.

to use a result set you must iterate over a resource using an odbc_fetch_*() method.


with apologies the code I posted had a serious error in that the first argument of odbc_exec should have been the connection object returned from.odbc_connect.

Code:
$r=odbc_exec($conn,$query);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top