Skip to content

Instantly share code, notes, and snippets.

@ChrisMoney
Last active October 13, 2015 17:08
Show Gist options
  • Save ChrisMoney/4228766 to your computer and use it in GitHub Desktop.
Save ChrisMoney/4228766 to your computer and use it in GitHub Desktop.
Perl - Select database and return data from it
#!/usr/bin/perl
use Mysql;
print "Content-type: text/html \n\n";
# MYSQL CONFIG VARIABLES
$host = "localhost";
$database = "store";
$tablename = "inventory";
$user = "username";
$pw = "password";
# PERL MYSQL CONNECT()
$connect = Mysql->connect($host, $database, $user, $pw);
# SELECT DB
$connect->selectdb($database);
# DEFINE A MySQL QUERY
$myquery = "SELECT * FROM $tablename";
# EXECUTE THE QUERY FUNCTION
$execute = $connect->query($myquery);
# HTML TABLE
print "<table border='1'><tr>
<th>id</th>
<th>product</th>
<th>quantity</th></tr>";
# FETCHROW ARRAY
while (@results = $execute->fetchrow()) {
print "<tr><td>"
.$results[0]."</td><td>"
.$results[1]."</td><td>"
.$results[2]."</td></tr>";
}
print "</table>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment