Park Productions Park Productions - 1 month ago 10
MySQL Question

How do I port this depreciated mysql script to mysqli

I posted this script earlier to fix a pagination error, now I need to update it to get away from the depreciated mysql but I am having trouble with the ceil(mysql_result) and mysql_fetch_assoc($query) parts.

$per_page = 25;
$pages_query = mysql_query("SELECT count(*) FROM my_db WHERE StartDate >= CURDATE()");
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$query = mysql_query("SELECT *, DATE_FORMAT(StartDate, '%d/%m/%Y') StartDate, CompName, HostState, Location, competitiontypedesc FROM my_db WHERE StartDate >= CURDATE() LIMIT $start, $per_page");

while($query_row = mysql_fetch_assoc($query)) {
echo"
<li class='kleo-masonry-item event-item'>
<div class='member-inner-list animated animate-when-almost-visible bottom-to-top start-animation grey-border'>
<div class='event-cell event-date-cell'>
<p class='no-margin'>" . $query_row["StartDate"] . "</p>
</div>
<div class='event-cell name'>
<p class='no-margin'>" . $query_row["CompName"] ." " .$query_row["LastName"]. "</p>
</div>
<div class='event-cell'>
<p class='no-margin'>" . $query_row["Location"] . "</p>
</div>
<div class='event-cell'>
<p class='no-margin'>" . $query_row["HostState"] . "</p>
</div>
</li>
"
;
}
$prev = $page - 1;
$next = $page + 1;

if(!($page <= 1)){
echo "<a href='?page=$prev'><</a> ";}

if($pages >= 1){
for($x=1; $x<=$pages; $x++){
echo ($x == $page) ? '<b><a href="?page='.$x.'">'.$x.'</a></b> ' : '<a href="?page='.$x.'">'.$x.'</a>';}
}

if(!($page >= $pages)){
echo "<a href='?page=$next'>></a> ";}
}
}

Answer

get used to name variables clearly: $pages_query should be $pagesResultset. here's the essence:

$dbCon = new mysqli(host,user,pas,db);
$resultset = $dbCon->query("select ...");
if($resultset->num_rows == 0) die('issues');
$pageSize = 25;
$pageCount = ceil($resultset->num_rows / $pageSize);
while($row = $resultset->fetch_assoc()) {
    /* do something */
}
$dbCon->close();