AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

PHP MySQL Select Data

PHP-MySQLi Tutorial » PHP MySQL Select Data

How to select data from mysql database tables using PHP?

How to select data from mysql database tables using PHP MySQLi and PDO

The SQL SELECT statement is used to select the records from database tables. Its basic syntax is as follows:

Syntax:

SELECT column1name, column2name, columnNname FROM tablename;)

In the previous chapter we have creat an empty table "Students", with six columns as: "id", "first_name", "last-name", "email", "password" and "date" and we select row from database.

 

PHP MySQL select data to display from database ( MySQLi Object-oriented ) - example

Open a Connection to MySQL

select data to display from mysql database using an html form

The following examples select data to display from database "Students" table:

Example: (MySQLi Object-Oriented)
<?php
$server_name = "localhost";
$user_name = "username";
$password = "password";
$db_name = "myDB";

// Create connection
$con = new mysqli($server_name, $user_name, $password, $db_name);
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}

$sql = "SELECT id, first_name, last_name FROM Students";
$result = $con->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["first-name"]. " " . $row["last-name"]. "<br>";
}
} else {
echo "0 results";
}
$con->close();
?>

Note:
If you remember from the preceding chapter, id field was marked with AUTO_INCREMENT flag. This modifier tells the MySQL to automatically assign a value to this field if it is left unspecified, by incrementing the previous value by 1.

Function num_rows() checks if in database are more than zero rows.

If we have more than zero rows then returned, function fetch_assoc() where puts all the results into an associative array that we can loop through.
The while() - loops through the result set and outputs the data from the id, first_name and last_name columns.

 

PHP MySQL select data from mysql using ( PHP MySQLi Procedural )

How to select data from mysql database tables using PHP MySQLi Procedural

Example: ( MySQLi Procedural )
<?php
$server_name = "localhost";
$user_name = "username";
$password = "password";
$db_name = "myDB";

// Create connection
$con = mysqli_connect($server_name, $user_name, $password, $db_name);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT id, first_name, last_name FROM Students";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["first_name"]. " " . $row["lastn_ame"]. "<br>";
}
} else {
echo "0 results";
}

mysqli_close($con);
?>

 

PHP MySQL Select Data, php mysql select data between two dates, php mysql select data from multiple tables, from table, from two tables, php mysql retrieve data, php mysql drop down menu to select data to display select data from a mysql database using an html form, select data from multiple databases mysql, select data from another database mysql, select data from form to mysql database php, how to select data from mysql database tables using php, android volley select data from mysql database by id specified by user

PDO get / select data from database - example

Example (PDO) select data from database - is a little bit different:

Example: (PDO)
<?php
echo "<table style='border: dotted 2px green;'>";
echo "<tr><th>Id</th><th>First_name</th><th>Last_name</th></tr>";

class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}

function current() {
return "<td style='width: 200px; border: 2px dotted green;'>" . parent::current(). "</td>";
}

function beginChildren() {
echo "<tr>";
}

function endChildren() {
echo "</tr>" . "\n";
}
}

$server_name = "localhost";
$user_name = "username";
$password = "password";
$db_name = "my_DB_PDO";

try {
$con = new PDO("mysql:host=$server_name;dbname=$db_name", $user_name, $password);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT id, first_name, last_name FROM Dtudents");
$stmt->execute();

// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);

foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$con = null;
echo "</table>";
?>

Note: In the PDO example above we have also specified a database (my_DB_PDO).

 



PHP MySQL Select Data, php mysql select data between two dates, php mysql select data from multiple tables, from table, from two tables, php mysql retrieve data, php mysql drop down menu to select data to display select data from a mysql database using an html form, select data from multiple databases mysql, select data from another database mysql, select data from form to mysql database php, how to select data from mysql database tables using php, android volley select data from mysql database by id specified by user
PHP MySQL Select Data - php mysqli

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Error: Unable to connect to MySQL. Debugging errno: 1045 Debugging error: Access denied for user 'u142985959_ager'@'localhost' (using password: YES)