���� JFIF �� � ( %"1"%)+...383,7(-.-
![]() Server : Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.20 System : Linux st2.domain.com 3.10.0-1127.10.1.el7.x86_64 #1 SMP Wed Jun 3 14:28:03 UTC 2020 x86_64 User : apache ( 48) PHP Version : 7.4.20 Disable Function : NONE Directory : /var/www/html/api-truyentranh/includes/ |
<?php class db_driver { var $obj = array ( "sql_database" => "", "sql_user" => "", "sql_pass" => "", "sql_host" => "", "sql_port" => "", "sql_tbl_prefix" => "", 'debug' => 0 ); var $query_id = ""; var $connection_id = ""; var $record_row = array(); var $return_die = 0; var $error = ""; var $mysql_version = ""; var $failed = 0; // Connect to the database /*========================================================================*/ function connect() { global $INFO; $this->obj['sql_database'] = $INFO['sql_database']; $this->obj['sql_user'] = $INFO['sql_user']; $this->obj['sql_pass'] = $INFO['sql_pass']; $this->obj['sql_host'] = $INFO['sql_host']; $this->obj['sql_tbl_prefix'] = $INFO['sql_tbl_prefix']; $this->connection_id = mysqli_connect($this->obj['sql_host'] ,$this->obj['sql_user'] ,$this->obj['sql_pass'], $this->obj['sql_database']); if (mysqli_connect_errno()) { echo "Failed to connect to Database: " . mysqli_connect_error(); exit(); } mysqli_set_charset($this->connection_id,"utf8mb4"); } // Process a query /*========================================================================*/ function query($the_query) { $this->query_id = mysqli_query($this->connection_id,$the_query); if (!$this->query_id) { echo("Error description: " . mysqli_error($this->connection_id)); exit(); } return $this->query_id; } // Fetch a row based on the last query /*========================================================================*/ function fetch_row($query_id = "") { if ($query_id == ""){ $query_id = $this->query_id; } $this->record_row = mysqli_fetch_array($query_id, MYSQLI_ASSOC); return $this->record_row; } /*========================================================================*/ function get_num_rows() { $res = mysqli_num_rows($this->query_id); if($res>0){ return $res; }else{ return 0; } } function do_insert($table,$insData){ $columns = "`".implode("`,`", array_keys($insData))."`"; $escaped_values = array_map(array($this, 'real_escape_string'), array_values($insData)); foreach ( $escaped_values as $idx => $data ) { $escaped_values[$idx] = "'".$data."'"; } $values = implode(", ", $escaped_values); if($this->query("INSERT INTO $table ($columns) VALUES ($values);")){ $insert_id = mysqli_insert_id($this->connection_id); return $insert_id; }else{ echo mysqli_error($this->connection_id); exit(); } } function real_escape_string($str){ return mysqli_real_escape_string($this->connection_id,$str); } function insert_id(){ $insert_id = mysqli_insert_id($this->connection_id); return $insert_id; } function do_update($table, $yourarray, $where) { $cols = array_keys($yourarray); $escaped_values = array_map(array($this, 'real_escape_string'), array_values($yourarray)); $set = ''; $x = 1; foreach ( $cols as $key => $val ) { $set .= "`{$val}`='{$escaped_values[$key]}'"; if ( $x < count($cols) ) { $set .= ','; } $x++; } $query = "UPDATE {$table} SET {$set} $where"; if($this->query($query)){ return $where; }else{ echo mysqli_error($this->connection_id); exit(); } } function get_colum_tb($table){ $arr = array(); $q_colum = $this->query("SHOW COLUMNS FROM ".$table); while ($r_colum = $this->fetch_row($q_colum)){ $arr[$r_colum['Field']] = ''; } return $arr; } function affected_rows() { $affected_rows = mysqli_affected_rows($this->connection_id); if($affected_rows>0) { return $affected_rows; }else{ return false; } } function do_insert_sec($tableName,$data) { if (empty($data)) { return false; } $columns = implode(", ", array_keys($data)); $placeholders = implode(", ", array_fill(0, count($data), "?")); $sql = "INSERT INTO " . $tableName . " (" . $columns . ") VALUES (" . $placeholders . ")"; // Chuẩn bị câu lệnh $stmt = mysqli_prepare($this->connection_id, $sql); if ($stmt === false) { error_log("Lỗi chuẩn bị câu lệnh SQL: " . mysqli_error($this->connection_id)); return false; } /*$types = str_repeat("s", count($data)); $values = array_values($data);*/ $types = ''; $values = []; foreach ($data as $value) { $types .= $this->getParamType($value); // Xây dựng chuỗi kiểu $values[] = $value; // Thu thập các giá trị } // Gắn các tham số $bindParams = array($stmt, $types); foreach ($values as $key => $value) { $bindParams[] = &$values[$key]; // Thêm tham chiếu } // Dùng call_user_func_array để gọi mysqli_stmt_bind_param với mảng động call_user_func_array('mysqli_stmt_bind_param', $bindParams); // Thực thi câu lệnh if (mysqli_stmt_execute($stmt)) { $lastId = mysqli_stmt_insert_id($stmt); mysqli_stmt_close($stmt); return $lastId; } else { error_log("Lỗi thực thi câu lệnh: " . mysqli_stmt_error($stmt)); mysqli_stmt_close($stmt); return false; } } function comit($arr_sql) { mysqli_autocommit($this->connection_id, FALSE); try { // Bắt đầu giao dịch mysqli_begin_transaction($this->connection_id); // thuc thi sql foreach ($arr_sql as $sql) { if (!mysqli_query($this->connection_id, $sql)) { throw new Exception(mysqli_error($this->connection_id)); } } // Nếu tất cả các lệnh đều thành công, commit giao dịch mysqli_commit($this->connection_id); } catch (Exception $e) { // Nếu có bất kỳ lỗi nào, rollback giao dịch mysqli_rollback($this->connection_id); } finally { mysqli_autocommit($this->connection_id, TRUE); } } function get_version(){ return mysqli_get_server_info($this->connection_id); } function free_result() { if($this->query_id and is_object($this->query_id)) { mysqli_free_result($this->query_id); } } function close_db() { return mysqli_close($this->connection_id); } /** * Hàm hỗ trợ để xác định kiểu dữ liệu của biến cho mysqli_stmt_bind_param. * * @param mixed $value Giá trị cần kiểm tra. * @return string Ký tự kiểu dữ liệu ('s', 'i', 'd', 'b'). */ function getParamType($value): string { if (is_int($value)) { return 'i'; // integer } elseif (is_float($value)) { return 'd'; // double (float) } elseif (is_bool($value)) { return 'i'; // boolean thường được lưu dưới dạng 0 hoặc 1 trong DB } // Đối với BLOB (binary data), bạn cần một logic riêng hoặc xử lý đặc biệt. // Mặc định cho tất cả các loại khác là string. return 's'; // string } function record_exists(string $tableName, array $conditions): bool { if (empty($conditions)) { // Nếu không có điều kiện, coi như không kiểm tra sự tồn tại cụ thể return false; } $where_clauses = []; $types = ''; $values = []; // Xây dựng mệnh đề WHERE và thu thập kiểu, giá trị foreach ($conditions as $column => $value) { $where_clauses[] = "`" . $column . "` = ?"; $types .= $this->getParamType($value); $values[] = $value; } $where_sql = implode(" AND ", $where_clauses); // Xây dựng câu lệnh SQL SELECT 1 $sql = "SELECT 1 FROM `" . $tableName . "` WHERE " . $where_sql . " LIMIT 1"; // Chuẩn bị câu lệnh $stmt = mysqli_prepare($this->connection_id, $sql); if ($stmt === false) { error_log("Lỗi chuẩn bị câu lệnh SQL: " . mysqli_error($this->connection_id)); return false; } // Gắn các tham số // `mysqli_stmt_bind_param` yêu cầu các tham số được truyền dưới dạng tham chiếu. // Dùng `call_user_func_array` để xử lý mảng động. $bind_params = [$stmt, $types]; foreach ($values as $key => $value) { $bind_params[] = &$values[$key]; // Thêm tham chiếu đến các giá trị } call_user_func_array('mysqli_stmt_bind_param', $bind_params); // Thực thi câu lệnh if (mysqli_stmt_execute($stmt) === false) { error_log("Lỗi thực thi câu lệnh: " . mysqli_stmt_error($stmt)); mysqli_stmt_close($stmt); return false; } // Lấy kết quả $result = mysqli_stmt_get_result($stmt); $exists = (mysqli_num_rows($result) > 0); // Kiểm tra số hàng trả về // Đóng statement và giải phóng kết quả /*mysqli_free_result($result); mysqli_stmt_close($stmt);*/ return $exists; } } ?>