# mSQL PHP

The functions in this section communicate with mSQL, a database that implements a subset of SQL.

There are a handful of mSQL functions that exist simply for backward compatibility. I have chosen to leave them out. Only some of them are documented in the official PHP 3 manual. Their use is described in the PHP 2 documentation. For reference they are msql,msql_createdb, msql_dbname, msql_dropdb, msql_freeresult, msql_listdbs, msql_list-fields, msql_listtables, msql_numfields, msql_numrows, msql_selectdb, msql_tablename.

Two mSQL extensions exist, one for mSQL version 1 and one for version 2. You must load the appropriate extension to enable the functions in this section. Zeev Suraski wrote both mSQL extensions.

The msql_affected_rows function returns the number of rows involved in the previous query made on the given link. The link argument must be an integer returned by msql_connect or msql_pconnect. For an example of use, see msql_db_query, below.

The msql_close function closes the link to a database. If the link argument is left out, the last-opened link is closed. Only links opened by msql_connect may be closed. Using this function is not strictly necessary, since all nonpersistent links are automatically closed when a script ends. For an example of use, see msql_db_query, below.

The msql_connect function attempts to connect to the mSQL server at the specified host. If the host argument is left out, the local host will be assumed. A link identifier is returned. In the case where an open link exists, it will be returned rather than establishing a second link. The connection is automatically closed at the end of the script. You may add a colon and a port number to the host argument.

The msql_create_db function attempts to create a database. The link argument is optional. If left out, the last-opened link will be used.

<?
$Link = msql_connect("msql.clearink.com"); msql_create_db("store",$Link);
msql_close($Link); ?> boolean msql_data_seek(integer result, integer row) Use msql_data_seek to move the internal row pointer to the specified row in a result set. The result argument is as returned by msql_query. <?$Link = msql_connect("msql.clearink.com");
msql_select_db("store", $Link);$Result = msql_query("SELECT Name FROM customer", $Link); // jump to tenth customer msql_data_seek($Result, 10);
$Row = msql_fetch_row($Result);
print($Row[0]); msql_close($Link);
?>

integer msql_db_query(string database, string query, integer link)
The msql_db_query function is identical to msql_query except that it specifies a database rather than using the database selected with msql_ select_db. The query is executed in the specified database and a result identifier is returned.

<?
$Link = msql_connect("msql.clearink.com");$Query = "DELETE FROM customer";
$Result = msql_db_query("store",$Query, $Link);$RowsAffected = msql_affected_rows($Link); print($RowsAffected . " rows deleted.");
msql_close($Link); ?> boolean msql_drop_db(string database, integer link) The msql_drop_db function removes an entire database from the server. The link argument is optional and, if omitted, the last connection opened will be used. <?$Link = msql_connect("msql.clearink.com");
if(msql_drop_db("store", $Link)) { print("Database deleted!"); } else { print("Database not deleted: "); } msql_close($Link);
?>

string msql_error()
Use msql_error to retrieve the last error message returned by an mSQL function. See msql_drop_db, above, for an example of use.

array msql_fetch_array(integer result, integer type)
The msql_fetch_array function returns an array of the data for the current row. The result argument is as returned by msql_query. By default, result columns are returned in two elements each: one referenced by number and one referenced by field name. The optional type argument controls which elements are created. MSQL_NUM signals that only numbered elements be created. MSQL_ASSOC signals that only named elements be created. If you want both, you can explicitly request it with MSQL_BOTH. Compare this function to msql_fetch_row and msql_fetch_object.

<?
$Link = msql_connect("msql.clearink.com"); msql_select_db("store",$Link);
$Query = "SELECT * FROM customer";$Result = msql_query($Query,$Link);
//fetch each row
while($Row = msql_fetch_array($Result, MSQL_ASSOC))
{
print($Row["FirstName"] . "<BR>n"); } msql_close($Link);
?>

object msql_fetch_field(integer result, integer field)
The msql_fetch_field function returns an object with properties that describe the specified field. The field argument may be left out, and the next unfetched field will be returned. The properties of the object are listed.

<?
$Link = msql_connect("msql.clearink.com"); msql_select_db("store",$Link);
$Query = "SELECT * FROM item i, SKU s ";$Query .= "WHERE i.SKU = s.ID ";
$Result = msql_query($Query, $Link); // get description of each field while($Field = msql_fetch_field($Result)) { print("Name: " .$Field->name . "<BR>n");
print("Table: " . $Field->table . "<BR>n"); print("Not Null: " .$Field->not_null . "<BR>n");
print("Primary Key: " . $Field->primary_key"<BR>n"); print("Unique: " .$Field->unique . "<BR>n");
print("Type: " . $Field->type . "<BR>n<BR>n"); } msql_close($Link);
?>

object msql_fetch_object(integer result)
The msql_fetch_object function returns an object with a property for each column of the resulting row. Each call to msql_fetch_object gets the next row from the results, or returns FALSE when there are none left.

<?
$Link = msql_connect("msql.clearink.com"); msql_select_db("store",$Link);
$Query = "SELECT * FROM item";$Result = msql_query($Query,$Link);
while($Row = msql_fetch_object($Result))
{
print("$Row->ID:$Row->Name<BR>n");
}
msql_close($Link); ?> array msql_fetch_row(integer result) The msql_fetch_row function returns an array with one element for each resulting column. FALSE is returned when no results are left. Columns arereferenced by integers starting at zero. Compare this function to msql_fetch_array and msql_fetch_object. <?$Link = msql_connect("msql.clearink.com");
msql_select_db("store", $Link);$Query = "SELECT * FROM item";
$Result = msql_query($Query, $Link); while($Row = msql_fetch_row($Result)) { print($Row[0] . ": " . $Row[1] . "<BR>n"); } msql_close($Link);
?>

boolean msql_field_seek(integer result, integer field)
Use msql_field_seek to move the internal field pointer to the specified field.

<?
$Link = msql_connect("msql.clearink.com"); msql_select_db("store",$Link);
$Query = "SELECT * FROM item i, SKU s ";$Query .= "WHERE i.SKU = s.ID ";
$Result = msql_query($Query, $Link); // get description of each field // starting with the third msql_field_seek($Result, 2);
while($Field = msql_fetch_field($Result))
{
print("Name: " . $Field->name . "<BR>n"); print("Table: " .$Field->table . "<BR>n");
print("Not Null: " . $Field->not_null ."<BR>n"); print("Primary Key: " .$Field->primary_key"<BR>n");
print("Unique: " . $Field->unique . "<BR>n"); print("Type: " .$Field->type . "<BR>n<BR>n");
}
msql_close($Link); ?> string msql_fieldflags(integer result, integer field) The msql_fieldflags function returns all the flags turned on for the specified field. These may be primary key, unique, and not null. <?$Link = msql_connect("msql.clearink.com");
msql_select_db("store", $Link);$Query = "SELECT * FROM item";
$Result = msql_query($Query, $Link); print("Field 0 flags are " .msql_fieldflags($Result, 0));
msql_close($Link); ?> integer msql_fieldlen(integer result, integer field) The msql_fieldlen function returns the length of the specified field. <?$Link = msql_connect("msql.clearink.com");
msql_select_db("store", $Link);$Query = "SELECT * FROM item";
$Result = msql_query($Query, $Link); print("Field 0 length is " . msql_fieldlen($Result, 0));
msql_close($Link); ?> string msql_fieldname(integer result, integer field) The msql_fieldname function returns the name of the specified field. <?$Link = msql_connect("msql.clearink.com");
msql_select_db("store", $Link);$Query = "SELECT * FROM item";
$Result = msql_query($Query, $Link); print("Field 0 is " . msql_fieldname($Result, 0));
msql_close($Link); ?> string msql_fieldtable(integer result, integer field) The msql_fieldtable function returns the name of the table for the specified field. <?$Link = msql_connect("msql.clearink.com");
msql_select_db("store", $Link);$Query = "SELECT * FROM item";
$Result = msql_query($Query, $Link); print("Field 0 is from " . msql_fieldtable($Result, 0));
msql_close($Link); ?>; string msql_fieldtype(integer result, integer field) The msql_fieldtype function returns the type of the specified field. <?$Link = msql_connect("msql.clearink.com");
msql_select_db("store", $Link);$Query = "SELECT * FROM item";
$Result = msql_query($Query, $Link); print("Field 0 is " . msql_fieldtype($Result, 0));
?>

boolean msql_free_result(integer result)
When a script ends, all results are freed. If memory is a concern while your script is running, use msql_free_result.

« Previous Topics
Mysql
Sybase
Oracle
Next Topics »
Inter Base
Postgres
Tokenizing