|
|
|
|
|
|
| |
There are numerous PHP functions for PHP -> Sybase connectivity, documented in
detail in the PHP Manual. However, you only need a few of these functions in
order to make a simple connection and select some data: |
|
sybase_connect - opens a connection to Sybase; requires a server name, username and password. |
|
sybase_db_select - selects a Sybase database. |
|
sybase_query - issues the SQL statement.
|
|
|
sybase_fetch_array - puts a SQL statement result row in an array. |
|
sybase_free_result - frees the resources in use by the current connection. |
|
sybase_close - closes the current connection. |
|
Just for argument's sake, let's pretend that Sybase is already installed on your system, and you have a valid username and password for an existing database. Let's also assume that you've created a table on that database, called COFFEE_INVENTORY. The COFFEE_INVENTORY table has three columns: COFFEE_NAME, ROAST_TYPE and QUANTITY. |
|
The rows in the COFFEE_INVENTORY table could be populated with data such as: |
|
French Roast,dark,18 |
|
Kenya,medium,6 |
|
Ethiopian Harrar,medium,35 |
|
Sumatra,dark,8 |
|
Columbian,light,12 |
|
Now, let's do some PHP. Before you begin, you must know the name of the server on which the database resides, and have a valid username and password for that server. Then, start your PHP code by creating a connection variable:
|
|
<?
$connection = sybase_connect("servername","username","password");
|
|
|
Test that a connection was established and if it wasn't, print an error message and exit the program: |
|
if (!$connection) {
echo "Couldn't make a connection!";
exit;
}
|
|
|
If you have made it through the connection test, the next steps are to select the database and create the SQL statement. Suppose that the COFFEE_INVENTORY table exists in a Sybase database called "myDB". First, create a database variable, such as: |
|
|
$db = sybase_select_db("myDB", $connection);
|
|
|
Next, test that the database was selected. If it wasn't, print an error message and exit the program:
|
|
if (!$db) {
echo "Couldn't select database!";
exit;
}
|
|
|
Up to this point, you've told PHP to connect to a server and select a database. If you've made it this far, you can issue a SQL statement and hopefully see some results! Using the COFFEE_INVENTORY table, suppose you want to view your inventory, including the name of the coffee and the roast type, with the highest number of bags listed first. Create a variable that holds your SQL statement: |
|
$sql = "SELECT COFFEE_NAME, ROAST_TYPE, QUANTITY
FROM COFFEE_INVENTORY
ORDER BY QUANTITY DESC";
|
|
|
Next, create a variable to hold the result of the query, carried out by the sybase_query function. The sybase_query function takes two arguments: the connection and SQL statement variables you've previously created.
|
|
$sql_result = sybase_query($sql,$connection);
|
|
|
To format the results currently held in $sql_result, first separate the results by row, using the sybase_fetch_array function: |
|
while ($row = sybase_fetch_array($sql_result)) {
// more code here
}
|
|
|
The while loop will create an array called $row for each record in the result set. To get the individual elements of the record (COFFEE_NAME, ROAST_TYPE, QUANTITY), create specific variables: |
|
$coffee_name = $row["COFFEE_NAME"];
$roast_type = $row["ROAST_TYPE"];
$quantity = $row["QUANTITY"];
|
|
|
You'll probably want to print the results in a simple HTML table. Step back and place this statement before the while loop begins, to open the table tag and create the row headings: |
|
echo "<TABLE BORDER=1>";
echo "<TR><TH>Coffee Name</TH><TH>Roast Type</TH><TH>Quantity</TH>";
|
|
|
After defining the variables within the while loop, print them in table format:
|
|
echo "<TR><TD>$coffee_name</TD><TD>$roast_type</TD><TD>$quantity</TD></TR>";
|
|
|
The new while loop now looks like this:
|
|
while ($row = sybase_fetch_array($sql_result)) {
$coffee_name = $row["COFFEE_NAME"];
$roast_type = $row["ROAST_TYPE"];
$quantity = $row["QUANTITY"];
echo "<TR><TD>$coffee_name</TD><TD>$roast_type</TD><TD>$quantity</TD></TR>";
}
|
|
|
After the while loop, close the HTML table:
|
| |
|
Finally, you'll want to free up the resources used to perform the query, and close the database connection. Failing to do so could cause memory leaks and other nasty resource-hogging things to occur.
|
|
sybase_free_result($sql_result);
sybase_close($connection);
?>
|
|
|
The full script to perform a simple connection and data selection from a Sybase database could look something like this: |
|
<?php
// create connection
$connection = sybase_connect("servername","username","password");
// test connection
if (!$connection) {
echo "Couldn't make a connection!";
exit;
}
// select database
$db = sybase_select_db("myDB", $connection);
// test selection
if (!$db) {
echo "Couldn't select database!";
exit;
}
// create SQL statement
$sql = "SELECT COFFEE_NAME, ROAST_TYPE, QUANTITY
FROM COFFEE_INVENTORY
ORDER BY QUANTITY DESC";
// execute SQL query and get result
$sql_result = sybase_query($sql,$connection);
// start results formatting
echo "<TABLE BORDER=1>";
echo "<TR><TH>Coffee Name</TH><TH>Roast Type</TH><TH>Quantity</TH>";
// format results by row
while ($row = sybase_fetch_array($sql_result)) {
$coffee_name = $row["COFFEE_NAME"];
$roast_type = $row["ROAST_TYPE"];
$quantity = $row["QUANTITY"];
echo "<TR><TD>$coffee_name</TD><TD>$roast_type</TD><TD>$quantity</TD></TR>";
}
echo "</TABLE>";
// free resources and close connection
sybase_free_result($sql_result);
sybase_close($connection);
?>
|
|
|
Please see the PHP Manual for additional Sybase database functions, and try using your own tables and SQL statements instead of the examples above.
|
|
| |
| Creating Auto-incrementing ID Fields with PHP and Oracle Categories : PHP, PHP options/info, Databases, Oracle | | | PHP 101 Part 8 of 15 : Databases and Other Animals Categories : PHP, Beginner Guides, Databases | | | Saving Images in MySQL Categories : MySQL, PHP, Graphics, Databases | | | Beginners guide to PHP and MySQL Categories : PHP, Beginner Guides, Databases, MySQL, Installation | | | Custom MySQL-functions Categories : Databases, MySQL, PHP, PHP Functions | | | PHP 101 Part 9 of 15 : SQLite My Fire! Categories : PHP, Beginner Guides, Databases, SQLite | | | Referer Statistics Categories : PHP, MySQL, HTTP, Databases | | | Simple ODBC Connections with PHP Categories : PHP, ODBC, Databases | | | User identification using cookies in PHP and MySQL Categories : PHP, Databases, MySQL, Cookies | | | Multicolumn Output from a Database with PHP Categories : PHP, Databases, HTML and PHP, MySQL | | | Creating an IE-Only Database Driven Menu System With PHP, MySQL and DHTML Categories : PHP, MySQL, Databases, DHTML | | | Simple Connection to Informix with PHP Categories : PHP, Informix, Databases | | | PHP and MySQL News with Comments Categories : PHP, Databases, MySQL | | | Apache, PHP, and PostgreSQL on RedHat Linux Categories : Apache, PHP, Databases, PostgreSQL, Linux | | | Abstracting Oracle Connectivity with PHP and OCI8 Categories : PHP, Databases, OCI8, Oracle | |
| |
|
|