Change PDO to mysqli [duplicate]

This question already has an answer here:

I’m learning to develop wordpress plugin and I have a PDO script that i need to convert to mysqli
I CONVERTED THE PDO(“mysql:dbname=$db_name;host=$db_host”,”$db_user”,”$db_password”)
BUT THE fetchAll is what i have an issue with

<?php

/*
I COVERTED THIS BIT
$db = new 
PDO("mysql:dbname=$db_name;host=$db_host","$db_user","$db_password");
*/


require_once(ABSPATH . 'wp-config.php');
$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysqli_select_db($connection, DB_NAME);

/*
BUT THE REST OF THE SCRIPT THAT SAYS
return $get->fetchAll(PDO::FETCH_ASSOC);
IS WHAT I HAVE AN ISSUE WITH
*/

function categoryExists($db = '', $category = '') {
$response = false;
$get = $db->query("SELECT name FROM category WHERE name = '$category'");
if ($get->rowCount() > 0) {
$response = true;
}
return $response;
}

function saveCategory($db = '', $name='') {
$db->query("INSERT INTO category (name) VALUES ('$name')");
return $db->lastInsertId();
}

function saveCategoryProduct($db = '', $name = '', $price = '', $category = 
'') {
$db->query("INSERT INTO products (name, price, category) VALUES ('$name', 
'$price', '$category')");
}

function getCategoryAndProducts($db = '') {
$get = $db->query("SELECT category.name as catName, category.*, products.* FROM category INNER JOIN products ON category.name = products.category WHERE products.category = category.name GROUP BY catName");
return $get->fetchAll(PDO::FETCH_ASSOC);
}

function getCategories($db = '') {
$get = $db->query("SELECT * FROM category");
return $get->fetchAll(PDO::FETCH_ASSOC);
}

function getProducts($db = '', $category = '') {
$get = $db->query("SELECT * FROM products WHERE category = '$category'");
return $get->fetchAll(PDO::FETCH_ASSOC);
}

?>

Read more here: Change PDO to mysqli [duplicate]

Leave a Reply

Your email address will not be published. Required fields are marked *