<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <HTML> <HEAD> <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9"> <TITLE>Connecting to MS SQL 6.x+ via Openlink/PHP/ODBC mini-HOWTO: Example</TITLE> <LINK HREF="MSSQL6-Openlink-PHP-ODBC-5.html" REL=next> <LINK HREF="MSSQL6-Openlink-PHP-ODBC-3.html" REL=previous> <LINK HREF="MSSQL6-Openlink-PHP-ODBC.html#toc4" REL=contents> </HEAD> <BODY> <A HREF="MSSQL6-Openlink-PHP-ODBC-5.html">Next</A> <A HREF="MSSQL6-Openlink-PHP-ODBC-3.html">Previous</A> <A HREF="MSSQL6-Openlink-PHP-ODBC.html#toc4">Contents</A> <HR> <H2><A NAME="s4">4. Example</A></H2> <P>Please refers to PHP manual for ODBC functions. Here is my example code odbc.php3: (Note that odbc_num_rows() will not return anything, so you must repeately call odbc_fetch_row() to get that number.) <BLOCKQUOTE><CODE> <PRE> <? /* some environment variables, you can test to comment them out to see * if things still work. */ putenv("LD_LIBRARY_PATH=/usr/local/openlink/odbcsdk/lib"); putenv("UDBCINI=/etc/udbc.ini"); putenv("ODBCINI=/root/.odbc.ini"); putenv("DebugFile=/tmp/udbc.out"); // debug trace output $dsn="DSN=dsn_main"; // note 'DSN=' is required $user="sa"; $password="xxxxxxx"; $sql="SELECT * FROM titles"; /* directly execute mode */ if ($conn_id=odbc_connect("$dsn",$user,$password)){ echo "connected to DSN: $dsn<br><br>"; if($result_id=odbc_do($conn_id, $sql)) { echo "executing '$sql'<br><br>"; $num_fields=odbc_num_fields($result_id); if($num_fields>0){ echo "Number of fields: $num_fields<br>"; for($i=1;$i<=$num_fields;$i++){ $field_name[$i-1]=odbc_field_name($result_id,$i); } $num_rows=0; while(odbc_fetch_row($result_id)){ for($i=1;$i<=$num_fields;$i++){ $result[$num_rows][$field_name[$i-1]]=odbc_result($result_id,$i); } $num_rows++; } echo "Number of rows: $num_rows<br>"; }else{ echo "not a field returned. <br><br>"; } echo "Results:<br>"; for($i=0;$i<sizeof($result);$i++){ while(list($key,$value)=each($result[$i])){ echo "$i:$key=$value<br>"; } } echo "freeing result<br><br>"; odbc_free_result($result_id); }else{ echo "can not execute '$sql'<BR><BR>"; } echo "closing connection $conn_id"; odbc_close($conn_id); }else{ echo "can not connect to DSN: $dsn<br><br>"; } ?> </PRE> </CODE></BLOCKQUOTE> <P> <HR> <A HREF="MSSQL6-Openlink-PHP-ODBC-5.html">Next</A> <A HREF="MSSQL6-Openlink-PHP-ODBC-3.html">Previous</A> <A HREF="MSSQL6-Openlink-PHP-ODBC.html#toc4">Contents</A> </BODY> </HTML>