Database Reference
In-Depth Information
By ThE WAy Be sure to use comments to document your Web pages. PHP code seg-
ments with two forward slashes (//) in front of them are comments. This
symbol is used to define single-line comments. In PHP, comments can also be inserted
in blocks between the symbols /* and */, whereas in HTML comments must be inserted
between the symbols <!-- and -->.
The connection is used to open the VRG ODBC data source. Here the user ID of
VRG-User and the password of VRG-User+password that we created in Chapter 10 for SQL
Server 2012 are being used to authenticate to the DBMS. If you are using Oracle Database
or MySQL, use the ODBC data source name, user name, and user password as you created
it for your database. Note that the user ID and password are only sent to the database
server to get data, and are  never seen in either (1) the resulting Web page as displayed in
the user's Web browser, or (2) the underlying HTML code. There is no security problem
here!
The test of the connection is contained in the code segment:
// Test connection
if (!$Conn)
{
exit ("ODBC Connection Failed: " . $Conn);
}
In English, this statement says, “IF the connection Conn does not exist, THEN print the
error  message 'ODBC Connection Failed' followed by the contents of the variable $Conn.”
Note that the code (!$Conn) means NOT $Conn—in PHP the exclamation point symbol (!)
means NOT.
At this point, a connection has been established to the DBMS via the ODBC data source,
and the database is open. The $Conn variable can be used whenever a connection to the data-
base is needed.
Creating a RecordSet
Given the connection with an open database, the following code segment from Figure 11-31
will store an SQL statement in the variable $SQL and then use the PHP odbc_exec command
to run that SQL statement against the database to retrieve the query results and store them in
the variable $RecordSet:
// Create SQL statement
$SQL = "SELECT LastName, FirstName, Nationality FROM ARTIST";
// Execute SQL statement
$RecordSet = odbc_exec($Conn,$SQL);
// Test existence of recordset
if (!$RecordSet)
{
exit ("SQL Statement Error: " . $SQL);
}
?>
Note that you need to test the results to be sure the PHP command executed correctly.
 
Search WWH ::




Custom Search