I'm using PHP and a MySQL database to build a website.
However, I find PHP quite horrible compared to other languages I have learnt previously. Largely because I seem to find a lot of instructions online which are over 5 years old and they just don't work.
For example the accepted answer here: select count(*) from table of mysql in php
I have this code so far, but it gives me error (undefined index $recordCount = $row['totalEntries'];).
// create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// check connection
if ($conn->connect_error) {
die("connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM games ORDER BY id DESC LIMIT $startRow, $rowsPerPage";
$result = $conn->query($sql);
$sql = "SELECT COUNT(*) as totalEntries FROM games";
$results = $conn->query($sql);
$row = $result->fetch_assoc();
$recordCount = $row['totalEntries'];
echo "<br/> record count = " . $recordCount;
$totalPages = ceil($recordCount / $rowsPerPage);
$pagination = "<div class='pagination'>";
for ($i = 0; $i <= $totalPages; $i++) {
$pagination .= "<a href='index.php?page=" . $i . "'>" . $i . "</a>";
echo 'wtf!';
}
$pagination .= "</div>";
echo ' <br/> pagination = ' . $pagination;
I also tried:
$recordCount = Count($row);
and
$recordCount = Count($result->num_rows);
and many other parameters passed into Count but they always return either 1 or 7(which is the number of COLUMNS)
I guess I find PHP so hard to learn because I don't have a good IDE for it and am using Notepad++. Can anyone tell me how we get the Count of all entries in the table after I have done the SELECT statement as in the above code?