Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
1.2k views
in Connecting PHP and MYSQL by (54.8k points)
closed by

Explain in details types of MySQL connection method in PHP?

1 Answer

+1 vote
by (49.1k points)
selected by
 
Best answer

Database Connections:

Before accessing MySQL Database, connect to Database Server machine via PHP scripting language using Mysqli_connect( ) Function.

Syntax:

mysqli_connect(“Server Name” , “User Name” , “Password” , “DB Name”);

This function requires four parameters to connect to database server. Database Server name, Database username, password and Database Name.

Managing Database Connections:

The below code snippet describes managing database connection methods and features<!--?php $servemame = “localhost”; $usemame = “username”; $password = “password”; SDB_name = “SchoolDB”;-->

<?php

$servemame = “localhost”;

$usemame = “username”;

$password = “password”;

SDB_name = “SchoolDB”;

// Create connection

$conn = mysqli_connect($servemame, Susemame, Spassword,$DB_name);

// Check connection

if(!$conn){

die(“Connection failed: “ . mysqli_connect_error( ));

}

echo “Connected successfully”;

?>

In the above code snippet, three variables are used to connect to the Database server. 

They are 

1. $servemame → Database Server Server IP address

2. $username → Database Server User Name

3. $password → Database Server Password 

4. $DBName → Database Name

The mysqli_connect function uses these variables and connect Database server from PHP scripting. If connection gets fail, output will be printed with MySQL error code. Otherwise connection is success.

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...