Connect to MySQL in PHP

Important:
On Webserve, the default version of PHP is 7.1; however, PHP 5.6 will still be available until it is retired at the end of December 2018. Webtest servers are already using PHP 7.1. For help specifying which version of PHP to use, 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 New MySQL, note the following server changes:

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

The port number for all New 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-02-14 08:33:27.

  • Fill out this form to submit your issue to the UITS Support Center.
  • Please note that you must be affiliated with Indiana University to receive support.
  • All fields are required.

Please provide your IU email address. If you currently have a problem receiving email at your IU account, enter an alternate email address.

  • Fill out this form to submit your comment to the IU Knowledge Base.
  • If you are affiliated with Indiana University and need help with a computing problem, please use the I need help with a computing problem section above, or contact your campus Support Center.

Please provide your IU email address. If you currently have a problem receiving email at your IU account, enter an alternate email address.