Pure PDO sql

This commit is contained in:
Alexander Yakovlev 2017-05-04 13:59:49 +07:00
parent 46b95b0780
commit 67039f2b20
4 changed files with 19 additions and 88 deletions

View file

@ -7,9 +7,9 @@
* @param boolean $excluse_invisible Shall we also get invisible threads?
* @return array
*/
function getLatestActiveThreads($forum_id = 0, $limit = 7, $exclude_invisible = true)
function getLatestActiveThreads($limit = 7, $exclude_invisible = true)
{
global $pdo, $prefix;
global $pdo;
// This will be the array, where we can save the threads
$threads = array();
@ -20,21 +20,19 @@ function getLatestActiveThreads($forum_id = 0, $limit = 7, $exclude_invisible =
* We only fetch visible threads, if there is anything we want to hide ;)
* However we can also define that we want the invisible threads as well
*/
$fetch_invisible_threads = ($exclude_invisible == true) ? '1' : '0';
$condition = [];
$condition['visible'] = $fetch_invisible_threads;
if ($forum_id != 0){
$condition['fid'] = intval($forum_id);
}
$fetch_invisible_threads = intval($exclude_invisible);
// Run the Query
$query = $pdo->select()
->from(MYBB_PREFIX.'threads')
->leftJoin(MYBB_PREFIX.'forums', MYBB_PREFIX.'forums.fid', '=', MYBB_PREFIX.'threads.fid')
->whereMany($condition, '=')
->orderBy(MYBB_PREFIX.'threads.lastpost', 'DESC')
->limit(intval($limit), 0)
->execute();
$query = "SELECT `lastpost` FROM `".MYBB_PREFIX."threads`
LEFT JOIN `".MYBB_PREFIX."forums` ON `".MYBB_PREFIX.'forums`.`fid` = `'.MYBB_PREFIX.'threads`.`fid`'."
WHERE `".MYBB_PREFIX."threads`.`visible` = :visible
ORDER BY `".MYBB_PREFIX."threads`.`lastpost` DESC
LIMIT :limit";
$query = $pdo->prepare($query);
$query->execute([
'visible' => $fetch_invisible_threads,
'limit' => intval($limit)
]);
return $query->fetchAll();
}

View file

@ -1,6 +1,5 @@
{
"minimum-stability": "dev",
"require": {
"slim/pdo": "^1.10"
}
}

65
composer.lock generated
View file

@ -1,65 +0,0 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "d1c6c4a84dd7d10aa418cd379c810ed7",
"content-hash": "1be1f0d88c406a5544cb07add016bdd4",
"packages": [
{
"name": "slim/pdo",
"version": "1.10.0",
"source": {
"type": "git",
"url": "https://github.com/FaaPz/Slim-PDO.git",
"reference": "9424e6d5f3737a71722ab1f868c1d3090b63287d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/FaaPz/Slim-PDO/zipball/9424e6d5f3737a71722ab1f868c1d3090b63287d",
"reference": "9424e6d5f3737a71722ab1f868c1d3090b63287d",
"shasum": ""
},
"require": {
"ext-pdo": "*",
"php": ">=5.3.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Slim\\PDO\\": "src/PDO/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabian de Laender",
"email": "fabian@faapz.nl",
"homepage": "http://faapz.nl",
"role": "Developer"
}
],
"description": "PDO database library for Slim Framework",
"homepage": "https://github.com/FaaPz/Slim-PDO",
"keywords": [
"database",
"framework",
"pdo",
"slim"
],
"time": "2016-08-14 11:42:49"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "dev",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": []
}

View file

@ -9,14 +9,13 @@ $dsn = 'mysql:host=localhost;dbname=mybb;charset=utf8';
$usr = 'root';
$pwd = '123456';
$pdo = new \Slim\PDO\Database($dsn, $usr, $pwd);
$pdo = new PDO($dsn, $usr, $pwd);
$threads = getLatestActiveThreads(FORUM_ID, 100, true);
$users = $pdo->select()
->from(MYBB_PREFIX.'users')
->where('usergroup', '=', 4)
->where('isSubscribed', '=', 1)
->execute()
->fetchAll();
$users = $pdo->prepare("SELECT email, username FROM ".MYBB_PREFIX."users
WHERE usergroup = 4
AND isSubscribed = 1");
$users->execute();
$users = $users->fetchAll();
foreach ($users as $user) {
$email = $user['email'];
print_email($user['username'], '/unsubscribe', $threads);