https://dev.webpages.dk/  
 
PHP Read from a Database, part 1
PHP Read from a Database, part 1:

To retrieve information from a database you will use the;

SELECT fieldname FROM tablename

This example will show all rows in the table. If you look to select a single row you should look at the next example as it does exactly that.

 Read from a Database, part 1

How do you Read from a Database using PHP?

Welcome back.

In the last posting I tried to explain how to write some values taken from a HTML form, into a database that we created in an earlier post.

So, I expect that you have created the Database and the Table, and that you have populated the database with some contacts. Because now we will be reading those contacts.
On the PHP tab, is the simple PHP code that does one simple thing – it reads the First and Last names of alle the contacts, as found in the database.

So, now you can list all the entries in the database, and in the example given here, you can see how you can build a link using some of the values.
Because there are several entries in the database we make use of WHILE. So we can loop through all rows, and display them on a webpage. In the next example we will look at selecting only one entry from the database. Hang on for the next solution. We continue learning about reading from a SQL database using PHP. SELECT.

 HTML

<HTML><BODY> 
    <A HREF='http://127.0.0.1/read.php'>Read all contacts from DB</A>
</BODY></HTML>

 PHP

<?php
    $con = mysqli_connect('127.0.0.1', 'root', 'Your_Password', 'dbname'); 

    if (mysqli_connect_errno()) { 
    echo 'Error connecting to DB'; 
    } 

    $SQL_String = mysqli_query($con, 'SELECT * FROM 
    contacts ORDER BY AutoID ASC'); 

    while($row = mysqli_fetch_array($SQL_String))  { 
    echo "" .  
    $row['Firstname'] . " " . $row['Lastname'] . "
    "; 
    } 
?>
Icons made by Freepik from www.flaticon.com Here is an example on how to SELECT field values from a database. Also see part two here
12:44:04