469,295 Members | 1,978 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Perl MySQL Functions

By Blair Ireland
Senior Editor, TheScripts.com

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 MySQL requires for statements. You would run your query string through this before using it.

Error Functions

PHP has:

Perl has:
$error_message = $db->errmsg();

This will return the string containing the error message from your last MySQL operation.

Administrative Functions

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

Perl Has:
$newdb = $dbh->createdb("database name"); Want a new database, quickly and easily? This function will make it for you. Just a note though, you must have the correct priveledges to do this operation, otherwise the operation will not actually 'do' anything. To make sure the database was made, check the $db->errmsg(); return. PHP has:
mysql_drop_db("database_name", $link_id); Perl has:
$nodb = $db->dropdb("database name"); This will, as you might guess, drob the database specified. There is no way to cancel this operation, as there is no prompting of any kind, and you may not undo the operation in any way from perl. This is a warning.......


To finish this off, we will have a couple of examples.

Here is how to traverse through all of your tables.

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


@tables = $db->listtables;

foreach $table (@tables) {
print $table."\n<BR>";

That's it! The next example is how to do a normal query in your program, and fetch the results.

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


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

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

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

There you go, now you can make your MySQL perl apps. Not only are these function much easier then having to use a petty text file for a database, but they are also much quicker.

« Query MySQL