In this session we will study, how to connect to database and fetch the data in php.
- To connect to database follow the steps given below :
- Install Xampp server in your machine and if it is already installed then go to xampp -> htdocs folder.
- Create a new folder in the htdocs folder called as connect and in this connect folder create a new file say index.php and open it with notepad++ or any other text editor.
- Now write the given code in index.php file .
- You will have the following view of your notepad++ window :
- Now go to xampp control pannel and start the apache and mysql modules .
- Open up the browser and enter the url as http://localhost/connect/index.php
- The output window will look like as shown below :
- To display the data in the table format replace the code in the index.php file by the following one :
- You will have the following view of your notepad++ window :
- Run the program in the browser window.
- The output window will look like as shown below :
- Thus we have learnt the database connection in php .
<html> <body> <h1>Cheak the database connection</h1> <?php //create the connection $connect=mysqli_connect('localhost','root',' “,'mydatabase'); if($connect) { echo 'Connect to the database<br />'; } else{ echo 'Not connected'; } // perform the select operation $result=mysqli_query($connect,"SELECT first_name,surname_name FROM mydemo"); while($row=mysqli_fetch_array($result)) { $result1=$row['first_name'].' ' .$row['surname_name'].'<br />'; echo $result1; } ?> <br /> <h1> Thrown the error message if not connected</h1> <?php $connect=mysqli_connect('localhost','root',' ','mydatabase1'); if(mysqli_connect_errno($connect)){ echo 'Failed to connect to database'; } ?> </body> </html>
<?php // Create connection $connect=mysqli_connect("localhost","root","","mydatabase"); // Check connection if (mysqli_connect_errno($connect)) { echo "Failed to connect to database: " . mysqli_connect_error(); } mysqli_query($connect,"INSERT INTO employees1 (first_name,last_name,department,email) VALUES ('rahul','maharana','marketing','[email protected]');"); $result = mysqli_query($connect, "SELECT * FROM employees1"); ?> <h1 bgcolor="red"> THIS IS THE EMPLOYEES TABLE </h1> <table width="500" cellpadding=5 cellspacing=5 border=1> <tr> <th>ID#</th> <th>First Name</th> <th>Last Name</th> <th>Dept</th> <th>Email</th> </tr> <?php while($row = mysqli_fetch_array($result)) :?> <tr> <td><?php echo $row['id']; ?></td> <td><?php echo $row['first_name']; ?></td> <td><?php echo $row['last_name']; ?></td> <td><?php echo $row['department']; ?></td> <td><?php echo $row['email']; ?></td> </tr> <?php endwhile; ?> </table>
1st error
Parse error: syntax error, unexpected ‘mydatabase’ (T_STRING) in C:xampphtdocsconnectindex.php on line 8
2nd error
you don’t mention anywhere aboutcreating database in phpmyadmin thus leadin us to next error
3rd
Warning: mysqli_connect(): (HY000/1045): Access denied for user ‘root’@’localhost’ (using password: NO) in C:xampphtdocsconnectindex.php on line 5
Failed to connect to database: Access denied for user ‘root’@’localhost’ (using password: NO)
Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in C:xampphtdocsconnectindex.php on line 20
Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in C:xampphtdocsconnectindex.php on line 23
THIS IS THE EMPLOYEES TABLE
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:xampphtdocsconnectindex.php on line 43
ID# First Name Last Name Dept Email