Connecting to MySQL using PHP or Perl
-
The purpose of this document is to help the MySQL account holders get started with developing their Web front for their MySQL database.
-
Please note that the IU/UITS WebMaster and IU/UITS WebHost groups will not be providing services related to database design, creation, and management (often referred to as the DBA, or DataBase Administrator role) or Web to database application programming services as part of the mySQL database service.
-
We provide the following codes as an example of scripts connecting to MySQL DB from your Veritas/Champion account when you use PHP or Perl scripts.
-
To make the code work for your account, replace port_number with your mysql account's port number, user_name with your user name, and password with the user's password.
I. PHP
#!/usr/local/bin/php
//This line should be the first line of the script
//and "#" should be the first character of that line
<?php
//connect to the database server
$db = mysql_connect("mysql.iu.edu:port_number", "user_name", "password");
//report the connection failure or success
if (!$db) {
echo "there was a problem connecting to the database.";
exit;
}
if ($db) {
echo "there was no problem connecting to the database.";
exit;
}
?>
Note: Save this code with the file extension .php, and set the permission of the file to 700 with the command, chmod 700 filename.php.
II. Perl
#!/usr/local/bin/perl
use DBI;
#### main program
$dbpath = "dbi:mysql:database=database_name;host=mysql.iu.edu:port_number";
$dbh = DBI->connect($dbpath, "user_name", "password")
or die "Can't open database: $DBI::errstr";
print "Content-type:text/html", "\n\n";
print "Successful MySQL DB Connection";
exit 0;
Note: Save this code with the file extension .pl and set the permission of the file to 700 with the command, chmod 700 filename.pl.



