Database Connectivity using PHP Some Points to Remember: 1. PHP has a boolean datatype which can have 2 values: true or false. However, in PHP, the number 0 (zero) is also considered as equivalent to False. Every other value (1, 2, 17, -34, +1742, etc) is considered as True. In other words, zero is False and non-zero is True. This means that if we write: if ( 0)... It will evaluate to False, and the statements in the body of IF will not be executed. And if we write: if (1)... It will evaluate to True, and the statements in the body of the IF will get executed. Further, a function that returns zero is considered to have returned false. And a function that returns 1 or a non-zero value, is considered to have returned true. 2. The die() function is used to stop execution of a php program. It is similar to the return statement that you have seen in the class. This function simply checks the value of the argument we pass to it, and if the argument is false, it stops the php program (ie, the php page dies). Example: $conn = mysqli_connect("localhost","root","toor"); Here, the first line tries to connect to a MySql server using mysqli_connect() function. If the connection cannot be established, the function will return false which gets stored in a variable called $conn. Then we use an if condition to check if the value of $conn is false. If it is false, then we use the die() function to display an error message and immediately terminate execution of the php page. 3. To connect to a MySQL Database Server using PHP, there are two techniques: a. The MYSQL extension b. The MYSQLI extension The mysql extension was used in earlier versions of PHP but has now been deprecated meaning that its use is officially discouraged and it will be removed in the near future from PHP. There are only small differences in syntax when using either extension. You have seen programs that use functions from both mysql extension and mysqli extension. Here, all the programs will use the mysql extension. You should remember that some new versions of PHP
will show a warning when the page runs, telling you that you should use the new mysqli extension to connect to the database. We can suppress that warning by using the statement: error_reporting(e_all ^ E_DEPRECATED) at the top of each page. If we use mysqli, this statement is not needed. Database Connectivity Programs: 1. Write a PHP Program to Connect to a MySQL Server. Assume that the MySQL Server is running on the local machine and that your MySQL username is root with password toor. After connecting to the MySQL Server, open a database called test present on the server. Program to Connect to MySQL Server + Open a Database named "test" // If we are here, it means connection succeeded echo('successfully connected to mysql server <br/>'); $openstatus = mysql_select_db("test"); // If we are here, it means database was successfully opened echo('successfully opened the database named "test" <br/>'); 2. Write a PHP Program to create a new mysql database named Movies. Assume that your MySQL host is localhost, username is root and password is toor. Program to Create a New MySQL Database named Movies
$sql = "CREATE DATABASE Movies"; $result = mysql_query($sql); die('failed to create the database named "movies"<br/>'); else echo('database was created<br/>'); 3. Write a PHP Program to create a table called Students within the test database. Use same hostname, username and password to connect as in the previous question. The table you create should have the following structure: StudentID FirstName LastName DOB Int Varchar(30) Varchar(30) Date Program to Create new Table "Students" in the database named "test" // If we are here, it means connection succeeded echo('successfully connected to mysql server <br/>'); $openstatus = mysql_select_db("test");
// If we are here, it means database was successfully opened echo('successfully opened the database named "test" <br/>'); $sql = "Create Table Students(StudentID int, FirstName varchar(30), LastName varchar(30), DOB date)"; $result = mysql_query($sql); die('failed to create the Table named "Students" in database named "test"<br/>'); else echo('table was created<br/>'); 4. Suppose there exists a mysql database called test having a table students. Write a PHP Program to read and display all records from the table named students in a database named test on the MySQL Server. Program to retrieve all rows of a table and display them on the page $openstatus = mysql_select_db("test"); $result = mysql_query("select * From Students"); die('query failed <br/>'); while($row = mysql_fetch_row($result)) foreach($row as $x) echo($x);
echo(' '); echo('<br/>'); [output of program 4] 5. Suppose there exists a mysql database called test having a table Students whose structure is shown below: StudentID FirstName LastName DOB Int Varchar(30) Varchar(30) Date Write a PHP Program to display just the FirstName and LastName fields of those students whose StudentID is greater than 5. Program to Select and Display only those students whose ID is greater or equal to 5 $openstatus = @mysql_select_db("test"); $result = mysql_query("select FirstName,LastName From Students Where StudentID >= 5"); die('query failed <br/>'); while($row = mysql_fetch_row($result))
foreach($row as $x) echo($x); echo(' '); echo('<br/>'); 6. Write a PHP Program to insert a new student row in the Students table of the database named mydatabase. Assume the table has the following structure while writing your insert query: StudentID FirstName LastName DOB Int Varchar(30) Varchar(30) Date The Student record you enter should have the values as: StudentID FirstName LastName DOB 11 Muzamil Hassan 1997-12-23 Program to Insert a new row in the "Students" table of "test" database // If we are here, it means connection succeeded echo('successfully connected to mysql server <br/>'); $openstatus = mysql_select_db("test"); // If we are here, it means database was successfully opened echo('successfully opened the database named "test" <br/>'); $sql = "Insert Into Students(StudentID, FirstName, LastName, DOB) Values(11,'Muzamil','Hassan','1997-12-23')"; $result = mysql_query($sql);
table"<br/>'); else die('failed to Insert new Student Record in the "Students" echo('insert was successful<br/>'); 7. Write a PHP Program to create a mysql database whose name is entered by the user in a textbox. Use same hostname, username and passwords to connect to the mysql server as you have in the previous question. The PHP Page should look like this: This problem requires 2 pages: 4. An HTML Form that is shown to the user, where the user enters the name of the database and clicks the submit button (mysql_createdb_form.php) 5. A PHP script that receives the name of the database entered by the user on the HTML form and creates the database (mysql_createdb_script.php) mysql_createdb_form.php Program to Create a Database Whose Name is Entered by the User in a Textbox
<form method="post" action="mysql_createdb_script.php"> <h1>create Database Form</h1> <br/> Enter Database Name: <input type="text" name="txtdbname"> <input type="submit" value="create Database"> </form> mysql_createdb_script.php $database_name=$_post['txtdbname']; if(!$database_name) die('no Database Name Specified. Please try again'); $sql = "CREATE DATABASE ".$database_name; $result = mysql_query($sql); die('failed to create the database named '.$database_name.'<br/>'); else echo('database '.$database_name.' was created<br/>');
<a href="mysql_createdb_form.php">back TO FORM</a> *********