Learn to connect database in php

2
14585

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 :

  1. Install Xampp server in your machine and if it is already installed then go to xampp -> htdocs folder.
  2. 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.
  3. 1

  4. Now write the given code in index.php file .
  5. <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>
    

  6. You will have the following view of your notepad++ window :
  7. 2
    3
    4

  8. Now go to xampp control pannel and start the apache and mysql modules .
  9. Open up the browser and enter the url as http://localhost/connect/index.php
  10. The output window will look like as shown below :
  11. v3

  12. To display the data in the table format replace the code in the index.php file by the following one :
  13. <?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>
    

  14. You will have the following view of your notepad++ window :
  15. 6
    7
    8
    9

  16. Run the program in the browser window.
  17. The output window will look like as shown below :
  18. v7

  19. Thus we have learnt the database connection in php .

2 COMMENTS

  1. 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

LEAVE A REPLY

Please enter your comment!
Please enter your name here