Verification: a143cc29221c9be0

Mysqli connection failed with php

PHP mysqli_connect function

The PHP mysql connect function is used to connect to a MySQL database server.

It has the following syntax.

HERE,

  • “$db_handle” is the database connection resource variable.
  • “mysqli_connect(…)” is the function for php database connection
  • “$server_name” is the name or IP address of the server hosting MySQL server.
  • “$user_name” is a valid user name in MySQL server.
  • “$password” is a valid password associated with a user name in MySQL server.

PHP mysqli_select_db function

The mysqli_select_db function is used to select a database.

It has the following syntax.

  HERE,

  • “mysqli_select_db(…)” is the database selection function that returns either true or false
  • “$database_name” is the name of the database
  • “$link_identifier” is optional, it is used to pass in the server connection link

PHP mysqli_query function

The mysqli_query function is used to execute SQL queries.

The function can be used to execute the following query types;

  • Insert
  • Select
  • Update
  • delete

It has the following syntax.

  HERE,

  • “mysqli_query(…)” is the function that executes the SQL queries.
  • “$query” is the SQL query to be executed
  • “$link_identifier” is optional, it can be used to pass in the server connection link

PHP mysqli_num_rows function

The mysqli_num_rows function is used to get the number of rows returned from a select query.

It has the following syntax.

  HERE,

  • “mysqli_num_rows(…)” is the row count function
  • “$result” is the mysqli_query result set

PHP mysqli_fetch_array function

The mysqli_fetch_array function is used fetch row arrays from a query result set.

It has the following syntax.

  HERE,

  • “mysqli_fetch_array(…)” is the function for fetching row arrays
  • “$result” is the result returned by the mysqli_query function.

PHP mysqli_close function

The mysqli_close function is used to close an open database connection.

It has the following syntax.

  HERE,

  • “mysqli_close(…)” is the PHP function
  • “$link_identifier” is optional, it is used to pass in the server connection resource

Let’s look at practical examples that take advantage of these functions.

Creating the MySQL database This tutorial assumes knowledge of MySQL and SQL, if these terms are unfamiliar to you, refer to our MySQL and SQL tutorials.

We will create a simple database called my_personal_contacts with one table only.

Below are the steps to create the database and table.

  • Connect to MySQL using your favorite access tool such as MySQL workbench, phpMyAdmin etc.
  • Create a database named my_person_contacts
  • Execute the script shown below to create the table and insert some dummy data

  We now have a database set up that we will manipulate from PHP.

Reading records from the database We will now create a program that prints the records from the database.

';         
		echo 'Full Names: ' . $row['full_names'] . '
';         echo 'Gender: ' . $row['gender'] . '
';         echo 'Contact No: ' . $row['contact_no'] . '
';         echo 'Email: ' . $row['email'] . '
';         echo 'City: ' . $row['city'] . '
';         echo 'Country: ' . $row['country'] . '

';     } } mysqli_close($dbh); //close the database connection ?>

  Executing the above code returns the results shown in the diagram shown below

Inserting new records

Let’s now look at an example that adds a new record into our table. the code below shows the implementation.

  Executing the above code outputs “Poseidon has been successfully added to your contacts list” go back to the select query example and retrieval your contacts again.

See if Poseidon has been added to your list.

Updating records Let’s now look at an example that updates a record in the database.

Let’s suppose that Poseidon has changed his contact number and email address.

Deleting records

Let’s now look at an example that removes records from the database. Let’s suppose that Venus has a restraining order against us, and we must remove her contacts info from our database.

Overview

This article contains the basic information you need to connect from PHP to your MySQL database on your (mt) Media Temple service. The most common mistake is in the host name, so check there first:

  • Grid: internal-db.s00000.gridserver.com
  • VPS servers: localhost

NOTE:
Do not use your domain name or IP address as the host name. This can result in an unreliable database server connection.

Requirements

Before you start, you'll need the following:

  • Host: internal-db.s00000.gridserver.comlocalhost
  • Username: db00000 or db00000_usernameSet in Plesk.
  • Password: Your password.
  • Database: Your database name. This should look like db00000_dbname.

You can see a summary of all these settings which are specific for your own server in the AccountCenter, under the Server GuideServer Guide.

Instructions

The host is based on your access domain. You may have to check the advanced section of your database settings to be able to specify this. For external connections, use external-db.s00000.gridserver.com instead and make sure your IP address has been added to the list of allowed external IPs. Click here for details.

For VPS servers, localhost is the default host in most scripts and applications. You likely won't have to enter anything special for this parameter.

The default username with access to all of your databases is db followed by your site number, db00000. You can edit your database user password in the AccountCenter. See Database users on the Grid for instructions on updating passwords and adding database users. If you want to test your username and password, try logging into phpMyAdmin.

This article has instructions for adding a database with a username and password.

CAUTION:
If you update the password for an existing database user, you must update it in any files or applications that use the old password. If you're not sure how to do this, you may want to create a new database user for your current project. The new database user will have the format db00000_username.

Your database name will be of the format db00000_dbname. You must create the database through the AccountCenter first. See How do I create a database? if you need to create your database.

Sample connection script

Below is a sample PHP connection script which shows three different methods of connecting to MySQL. This script is adapted from the PHP manual page "Choosing an API." Be sure to replace the arguments with your own MySQL login credentials.

CAUTION:
As of PHP 5.5.0, mysql_connect() is deprecated and will be removed in the future. You will want to look into using mysqli or PDO.

Filename: mysql_connect.php


query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = $result->fetch_assoc();
echo htmlentities($row['_message']);

// PDO
$pdo = new PDO('mysql:host=internal-db.s00000.gridserver.com;dbname=db00000_dbname', 'db00000_username', 'password');
$statement = $pdo->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = $statement->fetch(PDO::FETCH_ASSOC);
echo htmlentities($row['_message']);

// mysql
$c = mysql_connect("internal-db.s00000.gridserver.com", "db00000_username", "password");
mysql_select_db("db00000_dbname");
$result = mysql_query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = mysql_fetch_assoc($result);
echo htmlentities($row['_message']);

?>

query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = $result->fetch_assoc();
echo htmlentities($row['_message']);

// PDO
$pdo = new PDO('mysql:host=localhost;dbname=database', 'user', 'password');
$statement = $pdo->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = $statement->fetch(PDO::FETCH_ASSOC);
echo htmlentities($row['_message']);

// mysql
$c = mysql_connect("localhost", "user", "password");
mysql_select_db("database");
$result = mysql_query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = mysql_fetch_assoc($result);
echo htmlentities($row['_message']);
?>