Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

this is a big question, i know, but i'm writing a script for a weblog-like program for someone's site, and they want me to incorporate the same usernames/passwords as their bb. the thing is, they're using phpBB which stores said user info in a mySQL database, which i have no experience with or knowledge about...

how could i easily get this user info and format it into either a hash or flat text file (whichever's easier)? if you can help me, please go slow and speak as if to a child, because ive never worked with mysql before....

by the way, the versions are 3.x for mysql, 4+ for php

edited: Sat Feb 1 15:50:57 2003 by jeffa - title change (was: ok, this is a doosie...)

Replies are listed 'Best First'.
Re: Extracting data from mySQL
by fokat (Deacon) on Feb 01, 2003 at 05:00 UTC
    they're using phpBB which stores said user info in a mySQL database, which i have no experience with or knowledge about...

    Well, I don't know what phpBB is, but in order to talk to a mySQL database, I suggest you use DBI along with the proper DBD (database driver) for this.

    You can install the required software by doing the commands below (note that you may need super user privileges):

    bash-2.05a# perl -MCPAN -e 'install "DBI"'
    bash-2.05a# perl -MCPAN -e 'install "Bundle::DBD::mysql"'
    

    The book Programming the Perl DBI might be a very useful resource if you have the time and the money.

    how could i easily get this user info and format it into either a hash or flat text file (whichever's easier)?

    Your code would be somewhat similar to this untested example...

    use DBI; my $dbh = DBI->connect($dsn, $login, $password, { RaiseError => 1 } ); my $sth = $dbh->prepare('/* some adequate SQL query */'); o o o $sth->execute($user); my $r_udata = $sth->fetchrow_arrayref; $sth->finish; # $r_udata is a reference to an array where each element matches # the corresponding column on the query passed to ->prepare() o o o $dbh->disconnect;

    Please make sure to use placeholders when building your query. You can post more specific questions once you start coding.

    Best regards

    -lem, but some call me fokat

Re: Extracting data from mySQL
by zengargoyle (Deacon) on Feb 01, 2003 at 14:01 UTC

    since i've recently been doing lot's of db stuff lately (for me anyway) and you seem to have little db/SQL experience at all... i'd say skip to the next level up and use Class::DBI which sits atop Ima::DBI and DBI and does alot of work for you.

    assuming...

    #!/usr/bin/perl use strict; use warnings; # a database - 'phpbbdb' # a database user - 'dbuser' # a database password - 'dbpass' # a table - 'passwords' # # mysql> describe passwords; # +--------+--------------+------+-----+---------+-------+ # | Field | Type | Null | Key | Default | Extra # | # +--------+--------------+------+-----+---------+-------+ # | user | varchar(16) | | PRI | | # | # | passwd | varchar(128) | YES | | NULL | # | # +--------+--------------+------+-----+---------+-------+ # # mysql> select * from passwords; # +-------+------------+ # | user | passwd | # +-------+------------+ # | walla | washington | # +-------+------------+ package MyDB; use base 'Class::DBI'; MyDB->set_db( 'Main', 'dbi:mysql:database=phpbbdb', 'dbuser', 'dbpass' ); package MyPasswords; use base 'MyDB'; MyPasswords->table( 'passwords' ); MyPasswords->columns( All => qw/ user passwd / ); package main; die << "_USAGE_" unless @ARGV; Usage: $0 <username> [<username> ...] Display the password(s) for username(s)! _USAGE_ foreach my $user (@ARGV) { my $info = MyPasswords->retrieve( $user ); printf "user: %s password: %s$/", $info->user(), $info->passwd(); } exit;

    and a little test

    $ test.pl walla
    user: walla password: washington
    $
    

    Update/Addendum: the users will really hate it if they end up having to login twice. phpBB may be using cookies for session management, and may store some of that info in the database as well. it would be ideal if your script could arrange to have the cookie passed to it as well and use the cookie contents and a database query to allow access instead of asking for username/password again.

Re: Extracting data from mySQL
by Ctrl-z (Friar) on Feb 01, 2003 at 12:19 UTC
    dont sweat it, this is easy (ish) ;)

    Youll find plenty of DBI examples online. Youll find everything you need for integrating with phpBB on the guys server, ie in the .php files.
    I know the inside of phpBB quite well* - its pretty easy to follow.

    So, grab some coffee and start looking through the php files. Youll see how phpBB creates sessions, what SQL statements it uses, how it checks cookies and what it looks for etc
    just translate the bits you need back into your own app using DBI and perl.

    DBI is probably the greatest Perl module, you want to get to know it better anyway. This could be a really good learning experience for you.

    *when i first started learning perl i rewrote some of phpBB in perl as it covered a lot of important web-dev concepts and like your friend, I wanted to integrate stuff round the solid forum database. I might still have a few of those files on my HD. If i find anything worth repeating Ill message you

Re: Extracting data from mySQL
by batkins (Chaplain) on Feb 01, 2003 at 04:47 UTC
    Just use the DBI module with DBD::MySQL. Both are available on CPAN.
      but...how do i use it? like i said, i have no experience with mysql
        jues pull up the docs for the modules i mentioned. they're all on CPAN.
Re: Extracting data from mySQL
by vek (Prior) on Feb 01, 2003 at 18:08 UTC
    If you have no experience with MySQL itself then I suggest you go here and read the documentation. Once you are familiar with the database then you can start looking at how to access your database from Perl. For that you've already had some spiffy suggestions from other monks. Reading the documentation for DBI and DBD::MySQL are good starting points. Best of luck.

    -- vek --