MySQL

Aantal rijen tellen

Aantal rijen tellen in mysql
<?php
$conn = new mysqli ("server", "user", "password", "database");
$sql = "SELECT * FROM top_5000_songs WHERE `artist` = 'abba' ORDER BY year ASC";
$result = $conn -> query($sql);
$row_count = $result->num_rows;
while ($row = $result -> fetch_assoc()) {
echo "<div>" . $row["song"] . "  (" . $row["artist"] . ", " . $row["year"] . ")</div>";
}
$conn -> close();
?>
<?php
echo "<div class='total-rows'>" . $row_count . " rijen </div>";
?>

<?php 
$server_name = "";
$user_name = "";
$user_password = "";
$database_name = "";
$table_name = "top_5000_songs";
$error_message_no_connection = "Verbinding met de database is niet mogelijk op dit moment.";
$error_message_no_results = "Geen resultaten gevonden";
$conn = new mysqli ($server_name, $user_name, $user_password, $database_name);
mysqli_set_charset ($conn, "utf8");
if ($conn -> connect_error) {
die ("<strong>" . $error_message_no_connection . "</strong>:</br>" . $conn->connect_error);
} 
$sql = "SELECT * FROM " . $table_name . " WHERE `artist` = 'abba' ORDER BY year ASC";
$result = $conn -> query($sql);
$row_count = $result->num_rows;
if ($result -> num_rows > 0) {
while ($row = $result -> fetch_assoc()) {
echo "<div>" . $row["song"] . "  (" . $row["artist"] . ", " . $row["year"] . ")</div>";
}
} 
else {
echo "<strong>" . $error_message_no_results . "</strong>";
}
$conn -> close();
?>
<?php
echo "<div class='total-rows'>" . $row_count . " rijen </div>";
?>

<?php 
// ========= 01 ==========
$server_name = "";
$user_name = "";
$user_password = "";
$database_name = "";
// ========= 02 ==========
$table_name = "top_5000_songs";
// ========= 03 ==========
$error_message_no_connection = "Verbinding met de database is niet mogelijk op dit moment.";
$error_message_no_results = "Geen resultaten gevonden";
// ========= 04 ==========
$conn = new mysqli ($server_name, $user_name, $user_password, $database_name);
// ========= 05 ==========
mysqli_set_charset ($conn, "utf8");
// ========= 06 ==========
if ($conn -> connect_error) {
die ("<strong>" . $error_message_no_connection . "</strong>:</br>" . $conn->connect_error);
}
// ========= 07 ========== 
$sql = "SELECT * FROM " . $table_name . " WHERE `artist` = 'abba' ORDER BY year ASC";
$result = $conn -> query($sql);
// ========= 08 ==========
$row_count = $result->num_rows;
// ========= 09 ==========
if ($result -> num_rows > 0) {
// ========= 10 ==========
while ($row = $result -> fetch_assoc()) {
echo "<div>" . $row["song"] . "  (" . $row["artist"] . ", " . $row["year"] . ")</div>";
}
} 
// ========= 11 ==========
else {
echo "<strong>" . $error_message_no_results . "</strong>";
}
// ========= 12 ========== 
$conn -> close();
?>
<?php
echo "<div class='total-rows'>" . $row_count . " rijen </div>";
?>

MySQL and PHP Lessons for Beginners count rows in a table