# Sybase PHP

Sybase offers an industrial-strength database that stands among other big competitors such as Oracle, Informix, and IBM's DB2. Unlike these other databases, Sybase is more available to developers with small budgets because of partnerships with application vendors. InterShop's electronic commerce server comes with a Sybase database. Microsoft's SQL Server 6.5 is a dressed-up version of Sybase. In fact, PHP's Sybase functions are able to connect to SQL Server databases.

lists all mssql_ aliases

When support for Sybase is compiled for PHP, one of two libraries may be used. One is the older DB-Library. The other is its replacement, Client-Library. These two libraries are not compatible with each other, so PHP has special code to adapt either of them into a single set of functions. Consequently, some of these functions are present when using DB-Library and not when using Client-Library. Also, it is possible to compile PHP for Windows using an MSSQL library. This library is really just the DB-Library, but thePHP extension creates only mssql_ functions. It also contains three functions unavailable in the Sybase extension: mssql_field_length, mssql_field_name, and mssql_field_type.

Use sybase_affected_rows to get the number of rows affected by the last delete, insert, or update statement on a given connection. If the optional link argument is left out, the most recently opened connection will be used. Note that this function is not useful for determining the number of rows returned by a select statement. Also, this function is available only when using Client-Library.

<?
$Link = sybase_pconnect("db1", "sa", "sa"); //use the store database sybase_select_db("store",$Link);
//take 10% off all items that cost
//more than ten dollars
$Query = "UPDATE item ";$Query .= "SET Price = Price * 0.90 ";
$Query .= "WHERE Price > 10.00 ";$Result = sybase_query($Query,$Link);
//get number of rows changed
$RowsChanged = sybase_affected_rows($Link);
print("$RowsChanged prices updated.<BR>n"); //close connection sybase_close($Link);
?>

The sybase_close function closes a connection to a database. Its use is not strictly necessary, since PHP will close connections for you when your script ends. You can leave out the link argument, and the last onnection to be opened will be closed.

integer sybase_connect(string server, string user, string password)
The sybase_connect function returns a connection identifier based on the server, user and password arguments. The server must be a valid server name as defined in the interfaces file. All the arguments are optional, and if left out, PHP will use sensible defaults. Connections created with sybase_connect will be closed automatically when your script completes. Compare this function with sybase_pconnect.

boolean sybase_data_seek(integer result, integer row)
The sybase_data_seek function moves the internal row pointer for a result to the specified row. Rows are numbered starting with zero. Use this function with sybase_fetch_array, sybase_fetch_object or sybase_fetch_ row to move
arbitrarily among the result set.

<?
//move to sixth row
sybase_data_seek($Result, 5); ?> array sybase_fetch_array(integer result) The sybase_fetch_array function returns an array that contains the values of all the fields for the next row. Each call to sybase_fetch_array gets the next row in the result set, or returns FALSE if no rows remain. Each field is returned in two elements. One is indexed by the field number, starting with zero. The other is indexed by the name of the field. Compare this function to sybase_fetch_object and sybase_fetch_row. <? //connect$Link = sybase_pconnect();
//use the store database
sybase_select_db("store", $Link); //get all items$Result = sybase_query("SELECT * FROM item ");
print("<TABLE BORDER="1">n");
//get rows
while($Row = sybase_fetch_array($Result))
{
print("<TR>n");
print("<TD>" . $Row["Name"] . "</TD>n"); print("<TD>" .$Row["Price"] . "</TD>n");
print("</TR>n");
}
print("</TABLE>n");
?>

object sybase_fetch_field(integer result, integer field)
The sybase_fetch_field function returns an object that describes a field in the result set. The field argument is optional. If left out, the next field is returned. See sybase_result for an example of use.

object sybase_fetch_object(integer result)
The sybase_fetch_object function returns an object with a property for each of the fields in the next row. Each call to sybase_fetch_object gets the next row in the result set, or returns FALSE if no rows remain. Compare this function to sybase_fetch_array and sybase_fetch_row.

array sybase_fetch_row(integer result)
The sybase_fetch_row function returns an array of all the field values for the next row. The fields are indexed by integers starting with zero. Each call to sybase_fetch_row gets the next row in the result set, or returns FALSE if no rows remain. Compare this function to sybase_fetch_array and sybase_fetch_object.

<?
//connect
$Link = sybase_pconnect(); //use the store database sybase_select_db("store",$Link);
//get all items
$Result = sybase_query("SELECT * FROM item "); print("<TABLE BORDER="1">n"); //get rows while($Row = sybase_fetch_array($Result)) { print("<TR>n"); print("<TD>" .$Row[0] . "</TD>n");
print("<TD>" . $Row[1] . "</TD>n"); print("</TR>n"); } print("</TABLE>n"); ?> boolean sybase_field_seek(integer result, integer field) The sybase_field_seek function moves the internal field pointer to the specified field. Fields are numbered starting with zero. If you leave out the field argument, the internal pointer will be moved to the next field. his is the same internal pointer used by sybase_fetch_field. <? // go back to first field sybase_field_seek($result, 0);
?>

boolean sybase_free_result(integer result)
The sybase_free_result function frees memory associated with a result set. It is not strictly necessary to call this function. All memory is freed when a script finishes executing.

string sybase_get_last_message()
The sybase_get_last_message function returns the last message from the Sybase database. This function is not available if you're using Client-Library instead of DBLibrary.

<?
print(sybase_get_last_message());
?>

sybase_min_client_serverity(integer severity)
This function is available only when using Client-Library. It sets the minimum severity for messages sent from the client interface to be turned into PHP error messages.

sybase_min_error_severity(integer severity)
Use sybase_min_error_severity to set the minimum severity level for errors to be turned into PHP error messages. This function is available only when using DB-Library.

sybase_min_message_severity(integer severity)
Use sybase_min_message_severity to set the minimum severity level for messages to be turned into PHP error messages. This function is available only when using DBLibrary.

sybase_min_server_severity(integer severity)
This function is available only when using Client-Library. It sets the minimum level for messages from the server interface to cause PHP error messages to be generated.

integer sybase_num_fields(integer result)
The sybase_num_fields function returns the number fields in the given result set. See sybase_result for an example of use.

integer sybase_num_rows(integer result)
The sybase_num_rows function returns the number of rows in a result set. See sybase_result for an example of use.

The sybase_pconnect function is identical to sybase_connect, except that connections created with this function persist after the script ends. The connection lasts as long as the server process does, so if the process executes another PHP script, the connection will be reused. Connections created with sybase_pconnect should not be closed with sybase_close.

nteger sybase_query(string query, integer connection)
The sybase_query function executes a query on the given connection and returns a result identifier. This is used by many of the other functions in this section. If the connection argument is left out, the last opened connection is used.

string sybase_result(integer result, integer row, string field)
The sybase_result function returns the value of a particular field, identified by row and field. The field argument may be an integer or the name of a field. Fields and rows are numbered starting with zero. If performance is an issue, considering using sybase_fetch_row, which is much faster.

<?
//connect using defaults
$Link = sybase_connect(); //use the store database sybase_select_db("store",$Link);
//get all items
$Result = sybase_query("SELECT * FROM item "); print("<TABLE BORDER="1">n"); //header row$Fields = sybase_num_fields($Result); for($i = 0; $i <$Fields; $i++) {$Field = sybase_fetch_field($Result); print("<TR>n"); print("<TH>"); print($Field->column_source);
print(".");
print($Field->name); print("("); print($Field->max_length);
print(")");
print("</TH>n");
print("</TR>n"); }
// data rows
$Rows = sybase_num_rows($Result);
for($n = 0;$n < $Rows;$n++)
{
print("<TR>n");
for($i = 0;$i < $Fields;$i++)
{
print("<TD>");
print(sybase_result($Result,$n, $i)); print("</TD>n"); } print("</TR>n"); } print("</TABLE>n"); sybase_free_result($Result);
?>

boolean sybase_select_db(string database, integer connection)
The sybase_select_db function selects the database to use on the database server. If the connection argument is omitted, the last connection created will be used. See sybase_fetch_array for an example.

« Previous Topics
Oracle
Odbc
Informix
Next Topics »
Mysql
Msql
Inter Base