Wednesday, April 23, 2025

MySQLi Select All

select_all.php


<?php
//Config

$server = "localhost";
$user = "root";
$password = "";
$database = "address_book";

//Establishing a Connection to MySQL Server
$connection = mysqli_connect($server, $user, $password, $database);

//Check Connection
if (!$connection) {
	die("<h2>Total Fail</h2> " . mysqli_connect_error());
} else {
	echo "Connection Successfull <br>";
}

//SQL Command
$sql_command = "SELECT * FROM people";
$action = mysqli_query($connection, $sql_command);

while ($line = mysqli_fetch_assoc($action)) {
	echo "ID: " . $line["id"] . "<br>";
}

?>

The above PHP code executes an SQL SELECT statement to retrieve all data from the "people" table and prints the "id" field for each record retrieved using a while loop.

Here is an explanation of each line:

  • $sql_command = "SELECT * FROM people"; - This sets the SQL command to select all data from the "people" table.
  • $action = mysqli_query($connection, $sql_command); - This executes the SQL command on the database using the established connection and stores the result in the $action variable.
  • while ($line = mysqli_fetch_assoc($action)) { - This starts a while loop that runs as long as there are rows in the result set to be processed.
  • echo "ID: " . $line["id"] . "<br>"; - This prints the "id" field value of the current row in the result set to the screen, with some HTML formatting.
  • } - This ends the while loop.

Note that this code could be extended to print out more fields or to process the data in some other way.

Initial report with just IDs is ok:


Connection Successfull
ID: 1
ID: 2
ID: 3
ID: 4
ID: 5
...
ID: 24
ID: 25
ID: 26

select_all_2.php


<?php
//Config

$server = "localhost";
$user = "root";
$password = "";
$database = "address_book";

//Establishing a Connection to MySQL Server
$connection = mysqli_connect($server, $user, $password, $database);

//Check Connection
if (!$connection) {
	die("<h2>Total Fail</h2> " . mysqli_connect_error());
} else {
	echo "Connection Successfull <br>";
}

//SQL Command
$sql_command = "SELECT * FROM people";
$action = mysqli_query($connection, $sql_command);

while ($line = mysqli_fetch_assoc($action)) {
	echo "ID: " . $line["id"] . "<br>";
	echo "Name: " . $line["name"] . "<br>";
	echo "Lastname: " . $line["lastname"] . "<br>";
	echo "Telephone: " . $line["telephon"] . "<br>";
	echo "email: " . $line["email"] . "<br>";
	echo "Address: " . $line["address"] . "<br>";
	echo "Date/Time: " . $line["meta"] . "<br>";
	echo "<hr>";
}

?>

This PHP code performs the following tasks:

  1. Define a SQL command to select all columns from the table "people".
  2. Execute the SQL command using the mysqli_query() function, which returns a result set.
  3. Loop through each row in the result set using the mysqli_fetch_assoc() function.
  4. For each row, print out the values of each column in the row, using associative array keys to access the values.
  5. Separate each row with a horizontal rule ("<hr>").

Small part of report for first ID:


Connection Successfull
ID: 1
Name: Samantha
Lastname: Fox
Telephone: 555444
email: mail@server.com
Address: Main Road 12a
Date/Time: 2020-07-25 12:01:51
...

select_all_3.php


<?php
//Config

$server = "localhost";
$user = "root";
$password = "";
$database = "address_book";

//Establishing a Connection to MySQL Server
$connection = mysqli_connect($server, $user, $password, $database);

//Check Connection
if (!$connection) {
	die("<h2>Total Fail</h2> " . mysqli_connect_error());
} else {
	echo "Connection Successfull <br>";
}

//SQL Command
$sql_command = "SELECT * FROM people";
$action = mysqli_query($connection, $sql_command);

while ($line = mysqli_fetch_assoc($action)) {
	echo "ID: " . $line["id"] . 
	"Name: " . $line["name"] . 
	"Lastname: " . $line["lastname"] . 
	"Telephone: " . $line["telephon"] . 
	"email: " . $line["email"] . 
	"Address: " . $line["address"] . 
	"Date/Time: " . $line["meta"]
	. "<hr>";
}

?>

Part of report:


Connection Successfull
ID: 1 Name: Samantha Lastname: Fox Telephone: 555444 email: mail@server.com Address: Main Road 12a Date/Time: 2020-07-25 12:01:51
ID: 2 Name: Samantha Lastname: Fox Telephone: 555444 email: mail@server.com Address: Main Road 12a Date/Time: 2020-07-25 12:04:28
ID: 3 Name: Samantha Lastname: Fox Telephone: 555444 email: mail@server.com Address: Main Road 12a Date/Time: 2020-07-25 12:04:29

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...