class Database {
public $db;
public function __construct ($dbname, $user = "root", $password = "", $host = "localhost", $type = "mysql", $charset = "utf8", $options = array())
{
$default_options = [
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
];
$options = array_replace($default_options, $options);
try {
$this->db = new PDO("{$type}:host={$host};dbname={$dbname};charset={$charset};", $user, $password, $options);
} catch (PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
}
public function disconnect () {
$this->db = NULL;
}
public function query ($query, $params = array()) {
$stmt = $this->db->prepare($query);
$stmt->execute($params);
return $stmt;
}
public function getRow ($query, $params = array(), $fetchMode = PDO::PDO_FETCH_ASSOC) {
return $this->query->fetch($fetchMode);
}
public function getRows ($query, $params = array(), $fetchMode = PDO::PDO_FETCH_ASSOC) {
return $this->query->fetchAll($fetchMode);
}
}
выбирать все строки, сортировать и выбирать с самым маленьким ID - это как-то трудоёмко)
$array = [1,2,3];
$in = str_repeat('?,', count($array) - 1) . '?';
$sql = "SELECT * FROM `tovar` WHERE `id` = IN ($in)";
$stmt = $mysqli->prepare($sql);
$types = str_repeat('s', count($array));
$stmt->bind_param($types, ...$array);
$Stmt->execute();
$result = $stmt->get_result(); // get the mysqli result
$data = $result->fetch_all(MYSQLI_ASSOC);
$array = [1,2,3];
$in = str_repeat('?,', count($array) - 1) . '?';
$sql = "SELECT * FROM `tovar` WHERE `id` = IN ($in)";
$stmt = $pdo->prepare($sql);
$stmt->execute($array);
$data = $stmt->fetchAll(PDO::FETCH_UNIQUE);
- Ты где деньги берешь?
- В тумбочке.
- А откуда они там?
- Жена кладет.
- А у нее откуда?
- Я даю.
- Ну а ты-то где их берешь?
- Да в тумбочке!
function fakePayout ($mysqli, $user, $amount, $time) {
function fakePayout ($mysqli, $user, $amount, $time) {
$referer = 'P'.rand(4, 6).rand(1, 0).rand(000000, 999999);
$refererIncome = $amount/100*150;
$stmt = $mysqli->prepare("INSERT INTO `deposits` (`user`, `amount`, `time`, `type`, `fake`) VALUES (?,?,?, '1', '1')");
$stmt->bind_param("sss", $user, $amount, $time);
$stmt->execute();
$stmt = $mysqli->prepare("INSERT INTO `deposits` (`user`, `amount`, `time`, `type`, `fake`) VALUES (?,?,?, '2', '1')");
$stmt->bind_param("sss", $referer, $refererIncome, $time);
$stmt->execute();
}
include_once(dirname(__FILE__).'/engine/что-нибудь/mysqli.php');
fakePayout ($mysqli,$user, $amount, $time);