All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Select Query In PHP MySQL With Example

Last Updated : Mar 11, 2024

Select Query In PHP MySQL With Example

In this tutorial we will show you the solution of select query in PHP MySQL with example, today we are going to understand how to use select query in php MySQL. As we know that select query is used to select data from MySQL.

We can use select query in many different ways in php. Here, below we are going to give you some examples so that you see that how to use select query.

Step By Step Guide On Select Query In PHP MySQL With Example :-

Here, as we say that select query is used to select data from MySQL with is a database. A general syntax of select query is given as:

SELECT * FROM table_name

Here, * is used to select all columns, we can also specify the names of columns here and table_name is the name of table which we create in database. Now, let us see how we can use select query.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>home page</title>
</head>
<body>
   <?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
$query = "SELECT id, firstname, lastname FROM students";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo " Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}
mysqli_close($conn);
?>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, inside body as we can see that we use php tags and inside them we establish our connection with database and when our connection established successfully.
  6. Then our query runs in which we use select query on two columns whereas our table name is students. It means that our student table only two columns are showed having name as firstname and lastname.
  7. Here are many other method with help of which we can use select query. We will learn them one by one in next articles. Then our connection with query checked. If is true then our data got displayed on screen.
  8. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to use select query in php mysql.

I hope this tutorial on select query in PHP MySQL with example helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪