WebSiphon 2 Guide: MySQL Functions
contents prev next

This collection of MySQL functions (implemented through the Kingfisher add-on library) have been modeled to closely mirror the functionality offered in the MySQL developer API, with only slight variations as is appropriate in the WebSiphon environment.

If you have any questions about using MySQL and the MySQL API, or require detail beyond what is offered in this reference, visit http://www.mysql.com/.

The MySQL suite of functions utilizes a few special data types used to manage connections and query results:

mysql-linka reference to a MySQL server connection
result-linka reference to generated query results

To obtain a mysql-link, use mysqlConnect() to establish a connection to a MySQL server.

To retrieve values associated with a result-link, use mysqlFetchRow() to iterate through each row of the result set or mysqlFetchAllRows() to retrieve all rows at once.

Function definitions in this collection:

mysqlAffectedRows
mysqlClose
mysqlConnect
mysqlCreateDB
mysqlDataSeek
mysqlDropDB
mysqlEscapeString
mysqlFetchAllRows
mysqlFetchFields
mysqlFetchLengths
mysqlFetchRow
mysqlFreeResult
mysqlGetHostInfo
mysqlGetClientInfo
mysqlGetProtoInfo
mysqlGetServerInfo
mysqlInfo
mysqlInsertID
mysqlListDBs
mysqlListProcesses
mysqlListTables
mysqlNumRows
mysqlPing
mysqlQuery
mysqlReload
mysqlSelectDB
mysqlShutdown
mysqlStat
mysqlStoreResult
mysqlThreadID
mysqlUseResult
mysqlAffectedRows ( mysql-link )
Returns: integer Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference

When given a valid mysql-link, returns the number of rows that were affected by the last query to the specified MySQL connection.

Example:

sqls = "update Thinker set Name='Euclid' where (thoughts='geometry')";
mysqlQuery(mysql_link, sqls);
print mysqlAffectedRows(mysql_link);
1

See Also:

mysqlClose ( mysql-link )
Returns: null Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference

When given a valid mysql-link, closes the specified server connection.

See Also:

mysqlConnect ( host, mysql-user, mysql-pass [, server-port, database-name ])
Returns: mysql-link Location: Kingfisher
Parameter Description
host host name or IP address of MySQL server
mysql-user MySQL account username
mysql-pass MySQL account password
[server-port] (optional) MySQL server TCP/IP port
[database-name] (optional) Select as default database on connect

When given valid account parameters, opens a connection to the specified MySQL database server. Specify server-port if it is different than the default MySQL TCP/IP port number. If a database-name is provided, it will be selected when the connection is established.

Example:

// MySQL host and account info
mysql_host = "10.0.1.108";
mysql_user = "Kingfisher";
mysql_pass = "alcedinidae";
  
// open connection to MySQL server
mysql_link = mysqlConnect(mysql_host, mysql_user, mysql_pass);

See Also:

mysqlCreateDB ( mysql-link, database-name )
Returns: null Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference
database-name Name of database to be created

When given a valid mysql-link, creates a new database on the MySQL server with the name database-name.

The user whose account information was used to open the database connection must have sufficient permissions, or an access denied runtime error will occur.

Example:

// custom database creation function with basic 
// error handling using try/catch
function myCreateDB(db_name)
  try
    // tell mysql to create new database
    mysqlCreateDB(mysql_link, db_name);
  catch
    case siphon_error_number of
      1044: return (-1001); // access denied for user
      1007: return (-1002); // db with same name already exists

      // some other error occured
      otherwise:
            appendLog(siphon_error_message);
            return siphon_error_number;
      end case;
  end try;
  
  return null;
end function; // myCreateDB()

mysqlDataSeek ( results-link, row-number )
Returns: null Location: Kingfisher
Parameter Description
results-link query results reference
row-number Row to seek in last query

When given a valid query results-link, moves the cursor of the current query results forward to the given row-number.

mysqlDropDB ( mysql-link, database-name )
Returns: null Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference
database-name Name of database to drop (delete)

When given a valid mysql-link, permanently deletes database-name from the remote MySQL server.

The user whose account information was used to open the database connection must have sufficient permissions, or an access denied runtime error will occur.

Example:

// custom database drop function with basic 
// error handling using try/catch
function myKillDB(db_name)
  try
    // tell mysql to drop database
    mysqlDropDB(mysql_link, db_name);
  catch
    case siphon_error_number of
      1044: return (-1001); // access denied for user
      1008: return (-1002); // db with name doesn't exist

      // some other error occurred
      otherwise:
            appendLog(siphon_error_message);
            return siphon_error_number;         
    end case;
  end try;

  return null;
end function; // myKillDB()

mysqlEscapeString ( mysql-link, sql-text )
Returns: string Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference
sql-text Text to be properly formatted for SQL

When given sql-text, this function returns the same text properly formatted for transmission in an SQL query. This function handles quotes and other special characters.

mysqlFetchAllRows ( results-link )
Returns: list Location: Kingfisher
Parameter Description
results-link query results reference

When given a valid query results-link, returns a list of all rows contained within the results.

mysqlFetchFields ( results-link )
Returns: list Location: Kingfisher
Parameter Description
results-link query results reference

When given a valid query results-link, returns a list of field definitions contained within the results.

Each item in the returned list is a sub-list with the following structure:

  1. name - The name of the field
  2. default - The default value of this field
  3. type - The MySQL field type
  4. length - The width of the field
  5. max length - The maximum width of the field
  6. flags - The MySQL flags set for this field
  7. decimals - The number of decimal places, for numeric fields
Each of these values correspond directly to their definition in the MySQL API.

Example:

// list current processes, and output field definitions for
// columnar data returned in response to that query
results_link = mysqlListProcesses(mysql_link);
printListMatrix(mysqlFetchFields(results_link));
Example Output:
[ "Id", "", "", "longlong", 7, 3, 32769, 0 ]
[ "User", "", "", "var string", 16, 8, 1, 31 ]
[ "Host", "", "", "var string", 64, 10, 1, 31 ]
[ "db", "", "", "var string", 64, 8, 0, 31 ]
[ "Command", "", "", "var string", 16, 11, 1, 31 ]
[ "Time", "", "", "var string", 7, 1, 1, 31 ]
[ "State", "", "", "var string", 30, 0, 0, 31 ]
[ "Info", "", "", "var string", 100, 0, 0, 31 ]

mysqlFetchLengths ( results-link )
Returns: list Location: Kingfisher
Parameter Description
results-link query results reference

When given a valid query results-link, returns a list of field lengths.

mysqlFetchRow ( results-link )
Returns: list Location: Kingfisher
Parameter Description
results-link query results reference

When given a valid query results-link, returns the current row of the results and moves the cursor forward to the next row.

mysqlFreeResult ( results-link )
Returns: null Location: Kingfisher
Parameter Description
results-link query results reference

When given a valid query results-link, frees the specified results from memory. Results are automatically disposed of at the end of a template execution, so this is only necessary when dealing with large result sets.

mysqlGetHostInfo ( mysql-link )
Returns: string Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference

When given a valid mysql-link, returns information about the connection being used to access the host MySQL server.

Example:

print mysqlGetHostInfo(mysql_link);
10.0.1.108 via TCP/IP

mysqlGetClientInfo ( )
Returns: string Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference

Returns information about the MySQL client library version used by Kingfisher to implement the MySQL-API bridge.

Example:

print mysqlGetClientInfo();
3.23.41

mysqlGetProtoInfo ( mysql-link )
Returns: string Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference

When given a valid mysql-link, returns the network protocol version being used by the connection.

Example:

print mysqlGetProtoInfo(mysql_link);
10

mysqlGetServerInfo ( mysql-link )
Returns: string Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference

When given a valid mysql-link, returns the MySQL server version hosting the connection.

Example:

print mysqlGetServerInfo(mysql_link);
3.23.52-entropy.ch

mysqlInfo ( mysql-link )
Returns: string Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference

When given a valid mysql-link, returns information about the most recent query, provided that its execution caused meta-information to be returned by the MySQL server.

Returns null if no information is available for the previous query.

Examples:

  1. sqls = "alter table Thinker add (age tinyint unsigned)";
    mysqlQuery(mysql_link, sqls);
    print mysqlInfo(mysql_link);
    Records: 54 Duplicates: 0 Warnings: 0
  2. sqls = "update Thinker set name='Bucky' where (thoughts='synergy')";
    mysqlQuery(mysql_link, sqls);
    print mysqlInfo(mysql_link);
    Rows matched: 4 Changed: 4 Warnings: 0

mysqlInsertID ( mysql-link )
Returns: integer Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference

When given a valid mysql-link, returns the auto_increment value generated by the most recent query. Use this function to retrieve the primary key value after an INSERT statement to a table with an auto_increment column.

Example:

// in this example its assumed that the table "Thinker" 
// contains an integer primary key set to auto_increment
sqls = "insert into Thinker set name='Bohrs', thoughts='physics'";
mysqlQuery(mysql_link, sqls);
print mysqlInsertID(mysql_link);
55

mysqlListDBs ( mysql-link [, search-key ])
Returns: result-link Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference
[search-key] Filter by wildcard string [optional]

When given a valid mysql-link, returns a results reference link containing information about available databases hosted on the MySQL server. The results may be filtered using the optional search-key wildcard string.

This function returns a result-link. To retrieve result values, use mysqlFetchRow() to iterate through each row of results or mysqlFetchAllRows() to retrieve all rows at once.

Example:

// this example returns 4 rows of data, each containing
// only one column: the database name (hosted by MySQL)
results_link = mysqlListDBs(mysql_link);
printList(mysqlFetchAllRows(results_link));
[ ["Purity"], ["SiphonQA"], ["Verona"], ["mysql"] ]

mysqlListProcesses ( mysql-link )
Returns: results-link Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference

When given a valid mysql-link, returns a results reference link containing information about active processes on the remote MySQL server.

This function returns a result-link. To retrieve result values, use mysqlFetchRow() to iterate through each row of results or mysqlFetchAllRows() to retrieve all rows at once.

mysqlListTables ( mysql-link [, search-keys ])
Returns: results-link Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference
search-keys (optional) Filter by wildcard string

When given a valid mysql-link, returns a results reference link containing information about tables for the current database on the remote MySQL server. The results may be filtered using the optional search-keys wildcard string.

To view results, use mysqlFetchRow() or mysqlFetchAllRows() with the returned results-link.

Use mysqlSelectDB() to select the current database on a given connection.

mysqlNumRows ( results-link )
Returns: integer Location: Kingfisher
Parameter Description
results-link query results reference

When given a valid query results-link, returns the total number of rows in the specified result set.

Special Note: This value is only valid if you used mysqlStoreResult() for this result set. Alternately, if you used mysqlUseResult() this function will not return the correct value until all rows in the result set have been retrieved.

mysqlPing ( mysql-link )
Returns: boolean Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference

When given a valid mysql-link, checks to see if the MySQL connection is active, and if not, will try to reconnect. If the connection timed out or the database went offline for some other reason, this function will enable you to detect this condition and manually restablish the connection.

Returns true if the connection is still alive.

mysqlQuery ( mysql-link, sql-text )
Returns: null Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference
sql-text Properly formatted SQL statement

When given a valid mysql-link, executes the specified sql-text query on the remote MySQL server. The query string passed in sql-text must be properly formatted using mysqlEscapeQuery() or other means.

Use mysqlStoreResult() or mysqlUseResult() to access results before submitting a different query on the same connection or the current results will be lost.

mysqlReload ( mysql-link )
Returns: boolean Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference

When given a valid mysql-link, reloads the remote MySQL server's user permissions database. This function is necessary to make active changes to the database server, and requires that the user whose account information was used to open the database connection have sufficient permissions.

mysqlSelectDB ( database-name )
Returns: boolean Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference
database-name Select as current database

When given a valid mysql-link, selects database-name as the current database on the remote MySQL server.

mysqlShutdown ( mysql-link )
Returns: boolean Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference

When given a valid mysql-link, instructs the remote MySQL server to shutdown. Returns true if the shutdown was successful.

The user whose account information was used to open the database connection must have sufficient permissions, or an access denied runtime error will occur.

mysqlStat ( mysql-link )
Returns: string Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference

When given a valid mysql-link, returns useful status information about the MySQL server.

Example:

print mysqlStat(mysql_link);
Uptime: 373803 Threads: 2 Questions: 2356 Slow queries: 0 Opens: 31 Flush tables: 1 Open tables: 10 Queries per second avg: 0.006

mysqlStoreResult ( mysql-link )
Returns: results-link Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference

When given a valid mysql-link, returns a results reference link used to access result set data from the most recent query. The results are stored and held in memory, so there must be enough free memory to hold the entire result set.

If the result set is large, use mysqlUseResult() as an alternative, more memory-conscious approach.

mysqlThreadID ( mysql-link )
Returns: integer Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference

When given a valid mysql-link, returns the current thread ID used by the database connection on the remote MySQL server.

Example:

print mysqlThreadID(mysql_link);
578

mysqlUseResult ( mysql-link )
Returns: results-link Location: Kingfisher
Parameter Description
mysql-link MySQL server connection reference

When given a valid mysql-link, returns a results reference link used to access result set data from the most recent query. This function does not retrieve all of the results into memory as with mysqlStoreResult() and is helpful when working with a large result set.

However, it is important to note that mysqlNumRows() will not return the total number of results until all data has been retrieved using mysqlFetchRow() or mysqlFetchAllRows().

contents prev next

Copyright (c)1996-2003 Purity Software