Mini SQL 2.0

User Guide

Lite's mSQL Module

The Mini SQL module is a library of routines for communicating with a Mini SQL database. The functions provided by this module mimic the functions provided by the mSQL C API. Please see the mSQL documentation for more information.

Outlined below is a description of each of the functions available within the Mini SQL module.
 
 

msqlConnect ( )

int msqlConnect ( host )

char *host

msqlConnect() connects to the mSQL server on the specified host. If no host is specified it connects to the local mSQL server.

Example :

$sock = msqlConnect("research.Hughes.com.au");

if ($sock < 0)

{

echo("ERROR : $ERRMSG\n");

}
 
 
 
 
 
 

msqlClose ( ) msqlConnect ( sock )

int sock

msqlClose() closes a connection made using msqlConnect().

Example :

msqlClose($sock);
 
 
 
 
 
msqlSelectDB ( ) int msqlSelectDB ( sock , db )

int sock

char *db

msqlSelectDB() tells the mSQL server which database you wish to use.

Example :

if (msqlSelectDB($sock,"my_db") < 0)

{

echo("ERROR : $ERRMSG\n");

}
 
 
 
 
 
 
 
 

msqlQuery ( ) int msqlQuery ( sock , query )

int sock

char *query

msqlQuery() submits a query to the mSQL server connected to the specified socket.

Example :

if (msqlQuery($sock, "select * from foo") < 0)

{

echo("ERROR : $ERRMSG\n");

}
 
 
 
 
 
 

msqlStoreResult ( ) msqlStoreResult ( ) msqlStoreResult() stores any data that was a result of the previous query.

Example :

$res = msqlStoreResult();
 
 
 
msqlFreeResult ( ) msqlFreeResult ( res )

int res

msqlFreeResult() frees any memory allocated to the specified result.

Example :

msqlFreeResult($res);
 
 
 
msqlFetchRow ( ) msqlFetchRow ( res )

int res;

msqlFetchRow() returns a single row of the data stored in the specified result.

Example :

$row = msqlFetchRow($res);

if ( # $row == 0)

{

echo("ERROR : $ERRMSG\n");

}

else

{

echo("Field 0 is $row[0]\n");

}
 
 
 
 
 
 

msqlDataSeek ( ) msqlDataSeek ( res , location )

int res

int location

msqlDataSeek() allows you to move the data pointer within the result table. Specifying a location of 0 will rewind the result. The next call to msqlFetchRow() will return the first row of the result table again.

Example :

msqlDataSeek( $res, 0);
 
 
 
msqlListDBs ( ) msqlListDBs ( sock )

int sock

msqlListDBs() returns an array of the names of the databases available on the specified server.

Example :

$dbs = msqlListDBs($sock);

$index = 0;

while ($index < # $dbs)

{

printf("Database = %s\n", $dbs[$index]);

$index = $index + 1;

}
 
 
 
 
 
 

msqlListTables ( ) msqlListTables ( sock , db )

int sock

char *db

msqlListTables() returns an array of the names of all the tables available in the current database of the specified server.

Example :

$tabls = msqlListTables($sock);

$index = 0;

while ($index < # $tabls)

{

printf("Table = %s\n", $tabls[$index]);

$index = $index + 1;

}
 
 
 
 
 
 

msqlInitFieldList ( ) msqlInitFieldList ( sock , db , table )

int sock

char *db

char *table

msqlInitFieldList() generates an internal result handle containing details of all the fields in the specified table of the specified database. The result handle is used in conjunction with the functions below to access the field structure information. Note that the result handle is held as a static variable inside the mSQL module and further calls to msqlInitFieldList() will free the result.

msqlListField ( )

msqlListField ( ) msqlListField() returns an array of information about a single field of the current field list result that was generated using msqlInitFieldList(). The elements of the array contain the following information.
 
Element
Description
0
Field Name
1
Table Type
2
Type
3
Length
4
Flags
 

Example :

$res = msqlInitFieldList($sock,"my_db","my_table");

$field = msqlListField($res);

while( # $res 0)

{

echo("Name $field[0]\n");

$field = msqlListField($res);

}
 
 
 
 
 
 

msqlFieldSeek ( ) msqlFieldSeek ( res , location )

int res

int location

msqlFieldSeek() acts upon the result of a call to msqlInitFieldList() in the same way msqlDataSeek() acts upon the result of a call to msqlStoreResult(). It allows you to move the internal result pointer to the specified location.
 
 
 
 
 
 

msqlNumRows ( )

int msqlNumRows ( res )

int res

msqlNumRows() returns the number of rows contained in the result handle res.

Example :

msqlQuery($sock, "select * from foo");

$res = msqlStoreResult();

printf("There are %d rows in foo\n",

msqlNumRows($res);
 
 
 
 
 
 

msqlEncode ( ) msqlEncode ( string )

char *string

msqlEncode() is passed a string value that may contain characters that can cause errors in mSQL query strings (such as the ' character in text values). It returns a modified version of the string with all such characters escaped.

Example :

$name = "O'Reilly";

$newName = msqlEscape($name);