Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
Clubphp
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Clubphp
Commits
5157eafb
Commit
5157eafb
authored
Apr 07, 2026
by
Administrator
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update 1 files via Son of Anton
parent
d25685f8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
175 additions
and
15 deletions
+175
-15
Database.php
app/Core/Database.php
+175
-15
No files found.
app/Core/Database.php
View file @
5157eafb
// Make sure your Database constructor stores dbName.
// Find and verify/replace the constructor:
<?php
declare
(
strict_types
=
1
);
namespace
App\Core
;
final
class
Database
{
private
string
$dbName
;
private
?
\PDO
$pdo
=
null
;
private
array
$config
;
private
string
$host
;
private
int
$port
;
private
string
$user
;
private
string
$pass
;
private
string
$charset
;
public
function
__construct
(
array
$config
)
{
$this
->
config
=
$config
;
$this
->
dbName
=
$config
[
'name'
]
??
''
;
private
array
$onAfterInsertCallbacks
=
[];
private
array
$onBeforeUpdateCallbacks
=
[];
private
array
$onAfterDeleteCallbacks
=
[];
public
function
__construct
(
string
$host
=
'127.0.0.1'
,
int
$port
=
3306
,
string
$name
=
''
,
string
$user
=
'root'
,
string
$pass
=
''
,
string
$charset
=
'utf8mb4'
)
{
$this
->
host
=
$host
;
$this
->
port
=
$port
;
$this
->
dbName
=
$name
;
$this
->
user
=
$user
;
$this
->
pass
=
$pass
;
$this
->
charset
=
$charset
;
}
private
function
connect
()
:
\PDO
{
if
(
$this
->
pdo
===
null
)
{
$dsn
=
sprintf
(
'mysql:host=%s;port=%
s
;dbname=%s;charset=%s'
,
$this
->
config
[
'host'
]
??
'127.0.0.1'
,
$this
->
config
[
'port'
]
??
'3306'
,
$this
->
config
[
'name'
]
??
''
,
$this
->
c
onfig
[
'charset'
]
??
'utf8mb4'
'mysql:host=%s;port=%
d
;dbname=%s;charset=%s'
,
$this
->
host
,
$this
->
port
,
$this
->
dbName
,
$this
->
c
harset
);
$this
->
pdo
=
new
\PDO
(
$dsn
,
$this
->
config
[
'user'
]
??
'root'
,
$this
->
config
[
'pass'
]
??
''
,
[
$this
->
pdo
=
new
\PDO
(
$dsn
,
$this
->
user
,
$this
->
pass
,
[
\PDO
::
ATTR_ERRMODE
=>
\PDO
::
ERRMODE_EXCEPTION
,
\PDO
::
ATTR_DEFAULT_FETCH_MODE
=>
\PDO
::
FETCH_ASSOC
,
\PDO
::
ATTR_EMULATE_PREPARES
=>
false
,
\PDO
::
MYSQL_ATTR_INIT_COMMAND
=>
"SET NAMES '
utf8mb4' COLLATE 'utf8mb4
_unicode_ci'"
,
\PDO
::
MYSQL_ATTR_INIT_COMMAND
=>
"SET NAMES '
{
$this
->
charset
}
' COLLATE '
{
$this
->
charset
}
_unicode_ci'"
,
]);
}
return
$this
->
pdo
;
}
\ No newline at end of file
}
public
function
query
(
string
$sql
,
array
$params
=
[])
:
\PDOStatement
{
$pdo
=
$this
->
connect
();
$stmt
=
$pdo
->
prepare
(
$sql
);
$stmt
->
execute
(
$params
);
return
$stmt
;
}
public
function
select
(
string
$sql
,
array
$params
=
[])
:
array
{
return
$this
->
query
(
$sql
,
$params
)
->
fetchAll
();
}
public
function
selectOne
(
string
$sql
,
array
$params
=
[])
:
?
array
{
$result
=
$this
->
query
(
$sql
,
$params
)
->
fetch
();
return
$result
?:
null
;
}
public
function
insert
(
string
$table
,
array
$data
)
:
int
{
$columns
=
implode
(
', '
,
array_map
(
fn
(
$c
)
=>
"`
{
$c
}
`"
,
array_keys
(
$data
)));
$placeholders
=
implode
(
', '
,
array_fill
(
0
,
count
(
$data
),
'?'
));
$sql
=
"INSERT INTO `
{
$table
}
` (
{
$columns
}
) VALUES (
{
$placeholders
}
)"
;
$this
->
query
(
$sql
,
array_values
(
$data
));
$id
=
(
int
)
$this
->
connect
()
->
lastInsertId
();
foreach
(
$this
->
onAfterInsertCallbacks
as
$cb
)
{
try
{
$cb
(
$table
,
$data
,
$id
);
}
catch
(
\Throwable
$e
)
{
// Don't let callback errors break inserts
}
}
return
$id
;
}
public
function
update
(
string
$table
,
array
$data
,
string
$where
,
array
$whereParams
=
[])
:
int
{
foreach
(
$this
->
onBeforeUpdateCallbacks
as
$cb
)
{
try
{
$cb
(
$table
,
$data
,
null
);
}
catch
(
\Throwable
$e
)
{
// Don't let callback errors break updates
}
}
$set
=
implode
(
', '
,
array_map
(
fn
(
$c
)
=>
"`
{
$c
}
` = ?"
,
array_keys
(
$data
)));
$sql
=
"UPDATE `
{
$table
}
` SET
{
$set
}
WHERE
{
$where
}
"
;
$params
=
array_merge
(
array_values
(
$data
),
$whereParams
);
$stmt
=
$this
->
query
(
$sql
,
$params
);
return
$stmt
->
rowCount
();
}
public
function
delete
(
string
$table
,
string
$where
,
array
$whereParams
=
[])
:
int
{
$sql
=
"DELETE FROM `
{
$table
}
` WHERE
{
$where
}
"
;
$stmt
=
$this
->
query
(
$sql
,
$whereParams
);
$count
=
$stmt
->
rowCount
();
foreach
(
$this
->
onAfterDeleteCallbacks
as
$cb
)
{
try
{
$cb
(
$table
,
[],
null
);
}
catch
(
\Throwable
$e
)
{
// Don't let callback errors break deletes
}
}
return
$count
;
}
public
function
raw
(
string
$sql
)
:
void
{
$this
->
connect
()
->
exec
(
$sql
);
}
public
function
beginTransaction
()
:
void
{
$this
->
connect
()
->
beginTransaction
();
}
public
function
commit
()
:
void
{
$this
->
connect
()
->
commit
();
}
public
function
rollBack
()
:
void
{
$this
->
connect
()
->
rollBack
();
}
public
function
inTransaction
()
:
bool
{
return
$this
->
connect
()
->
inTransaction
();
}
public
function
lastInsertId
()
:
int
{
return
(
int
)
$this
->
connect
()
->
lastInsertId
();
}
public
function
tableExists
(
string
$table
)
:
bool
{
try
{
$result
=
$this
->
selectOne
(
"SELECT COUNT(*) as cnt FROM information_schema.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?"
,
[
$this
->
dbName
,
$table
]
);
return
(
int
)
(
$result
[
'cnt'
]
??
0
)
>
0
;
}
catch
(
\Throwable
$e
)
{
return
false
;
}
}
public
function
getDbName
()
:
string
{
return
$this
->
dbName
;
}
public
function
onAfterInsert
(
callable
$fn
)
:
void
{
$this
->
onAfterInsertCallbacks
[]
=
$fn
;
}
public
function
onBeforeUpdate
(
callable
$fn
)
:
void
{
$this
->
onBeforeUpdateCallbacks
[]
=
$fn
;
}
public
function
onAfterDelete
(
callable
$fn
)
:
void
{
$this
->
onAfterDeleteCallbacks
[]
=
$fn
;
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment