Connecting Tech Pros Worldwide Help | Site Map

mSQL Queries

By Blair Ireland
Senior Editor, TheScripts.com

PHP has:
$query = msql_query("Your SQL Statement", $link_id);

Perl has:
$query = $db->query($sql);

$sql contains your SQL statement being executed. It does not have the trailing semi-colon, and can be directly inserted instead of using $sql.

Gathering Data From Queries

PHP has:
msql_fetch_row($query);

Perl has:
@array = $query->fetchrow;

The fetchrow() function returns a single row of data from your query into the array @array. After using fetchrow once, it increments itself so using it again will retrieve the next row.

PHP has:
msql_fetch_array($query);

Perl has:
%hash = $query->fetchhash;

This is similar to fetchrow as it retrieves that row's data, and increments itself to the next row afterwards. It is different though as the data is inserted into an associative array, instead of a normal array. Each column name is represented as the key, and the value of that particular row's column is the value. If your row contains more then one column with the same name, the last column in that row will have it's value in the array and trash the other value that had been there before. If this is the case, use fetchrow.

Query Statement Statistics

PHP has:
msql_num_rows($query);

Perl has:
$number = $query->numrows;

This function will return the number of rows returned with your query statement into the scalar $number.

PHP has:
msql_num_fields($query);

Perl has:
$number = $query->numfields;

This function will return to your scalar variable $number the amount of fields returned with your query statement.

String Functions

PHP has:
$string = addslashes($string);

Perl has:
$quoted_string = $db->quote($unquoted_string);

This might not exactly be what you think it is. It does indeed escape all special characters as needed, like PHP's AddSlashes, but, it also encloses your string within two ' quotes, like mSQL requires for statements. You would run your query string through this before using it. It works the exact same way as it does if you are using the MySQL functions.

Error Functions

PHP has:
msql_error();

Perl has:
Msql->errmsg();
or
$db->errmsg();

The latter is usually used for portability reasons, as MySQL doesn't allow the former method. You are only concerned with this if there is the possibility of using your script with MySQL in the future. This function will return the error message, if any, into a string. Usually you use it like $error = $db->errmsg();

Administrative Functions

PHP has:
msql_create_db("database name", $link_id);

Perl has:
$result = $db->createdb($database);

First off, you must have the correct permissions to do this operation. If you are unsure if the database was made, print out the error message afterwards to see why. This allows you to create databases in the specified mSQL connection in a simple and easy fashion.

PHP has:
msql_drop_db("database name", $link_id);

Perl has:
$result = $db->dropdb($database);

Note: Before using this function, make sure it is what you want to do. There is no prompting before deleting your database..... so you must be sure. You may not undo the operation in any way as well, so watch out.

Examples.

Since I showed you how to traverse through table and database listings above, this part will just give you an example on how to send off a typical mSQL query, then fetch your results.

use Msql.
$db = Mysql->connect($host, $database, $user, $password);

$db->selectdb($database);

$querystring = $db->quote($querystring);

$query = $db->query($querystring);

while (%hash = $query->fetchhash) {
print $hash{'Name'}." Has an E-Mail Address of
".$hash{'Email'};
}

« mSQL Functions