473,386 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

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:
mysql_error($link_id);

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.......

Examples

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);

$db->selectdb($database);

@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);

$db->selectdb($database);

$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  

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.