MySQL and Perl

software development

#1

Anybody know why I can’t get past the sql query? Chmod is correct, and db functions properly in phpadmin.

use DBI;
print “past use DBI…
”;
#Query
$db = DBI->connect(‘DBI:mysql:dsltestdb’, ‘user’, ‘pwd’);
print “past DBI connect…
”;
$sql = “SELECT * FROM Languages”;
$db->do ($sql);
print “past query…
”;

The past query line never prints - either the connection is wrong (although past DBI prints) or the do is failing. Dreamhost suggested I post this on the forum. How is anyone out there doing this? A prepare function doesn’t work either.


#2

I take it this isn’t running on any dreamhost server as you don’t specify a hostname for your database?

You need to select a way of retrieving the information from the database by using anyone of DBI’s functions, such as selectrow_array or selectall_array, etc. You can’t just say “do this query”, DBI needs to know in what format it should return your data.

Check out the DBI documentation on the MySQL website for more information:

http://www.mysql.com/doc/en/Perl_DBI_Class.html

Check out the sections labeled:

fetchrow_array, fetchrow_arrayref, fetchrow_hashref

and decide what way you want DBI to return your data. Remember that the easiest format for you to use (hashref), is probably the most costly in terms of overheads. But I wouldn’t worry about that now; just get up and running and you can fine tune later.

  • wil

#3

Thanks for the response. That was just a code snip - I’m using fetchrow afterward. The point was it never got there.
My main problem was what you pointed out - I didn’t specify the hostname. Besides that, it seems that my book didn’t mention that the do function is for non-select queries. The link to the Perl DBI class helped a lot. It’s working now - thanks again.


#4

Yes, and your book should also mention that you should use do with great caution as you never prepare a query with do and that makes it more difficult to trap errors. But if you’re 100% certain of the outcome of a do then it can be a lot faster than the prepare/execute method.

Glad I could help.

  • wil