note 7874 deleted from What can PHP do? by zeev
| From: | zeev@php.net | Date: | Fri, 13 Jul 2001 16:46:42 +0000 |
| Subject: | note 7874 deleted from What can PHP do? by zeev | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-15333@lists.php.net to get a copy of this message | ||
I used it with MS SQL 7 in Win2K using ODBC just fine. Here's my code. This was the first
thing I tried to do with it of course, being a web guy who likes MS products. I'm new to this
so don't jump me if my comments are somewhat off.
You may need to dump this into Notepad without Word Wrap to see it properly.
Note that "LocalServer" is the System DSN name created in the ODBC Control Panel, and
sa/<blank> is the username/password (I really should secure that, shouldn't I. :))
<?php
$sqlstring1="SELECT * FROM sysobjects WHERE xtype='u'"; # Grabs list of tables
echo "Created $sqlstring1 (our query): '$sqlstring1'<BR>";
$con1 = odbc_connect("LocalServer","sa",""); # Connection
object
echo "Created $con1 (connection): '$con1'<Br>";
$res1 = odbc_exec( $con1, $sqlstring1 ); # Recordset object
echo "Created $res1 (recordset): '$res1'<Br>";
unset( $valid_row ); # Kills a variable, better than setting to
0
$valid_row = odbc_fetch_into( $res1, &$resultarray); # The &$ is for assigning by
reference. This is required
# for the 2nd arg of odbc_fetch_into,
which is "where to
# put the row it's fetching".
A referenced var is like a pointer
# to a variable, so if the original is
updated, so is
# the one being referenced.
echo "Created $valid_row (# of cols in record): '$valid_row'<Br>";
echo "..also Created $resultarray (array of data): '$resultarray'<Br>";
while ( $valid_row ) { # $valid_row was the return from
odbc_fetch_into, which is
# the number of columns in the record.
Very useful. Presumably 0 or false
# if no columns.
echo "$resultarray[0] (column 0): " . $resultarray[0] . "<Br>";
echo "$resultarray[1] (column 1): " . $resultarray[1] . "<Br>";
echo "$resultarray[2] (column 3): " . $resultarray[2] . "<Br>";
$valid_row = odbc_fetch_into( $res1, &$resultarray ); # Repeat the fetch, update
$valid_row
}
odbc_close($con1); # Clean up
?>