Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
PHP-MySQLi-Database-Class
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
Kulya
PHP-MySQLi-Database-Class
Commits
a3b39286
Commit
a3b39286
authored
Jul 08, 2016
by
Jonas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added avbdr's stuff to fix stuff #1
parent
4d413e3b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
82 additions
and
107 deletions
+82
-107
MysqliDb.php
MysqliDb.php
+82
-107
No files found.
MysqliDb.php
View file @
a3b39286
...
...
@@ -393,12 +393,8 @@ class MysqliDb
* @author Jonas Barascu
* @param [[Type]] $query [[Description]]
*/
private
function
queryUnprepared
(
$query
,
$escapeQuery
=
false
)
private
function
queryUnprepared
(
$query
)
{
// Should we escape?
if
(
$escapeQuery
)
{
$query
=
$this
->
mysqli
()
->
real_escape_string
(
$query
);
}
// Execute query
$stmt
=
$this
->
mysqli
()
->
query
(
$query
);
...
...
@@ -906,76 +902,63 @@ class MysqliDb
* @param string $importSettings An Array defining the import settings as described in the README.md
* @return boolean
*/
public
function
loadData
(
$importTable
,
$importFile
,
$importSettings
=
Array
(
"fieldChar"
=>
';'
,
"lineChar"
=>
'\r\n'
,
"linesToIgnore"
=>
1
))
public
function
loadData
(
$importTable
,
$importFile
,
$importSettings
=
null
)
{
// Define default success var
$success
=
false
;
// We have to check if the file exists
if
(
file_exists
(
$importFile
))
{
// Create default values
$terminateCharField
=
';'
;
// Default is ;
$terminateCharLine
=
PHP_EOL
;
// Default \r\n or PHP_EOL (*nix is \n)
$ignoreLines
=
1
;
// Default 1
// Check the import settings
if
(
gettype
(
$importSettings
)
==
"array"
)
{
if
(
isset
(
$importSettings
[
"fieldChar"
]))
{
$terminateCharField
=
$importSettings
[
"fieldChar"
];
}
if
(
isset
(
$importSettings
[
"lineChar"
]))
{
$terminateCharLine
=
$importSettings
[
"lineChar"
];
}
if
(
isset
(
$importSettings
[
"linesToIgnore"
]))
{
$ignoreLines
=
$importSettings
[
"linesToIgnore"
];
}
}
// Add the prefix to the import table
$table
=
self
::
$prefix
.
$importTable
;
// Add 1 more slash to every slash so maria will interpret it as a path
$importFile
=
str_replace
(
"
\\
"
,
"
\\\\
"
,
$importFile
);
// Build SQL Syntax
$sqlSyntax
=
sprintf
(
'LOAD DATA INFILE \'%s\' INTO TABLE %s'
,
$importFile
,
$table
);
// FIELDS
$sqlSyntax
.=
sprintf
(
' FIELDS TERMINATED BY \'%s\''
,
$terminateCharField
);
if
(
isset
(
$importSettings
[
"fieldEnclosure"
])){
$sqlSyntax
.=
sprintf
(
' ENCLOSED BY \'%s\''
,
$importSettings
[
"fieldEnclosure"
]);
}
// LINES
$sqlSyntax
.=
sprintf
(
' LINES TERMINATED BY \'%s\''
,
$terminateCharLine
);
if
(
isset
(
$importSettings
[
"lineStarting"
])){
$sqlSyntax
.=
sprintf
(
' STARTING BY \'%s\''
,
$importSettings
[
"lineStarting"
]);
}
// IGNORE LINES
$sqlSyntax
.=
sprintf
(
' IGNORE %d LINES'
,
$ignoreLines
);
// Exceute the query unprepared because LOAD DATA only works with unprepared statements.
$result
=
$this
->
queryUnprepared
(
$sqlSyntax
);
// Are there rows modified?
if
(
$result
)
{
$success
=
true
;
}
// Something went wrong
else
{
$success
=
false
;
}
}
else
{
if
(
!
file_exists
(
$importFile
))
{
// Throw an exception
throw
new
Exception
(
"importCSV -> importFile "
.
$importFile
.
" does not exists!"
);
return
;
}
// Define the default values
// We will merge it later
$settings
=
Array
(
"fieldChar"
=>
';'
,
"lineChar"
=>
'\r\n'
,
"linesToIgnore"
=>
1
);
// Check the import settings
if
(
gettype
(
$importSettings
)
==
"array"
)
{
// Merge the default array with the custom one
$settings
=
array_merge
(
$settings
,
$importSettings
);
}
echo
(
var_dump
(
$settings
));
// Add the prefix to the import table
$table
=
self
::
$prefix
.
$importTable
;
// Add 1 more slash to every slash so maria will interpret it as a path
$importFile
=
str_replace
(
"
\\
"
,
"
\\\\
"
,
$importFile
);
// Build SQL Syntax
$sqlSyntax
=
sprintf
(
'LOAD DATA INFILE \'%s\' INTO TABLE %s'
,
$importFile
,
$table
);
// FIELDS
$sqlSyntax
.=
sprintf
(
' FIELDS TERMINATED BY \'%s\''
,
$settings
[
"fieldChar"
]);
if
(
isset
(
$settings
[
"fieldEnclosure"
]))
{
$sqlSyntax
.=
sprintf
(
' ENCLOSED BY \'%s\''
,
$settings
[
"fieldEnclosure"
]);
}
// LINES
$sqlSyntax
.=
sprintf
(
' LINES TERMINATED BY \'%s\''
,
$settings
[
"lineChar"
]);
if
(
isset
(
$settings
[
"lineStarting"
]))
{
$sqlSyntax
.=
sprintf
(
' STARTING BY \'%s\''
,
$settings
[
"lineStarting"
]);
}
// IGNORE LINES
$sqlSyntax
.=
sprintf
(
' IGNORE %d LINES'
,
$settings
[
"linesToIgnore"
]);
echo
(
$sqlSyntax
);
// Exceute the query unprepared because LOAD DATA only works with unprepared statements.
$result
=
$this
->
queryUnprepared
(
$sqlSyntax
);
// Are there rows modified?
// Let the user know if the import failed / succeeded
return
$success
;
return
(
bool
)
$result
;
}
/**
...
...
@@ -989,59 +972,51 @@ class MysqliDb
*
* @return boolean Returns true if the import succeeded, false if it failed.
*/
public
function
loadX
ML
(
$importTable
,
$importFile
,
$importSettings
=
Array
(
"linesToIgnore"
=>
0
)
)
public
function
loadX
ml
(
$importTable
,
$importFile
,
$importSettings
=
null
)
{
// Define default success var
$success
=
false
;
// We have to check if the file exists
if
(
file_exists
(
$importFile
))
{
// Create default values
$ignoreLines
=
0
;
// Default 0
// Check the import settings
if
(
gettype
(
$importSettings
)
==
"array"
)
{
if
(
isset
(
$importSettings
[
"linesToIgnore"
]))
{
$ignoreLines
=
$importSettings
[
"linesToIgnore"
];
}
}
// Add the prefix to the import table
$table
=
self
::
$prefix
.
$importTable
;
if
(
!
file_exists
(
$importFile
))
{
// Does not exists
throw
new
Exception
(
"loadXml: Import file does not exists"
);
return
;
}
// Create default values
$settings
=
Array
(
"linesToIgnore"
=>
0
);
// Add 1 more slash to every slash so maria will interpret it as a path
$importFile
=
str_replace
(
"
\\
"
,
"
\\\\
"
,
$importFile
);
// Check the import settings
if
(
gettype
(
$importSettings
)
==
"array"
)
{
$settings
=
array_merge
(
$settings
,
$importSettings
);
}
// Build SQL Syntax
$sqlSyntax
=
sprintf
(
'LOAD XML INFILE \'%s\' INTO TABLE %s'
,
// Add the prefix to the import table
$table
=
self
::
$prefix
.
$importTable
;
// Add 1 more slash to every slash so maria will interpret it as a path
$importFile
=
str_replace
(
"
\\
"
,
"
\\\\
"
,
$importFile
);
// Build SQL Syntax
$sqlSyntax
=
sprintf
(
'LOAD XML INFILE \'%s\' INTO TABLE %s'
,
$importFile
,
$table
);
// FIELDS
if
(
isset
(
$importSettings
[
"rowTag"
]))
{
$sqlSyntax
.=
sprintf
(
' ROWS IDENTIFIED BY \'%s\''
,
$importSettings
[
"rowTag"
]);
}
// IGNORE LINES
$sqlSyntax
.=
sprintf
(
' IGNORE %d LINES'
,
$ignoreLines
);
// Exceute the query unprepared because LOAD XML only works with unprepared statements.
$result
=
$this
->
queryUnprepared
(
$sqlSyntax
);
// Are there rows modified?
if
(
$result
)
{
$success
=
true
;
}
// Something went wrong
else
{
$success
=
false
;
}
}
else
{
// Throw an exception
throw
new
Exception
(
"importXML -> importFile "
.
$importFile
.
" does not exists!"
);
// FIELDS
if
(
isset
(
$settings
[
"rowTag"
]))
{
$sqlSyntax
.=
sprintf
(
' ROWS IDENTIFIED BY \'%s\''
,
$settings
[
"rowTag"
]);
}
// IGNORE LINES
$sqlSyntax
.=
sprintf
(
' IGNORE %d LINES'
,
$settings
[
"linesToIgnore"
]);
// Exceute the query unprepared because LOAD XML only works with unprepared statements.
$result
=
$this
->
queryUnprepared
(
$sqlSyntax
);
// Are there rows modified?
// Let the user know if the import failed / succeeded
return
$success
;
return
(
bool
)
$result
;
}
/**
...
...
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