help needed with MySQL to MySQLi code snippet conversion

2 posts by 2 authors in: Forums > CMS Builder
Last Post: September 18, 2017   (RSS)

By Deborah - September 17, 2017

Hi, All. I have a code snippet in need of conversion for MySQLi.

I've used the following code (probably derived from an old forum post) to present a list of blog month + year archive links. After updating to PHP 7 this code causes the error "UNCAUGHT_EXCEPTION: Call to undefined function mysql_query()".

<?php
$query = "SELECT DATE_FORMAT(pubDate, '%M %Y') as dateAndYear, YEAR(pubDate) as year, MONTH(pubDate) as month FROM cmsb_blog GROUP BY dateAndYear ORDER BY pubDate DESC";
$result = mysql_query($query) or die("MySQL Error: ". htmlspecialchars(mysql_error()) . "\n");
 while ($record = mysql_fetch_assoc($result)):
?>

Simply changing mysql to mysqli isn't enough. I think it needs some further editing and am wondering if anyone here could offer assistance?

~ Deborah

By Dave - September 18, 2017

Hi Deborah, 

Try this :

<?php
$query = "SELECT DATE_FORMAT(pubDate, '%M %Y') as dateAndYear, YEAR(pubDate) as year, MONTH(pubDate) as month FROM cmsb_blog GROUP BY dateAndYear ORDER BY pubDate DESC"; 
$result = mysqli()->query($query) or die("MySQL Error: ". htmlspecialchars(mysqli()->error) . "\n"); 
 while ($record = mysqli_fetch_assoc($result)):
?>

Let me know if that works for you!

Dave Edis - Senior Developer
interactivetools.com