Learn to connect database in php

2
13310

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. Now write the given code in index.php file .
  4. You will have the following view of your notepad++ window :

  5. 3

    4

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

  9. To display the data in the table format replace the code in the index.php file by the following one :
  10. You will have the following view of your notepad++ window :

  11. 7

    8

    9

  12. Run the program in the browser window.
  13. The output window will look like as shown below :

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