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. You will have the following view of your notepad++ window :

  6. 3

    4

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

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

  13. 7

    8

    9

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

  17. Thus we have learnt the database connection in php .
Learn to connect database in php was last modified: by

Facebook Comments

Website Comments

  1. Antonina
    Reply

    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

Post a comment