mysql_ functions have been deprecated. MySQLi is the updated driver, but it's a pain to work with. I find working with PDO much better than working with MySQLi, but you'd do even better implementing an ORM unless your app is exceedingly simple.
Consider straight PDO
Code: Select all
$query = "SELECT * FROM products";
$stmt = $this->pdo->prepare($query);
$exec = $stmt->execute();
$products = $exec ? $stmt->fetchAll() : [];
vs. Eloquent
or for a more complex query:
Code: Select all
$query = "SELECT c.id AS catid, c.name AS catname, s.id AS subcatid, s.name AS subcatname
FROM categories AS c
INNER JOIN subcategories AS s ON c.id = s.category_id";
$stmt = $this->pdo->prepare($query);
$exec = $stmt->execute();
$categories = [];
if ($exec) {
$results = $stmt->fetchAll();
foreach ($results as $result) {
if (!array_key_exists($result->catid, $categories)) {
$categories[$result->catid] = [
'name' => $result->catname,
'subcategories' => [
[
'id' => $result->subcatid,
'name' => $result->subcatname,
],
],
];
} else {
$categories[$result->catid]['subcategories'][] = [
'id' => $result->subcatid,
'name' => $result->subcatname,
];
}
}
}
vs.
Code: Select all
$categories = Category::with('subcategories')->get();