In order to access a MySQL database from within your survey, you'll need to make sure you have the following pieces of information:
- host name (i.e. mysurvey.com)
- database name
- database user name
- database password
- table name (see below)
The first four items are the same items you have already specified in Lighthouse Studio's Web Server Management dialog box. The table name will come from one of the several tables that are created for each survey. Below is a list of the tables and their fields. Note that the fields of the data1
table will match those you see when you use the Data Management dialog box to view/edit data within Lighthouse Studio.
Table Fields
studyname_admin_log
- date : text
- action : text
- info : text
- ip_address : text
- user_agent : text
- time_stamp : int(11)
studyname_clists
- sys_RespNum : int(11)
- list_name : text
- value : text
studyname_data1
- sys_RespNum : int(11)
- sys_CheckSum : int(11)
- sys_DataSource : int(11)
- sys_StartTime : int(11)
- sys_EndTime : int(11)
- sys_RespStatus : int(11)
- sys_DispositionCode : int(11)
- sys_LastQuestion : text
- sys_UserJavaScript : tinyint(1)
- sys_UserAgent : text
- sys_OperatingSystem : text
- sys_Browser : text
- sys_IPAddress : text
- ...
studyname_history
- sys_RespNum : int(11)
- hop : int(11)
- ipaddress : varchar(16)
- timestamp : int(11)
- quest_name : varchar(64)
- quest_version : varchar(128)
- page_num : int(11)
- limbo : tinyint(1)
- data : text
studyname_info
- data_version : int(11)
- layout_build : text
- password_build : text
- study_path : text
- num_data_tables : int(11)
- remove_num : int(11)
- survey_paused : tinyint(1)
- close_survey : tinyint(1)
- close_survey_msg : text
studyname_map
- table : int(11)
- fields : longtext
studyname_quotas (IF QUOTAS ARE PRESENT)
- quota_name : text
- cell_value : int(11)
- cell_limit : int(11)
studyname_passwords (IF PASSWORDS ARE USED)
- Password : text
- max_respondents : int(11)
Sample Code
Below is the basic code we'll use. In the CONFIG VARIABLES section you'll need to plug in the database information mentioned earlier into the corresponding fields.
[% Begin Unverified Perl
#!/usr/bin/perl
# PERL MODULE
use DBI;
# CONFIG VARIABLES
my $host = "________________";
my $database = "DBI:mysql:________________";
my $tablename = "________________";
my $user = "________________";
my $pw = "________________";
my $sql = "SELECT * FROM $tablename";
# PERL MYSQL CONNECT
my $connect = DBI->connect($database, $user, $pw)
or die "Connection Error: $DBI::errstr\n";
my $sth = $connect->prepare($sql);
$sth->execute
or die "SQL Error: $DBI::errstr\n";
# CREATE OUTPUT
my $output .= "\n<ol>:\n";
while (my @row = $sth->fetchrow_array) {
$output .= "\n <li>@row\n</li>";
}
$output .= "\n</ol>\n";
return $output;
End Unverified %]
Notice the MySQL variable called my $sql
. This is where you'll type in the particular MySQL statement that you want to execute. Make sure properly format this MySQL statement to conform with Perl rules, such as adding a \ before certain symbols.
You can find documentation for acceptable MySQL statements on the Internet. A list of handy MySQL commands may be found here: http://www.pantz.org/software/mysql/mysqlcommands.html.
In our example above, the MySQL statement simply selects all of the columns in a particular table. You can make these statements as complex or as simple as you want, including displaying a summary of the data collected from previous respondents. MySQL is very powerful. Just remember that with great power comes great responsibility. You can also purge all of your data with a single MySQL command.
In the CREATE OUTPUT section, the code formats the output in a string and then displays it via the return
command.