Module 6:WEB SERVER AND SERVER SIDE SCRPTING, PART-2 Chapter 19of Text Book DATABSE Connection to MySQL / PHP Internet & World Wide Web How to Program, 5/e YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College © Yanbu University College DATABASE CONNECTIVITY PHP combined with MySQL are cross-platform (you can develop in Windows and serve on a Unix platform) PHP Connect to the MySQL Server Use the PHP mysqli_connect() function to open a new connection to the MySQL server. Open a Connection to the MySQL Server Before we can access data in a database, we must open a connection to the MySQL server. In PHP, this is done with the mysqli_connect() function. Syntax mysqli_connect(host , username , password , dbname); STEPS CREATE A USER in PHPMyAdmin CREATE A DATABASE CONNECT TO DATABASE DATA RETREIVAL/ MANIPULATION © Yanbu University College © Yanbu University College © Yanbu University College © Yanbu University College © Yanbu University College PHP Create Database and Tables A database holds one or more tables. Create a Database The CREATE DATABASE statement is used to create a database table in MySQL. We must add the CREATE DATABASE statement to the mysqli_query() function to execute the command. The following example creates a database named “MIS352": <?php $con=mysqli_connect(“localhost",“haseena",“husna"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // Create database $sql="CREATE DATABASE MIS352"; if (mysqli_query($con,$sql)) { echo "Database MIS352 created successfully"; } else { echo "Error creating database: " . mysqli_error(); } ?> © Yanbu University College Create a Table The CREATE TABLE statement is used to create a table in MySQL. We must add the CREATE TABLE statement to the mysqli_query() function to execute the command. The following example creates a table named “MArks", with three columns. The column names will be “STD_ID", “STD_NAME" and “STD_MARKS": <?php $con=mysqli_connect("localhost","PHPClass","hello","MIS352"); // Check connection if(mysqli_connect_errno()) { echo "Failed to connect to MySQL:" . mysqli_connect_error(); } // Create table $sql="CREATE TABLE Marks(STD_ID CHAR(30),STD_NAME CHAR(30),STD_MARKS INTO(20))"; // Execute query if(mysqli_query($con,$sql)) {echo "Table Marks created successfully";} else { echo "Error in creating table: " . mysqli_error(); } ?> Note: When you create a database field of type CHAR, you must specify the maximum length of the field, e.g. CHAR(50). The data type specifies what type of data the column can hold. © Yanbu University College PHP MySQL Insert Into Insert Data Into a Database Table The INSERT INTO statement is used to add new records to a database table. Syntax It is possible to write the INSERT INTO statement in two forms. The first form doesn't specify the column names where the data will be inserted, only ...