–This is a continuation of the database and table that we’ve created before, so to understand read the posts

before–

Hey there,

So for today, we’re gonna have a look at what we’ve inserted. We only need to write one page of code. As always, make it ASAP. As Simple As Possible.


 

<!DOCTYPE html>
<body>

<?php

$conn = mysqli_connect(“localhost”,”root”,””,”company”);

$select = “SELECT * FROM staff”;

$retrieve = mysqli_query($conn,$select);

while($row=mysqli_fetch_array($retrieve))
{
    echo $row[‘Name’] . “&nbsp”;

    echo $row[‘StaffID’] . “<br>”;
}

?>

</body>
</html>


And of course, I’d advise you to do it and check the results first before looking at the explanation; at least by that time you’ll have some questions on your mind, which will give you a better understanding after reading the explanation.

Explanation:

1.  $conn = mysqli_connect(“localhost”,”root”,””,”company”);

– As usual, I’m giving the connection syntax to the variable named $conn. And feel free to change the variable name to what’s easy for you to understand; it wouldn’t matter.

 

2.  $select = “SELECT * FROM staff”;

– “SELECT * FROM staff” is a syntax to select data from the table.

means all data that’s inside the table.

– So I’m passing this syntax to the variable named select.

 

3. $retrieve = mysqli_query($conn,$select);

– I queried to connect to the database and select all the data

– This query was passed on to the variable named retrieve.

 

4. while($row=mysqli_fetch_array($retrieve))
{
    echo $row[‘Name’] . “&nbsp”;

    echo $row[‘StaffID’] . “<br>”;
}

while() {}  is a loop, as long as the $retrieve returns a true, it’ll go on. Which also means it’ll automatically stop looping when there’s no more data in the database.

– $row=mysqli_fetch_array($retrieve) Ok right now the mysqli_fetch_array is a syntax to get the data in forms of array; The ONLY thing you need to know about it is it fetches the data. Now, you might be questioning why can’t we just echo $retrieve? The answer would be that if you just echo $retrieve; then the data is not converted into string form, which would result in an error. You don’t really need to study the whole topic of arrays, later when you move on you’ll have your own understanding. So now, the $row variable has the value of mysqli_fetch_array($retrieve). You can name the $row variable with anything, but I chose it because that’s how I was taught to do it 🙂

 

5. echo $row[‘Name’] . “&nbsp”; 

echo is a php syntax ; it is used to output value.

$row[‘Name’] is the column name of what we want to output. Since we decided to name the column ‘Name’, so that’s how we’re going to call it.

“&nbsp” is a html syntax; it means non-breaking space. Its the same as the space button. Try deleting  . “&nbsp” and see the results. Notice that the &nbsp is in “”. This is because we’re putting this html syntax into a php statement( we used echo at first). So to tell the computer it’s a html syntax we put “”.

This dot you have no idea how big a role it’s playing.  It’s used to separate the html syntax from the php syntax.

 

6.    echo $row[‘StaffID’] . “<br>”;

– So just like echo $row[‘Name’] we’re outputting the data under the column StaffID.

“<br>” this is another html syntax. It means break line. Like you pressing the enter button. It’s different from the &nbsp above because &nbsp doesn’t break the line; it just adds a space.

Again this dot separates the php syntax from the html syntax.

– And the <br> is inside “” because it’s a html syntax inside a php statement.

 

So, that’s it. I really hope that you try this a lot of time because no matter how much you think you understand this code it’s never the same as you typing it down and seeing it for yourself. And try to play around like deleting the &nbsp and <br> and see what happens!

 

~~Any questions mail me at imdad@zeroprogrammer.com~~

~~POSTS comes out every day, so keep reading~~