Connect to MySQL in PHP

Important:
The Webserve and Webtest servers use PHP 7.1; PHP 5.6 has been retired and is no longer available. For more about PHP at IU, see PHP server-side scripting language.

You must already have a MySQL account and information about PHP on Webserve.

The following code shows how to connect to your MySQL account using PHP's Data Object (PDO). Replace [PORT], [DATABASE], [USER], and [PASSWORD] with your port, your database name, your mysql user name, and your mysql user password.

Note:

If you are using MySQL for Webserve, note the following server changes:

  • Test: mysql-test.uits.iu.edu
  • Production: mysql.uits.iu.edu

The port number for all MySQL for Webserve accounts is 3306.

<?php
  try {
        $host = 'mysql.iu.edu';
        $port = '[PORT]'; //replace [PORT] with your port number
        $dbname = '[DATABASE]'; //replace [DATABASE] with your database name
        $user = '[USER]'; //replace [USER] with your MySQL user
        $pass = '[PASSWORD]'; //replace [PASSWORD] with your MySQL password
           
        $dbh = new PDO("mysql:host=$host;port=$port;dbname=$dbname", $user, $pass);
    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br />";
        die();
    }
  ?>

This is document bfol in the Knowledge Base.
Last modified on 2018-08-29 15:10:52.

Contact us

For help or to comment, email the UITS Support Center.