|
|
| |
|
·
|
Free Webmail |
|
·
|
E-Mail Anti Virus Scanning |
|
·
|
MySQL4 Optional Support |
|
·
|
Latest PHPMyAdmin |
|
·
|
Free Multiple FTP Accounts |
|
·
|
Free SpamAssassin Install |
| |
|
|
| |
| Question : | How do I connect to Perl using MySQL?
| | Answer : | Assuming the following :-
* MySQL is installed
* You have your own instance (I.E. account larger than the smallest one)
* Username = fred
* Root password letmein
* Connecting to MySQL database "mysql"
#!/usr/bin/perl
use DBI;
$db = "mysql";
$sock = "/tmp/mysql.fred";
$user = "root";
$pass = "letmein";
$dsn = "DBI:mysql:$db;mysql_socket=$sock";
$dbh = DBI->connect($dsn,$user,$pass);
$sth = $dbh->prepare("SELECT * FROM users");
$sth->execute();
while ( my $ref = $sth->fetchrow_hashref() )
{
print "ID = $ref->{'ID'}, Name = $ref->{'Name'}\n";
}
$sth->finish();
$dbh->disconnect();
If you have the smallest account you just need to replace the $sock line as follows :-
$sock = "/tmp/mysql.sock";
| | | Categories
Return to the main FAQ list |
|