I need to know how I can display the data stored in the database using php when a user load a page, without necessary puting them as it is on a static web page.

Recommended Answers

All 3 Replies

It is called AJAX. Once the page is loaded it enables you to get some more data and display it in a chosen element in the page (a div for example). It uses an XMLHTTP or XHR Javascript object that all modern browsers support to make additional request.

It is a good idea to use Javascript libraries such as jquery ajax to use this functionality since the hardest parts (such as supporting a wide variety of browsers) has already been solved there.

Member Avatar for John_81

What I've done in the past with similar information is to create an array in a global variable that I can then call later when needed. For example, this snippet of code pulls all of a user's data out of the database from their session id and stores it in a global variable:

//Define this before any code, of course:
$link = mysqli_connect($host, $user, $pass, $db) or die("Error " . mysqli_error($link));

//Define this function somewhere, preferably in a "functions.php" file that you call in an ititializer, or "init.php" file:
function user_data($user_id, $link)
{
    $data = array();
    $user_id = (int)$user_id;
    $query = mysqli_query($link, "SELECT * FROM users WHERE user_id = '$user_id'");
    $data = mysqli_fetch_assoc($query);

    return $data;
}

//This checks if the viewer on the website is logged in, and if so, uses the user_id variable that I've set in a login function as a parameter in the user_data() function:
if (logged_in() === true) {
    $session_user_id = $_SESSION['user_id'];
    $user_data = user_data($session_user_id, $link);
}

//You can now pull data from the global variable as needed by pointing to a value stored in the $user_data array under a database collumn name, as such:
$name = $user_data['name'];

OK, maybe I missunderstood your question. So, you want to display user data from the database (if the user is loged in) and user data shall be read from a databse (based on user_id stored in the session)? If this is true than you do not need ajax.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.