PHP program to fetch data from localhost server database using XAMPP

XAMPP is a free and open source cross platform web server solution stack package developed by Apache which allow a web application to be easily tested on local web server. Here, we can manually create a relational database and store data in tabular form by going to this link. But to operate on localhost or for storing data first we have to start Apache and MySQL from XAMPP control panel.

Let for example, database name is server, table name is user_info having column name as ID, First Name, Username and Password and we have to fetch the data stored there. So, below is the PHP program whose task is to fetch data.



filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// this php script is for connecting with database
// data have to fetched from local server
$mysql_host = 'localhost';
  
// user name is root
$mysql_user = 'root';
  
// function to connect with database having 
// argument host and user name
if (!@mysql_connect ($mysql_host, $mysql_user))
{
    die('Cannot connect to database');
}
else
{
    // database name is server
    if (@mysql_select_db('server'))
    {
        echo "Connection Success";
    }
    else
    {
        die('Cannot connect to database');
    }
}
?>

chevron_right


Output:

Connection Success

Let above script is saved with name database.php inside htdocs folder of XAMPP installation.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// going to use above code
require 'database.php';
  
// printing column name above the data
echo 'ID'.' '.'Name'.' '.'User'.' '.'Pass'.'<br>';
  
// sql query to fetch all the data
$query = "SELECT * FROM `user_info`";
// mysql_query will execute the query to fetch data
if ($is_query_run = mysql_query($query))
{
    // echo "Query Executed";
    // loop will iterate until all data is fetched
    while ($query_executed = mysql_fetch_assoc ($is_query_run))
    {
        // these four line is for four column
        echo $query_executed['ID'].' ';
        echo $query_executed['First Name'].' ';
        echo $query_executed['Username'].' ';
        echo $query_executed['Password'].'<br>';
    }
}
else
{
    echo "Error in execution";
}
?>

chevron_right


Output:

ID Name User Pass
1 Name1 user1 pass1
2 name2 user2 pass2
3 name3 pass3 pass3

For installing and working with XAMPP, refer to this link.

This article is contributed by Aditya Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.