0

I am getting this error:

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead.

I haven't used PDO still now.

I appreciate any help on how to migrate my MySQL to PDO.

My PHP Code is as followed below:

//Set the database connection
mysql_connect('localhost','root','chethan');
mysql_select_db('menu');
//select all rows from the main_menu table
$result = mysql_query("select id,title,parentid,link from main_menu");

//create a multidimensional array to hold a list of menu and parent menu
$menu = array(
    'menus' => array(),
    'parent_menus' => array()
);

//build the array lists with data from the menu table
while ($row = mysql_fetch_assoc($result)) {
    //creates entry into menus array with current menu id ie. $menus['menus'][1]
    $menu['menus'][$row['id']] = $row;
    //creates entry into parent_menus array. parent_menus array contains a list of all menus with children
    $menu['parent_menus'][$row['parentid']][] = $row['id'];
}

// Create the main function to build milti-level menu. It is a recursive function.  
function buildMenu($parent, $menu) {
$html = "";
if (isset($menu['parent_menus'][$parent])) {
    $html .= "<ul>";
    foreach ($menu['parent_menus'][$parent] as $menu_id) {
        if (!isset($menu['parent_menus'][$menu_id])) {
            $html .= "<li><a href='" . $menu['menus'][$menu_id]['link'] . "'>" . $menu['menus'][$menu_id]['title'] . "</a></li>";
        }
        if (isset($menu['parent_menus'][$menu_id])) {
            $html .= "<li><a href='" . $menu['menus'][$menu_id]['link'] . "'>" . $menu['menus'][$menu_id]['title'] . "</a>";
            $html .= buildMenu($menu_id, $menu);
            $html .= "</li>";
        }
    }
    $html .= "</ul>";
}
return $html;

}

mnille
  • 1,328
  • 4
  • 16
  • 20
  • 2
    Welcome to Stack Overflow. Unfortunately, this is not a free coding service. A coding request is inappropriate here. We can provide you with a link to a [PDO tutorial](https://phpdelusions.net/pdo) which you are supposed to read and then translate your code yourself. You are welcome to ask any practical questions you encounter in the process though. – Your Common Sense Jul 11 '16 at 07:14
  • you need to upgrade your code. U have to use mysqli or pdo you can read this [article](https://www.sitepoint.com/migrate-from-the-mysql-extension-to-pdo/) – Rafael Shkembi Jul 11 '16 at 07:14
  • Warning mysql_query, mysql_fetch_array,mysql_connect etc.. extensions were deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. – JYoThI Jul 11 '16 at 07:15

0 Answers0