Jump to content

How to retrieve data from MySQL database?


KenBrace

Recommended Posts

This tutorial will give a basic overview of how to retrieve information from a MySQL database.

 

Let's say you have a database table entitled "users".

 

This table has five fields:

 

1. ID

2. Username

3. Email

4. Password

5. Gender

 

There is an entry in this table with the following values:

 

1. 1

2. Johnson

3. john_d@gmail.com

4. df6dd8d8f7df8dd8f7d8f7d8fdf7df6d (encrypted password)

5. Male

 

Let's further suggest that you wish to retrieve this user and his information from the database to use it in some way (maybe display on his profile or send him an email).

 

This is how you would go about doing it.

 

PHP File

<?php
 
include_once("conn.php"); //include your database connection file
 
$user = $_GET['user']; //retrieve the name of the user you wish to search via the get method. However you can get the username any way you like. I'm just doing it this way for the tutorial.
 
$fetch_user = "SELECT * FROM users WHERE username='$user'"; //this var holds the command for fetching all fields from the 'users' table where the username is equal to the '$user' variable
 
$query_user = mysqli_query($conn, $fetch_user); //initiate the query and specify both the connection and command
 
if(!$query_user){ //check for an error with the query and echo error message if it fails
echo "There was an error retrieving the user information!";
exit();
}
 
?>
Edited by KenBrace
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...