Connecting PHP and MYSQL Chapter Connecting PHP and MYSQL version CHAPTER version CHAPTER - - - - Chapter Connecting PHP and MYSQL The MySQLi extension contains the following important functions which are related to MySQL database connectivity and management. () Function () Function ()Function . . Database Connections: Before accessing MySQL Database, connect to Database Server machine via PHP scripting language using () Function.
Syntax: (“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 describes managing database connection methods and features. <?php $servername = “localhost”; $username = “username”; $password = “password”; $ = “ ”; $conn = ($servername, $username, $password,$ ); if (!$conn) { die(“Connection failed: “ . ()); } version CHAPTER version CHAPTER - - - - Chapter Connecting PHP and MYSQL echo “Connected successfully”; ?> In the above code snippet, four variables are used to connect to the Database server. They are $servername -> Database Server Server IP address $username -> Database Server User Name $password -> Database Server Password $ -> Database Name The function uses these variables to connect Database server to PHP.
If connection gets fail, output will be printed with MySQL error code. Otherwise connection is success. . .
Performing Queries The main goal of MySQL and PHP connectivity is to retrieve and manipulate the data from MySQL database server. The SQL query statements help in PHP MySQL extension to achieve the objective of MySQL and PHP connection. “ ” is a function, that helps to execute the SQL query statements in PHP scripting language. Syntax: (“Connection Object”,”SQL Query”) Example: $con= (“localhost”,” ”,” ”,” “); $sql=”SELECT , FROM student”; ($con,$sql); .
. Closing Connection: () Function is used to close an existing opened database connection between PHP scripting and MySQL Database Server. Syntax: (“Connection Object”); Example: <?php version CHAPTER version CHAPTER - - - - Chapter Connecting PHP and MYSQL $con= (“localhost”,”$user”,”$password”,” ”); ($con); ?> Example of PHP