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 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.