mySQL database connection

This is how *I* connect to mySQL database(s). CPanel & Godaddy.

Step 1: create the connection string

// Create connection	
	$servername ="localhost";
	$username="database_admin";
	$password="password";
	$dbname="myDatabase";

	$conn = new mysqli($servername, $username, $password, $dbname);

Step 2: Check for a connection; die if not working

// Check connection
	if ($conn->connect_error) {	
		die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);
		} 

Step 3: build the SQL query. Note: the “\n” is the php line break.

$sql_query = "SELECT table.column,table.column,table.column,  \n"
	   . "FROM table \n"
	   . "WHERE table.column=conditiom \n"
	   . "ORDER By table.column";

Step 4: run the query

$result = $conn->query($sql_query);

Step 5: loop through the results

while($row = $result->fetch_assoc()) {
	$temp_variable = $row['column'];
        //do work with $temp_variable
	}

Step 6: close out the connection

mysqli_close($conn);