Connect to MySQL in PHP

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

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 Sitehost, note the following server changes:

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

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

<?php
  
  # Name of the Database

  $dbname = 'dbname_placeholder';

  # Name of the Database User

  $dbuser = 'dbuser_placeholder';

  # Password for the Database User

  $dbpass = 'strongpsw_placeholder';

  # Database Hostname

  $dbhost = 'mysql.uits.iu.edu';

  $connect = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");

  mysqli_select_db($connect, $dbname) or die("Could not open the db '$dbname'");

  if ($connect) {

    echo "No errors were found. The credentials are valid, and you can connect to the database!";

  }

  else {

    echo "ERROR:  unable to connect to the database. Please check your credentials!";
 
  }
  
?>

This is document bfol in the Knowledge Base.
Last modified on 2023-07-18 10:41:14.