Documentation

database_connect.php

\file \brief Connects to the database and check its state.

PHP version 8.1

Tags
category

Database

author

HugoFara hugo.farajallah@protonmail.com

license

Unlicense http://unlicense.org/

link
https://hugofara.github.io/lwt/docs/php/files/inc-database-connect.html

Table of Contents

Functions

do_mysqli_query()  : mysqli_result|true
Do a SQL query to the database.
runsql()  : string
Run a SQL query, you can specify its behavior and error message.
get_first_value()  : float|int|string|null
Return the record "value" in the first line of the database if found.
prepare_textdata()  : string
Replace Windows line return ("\r\n") by Linux ones ("\n").
prepare_textdata_js()  : string
convert_string_to_sqlsyntax()  : string
Prepares a string to be properly recognized as a string by SQL.
convert_string_to_sqlsyntax_nonull()  : string
Prepares a string to be properly recognized as a string by SQL.
convert_string_to_sqlsyntax_notrim_nonull()  : string
Prepares a string to be properly recognized as a string by SQL.
convert_regexp_to_sqlsyntax()  : string
validateLang()  : string
Validate a language ID
validateText()  : string
Validate a text ID
validateTag()  : mixed
validateArchTextTag()  : mixed
validateTextTag()  : mixed
getSettingZeroOrOne()  : int
Convert a setting to 0 or 1
getSetting()  : string
Get a setting from the database. It can also check for its validity.
getSettingWithDefault()  : string
Get the settings value for a specific key. Return a default value when possible
saveSetting()  : string
Save the setting identified by a key with a specific value.
LWTTableCheck()  : void
Check if the _lwtgeneral table exists, create it if not.
LWTTableSet()  : void
LWTTableGet()  : string
adjust_autoincr()  : void
Adjust the auto-incrementation in the database.
optimizedb()  : void
Optimize the database.
update_japanese_word_count()  : void
Update the word count for Japanese language (using MeCab only).
init_word_count()  : void
Initiate the number of words in terms for all languages.
set_word_count()  : void
Initiate the number of words in terms for all languages
parse_japanese_text()  : null|array<string|int, string>
Parse a Japanese text using MeCab and add it to the database.
save_processed_text_with_sql()  : void
Insert a processed text in the data in pure SQL way.
parse_standard_text()  : null|array<string|int, string>
Parse a text using the default tools. It is a not-japanese text.
prepare_text_parsing()  : null|array<string|int, string>
Pre-parse the input text before a definitive parsing by a specialized parser.
check_text_valid()  : void
Echo the sentences in a text. Prepare JS data for words and word count.
registerSentencesTextItems()  : void
Append sentences and text items in the database.
update_default_values()  : void
Append sentences and text items in the database.
displayTextStatistics()  : void
Display statistics about a text.
check_text()  : void
Check a text and display statistics about it.
checkExpressions()  : void
Check a language that contains expressions.
check_text_with_expressions()  : string
Check a language that contains expressions.
splitCheckText()  : null|array<string|int, string>
Parse the input text.
reparse_all_texts()  : void
Reparse all texts in order.
update_database()  : void
Update the database if it is using an outdate version.
prefixSQLQuery()  : mixed
Add a prefix to table in a SQL query string.
check_update_db()  : void
Check and/or update the database.
connect_to_database()  : mysqli
Make the connection to the database.
getDatabasePrefix()  : array<string|int, mixed>
Get the prefixes for the database.
get_database_prefixes()  : 0|1
Get the prefixes for the database.

Functions

do_mysqli_query()

Do a SQL query to the database.

do_mysqli_query(string $sql) : mysqli_result|true

It is a wrapper for mysqli_query function.

Parameters
$sql : string

Query using SQL syntax

Tags
global

mysqli $DBCONNECTION Connection to the database

Return values
mysqli_result|true

runsql()

Run a SQL query, you can specify its behavior and error message.

runsql(string $sql, string $m[, bool $sqlerrdie = true ]) : string
Parameters
$sql : string

MySQL query

$m : string

Success phrase to prepend to the number of affected rows

$sqlerrdie : bool = true

To die on errors (default = TRUE)

Return values
string

Error message if failure, or the number of affected rows

get_first_value()

Return the record "value" in the first line of the database if found.

get_first_value(string $sql) : float|int|string|null
Parameters
$sql : string

MySQL query

Tags
since
2.6.0-fork

Officially return numeric types.

Return values
float|int|string|null

Any returned value from the database.

prepare_textdata()

Replace Windows line return ("\r\n") by Linux ones ("\n").

prepare_textdata(string $s) : string
Parameters
$s : string

Input string

Return values
string

Adapted string.

prepare_textdata_js()

prepare_textdata_js(mixed $s) : string
Parameters
$s : mixed
Return values
string

convert_string_to_sqlsyntax()

Prepares a string to be properly recognized as a string by SQL.

convert_string_to_sqlsyntax(string $data) : string
Parameters
$data : string

Input string

Tags
global

$DBDONNECTION

Return values
string

Properly escaped and trimmed string. "NULL" if the input string is empty.

convert_string_to_sqlsyntax_nonull()

Prepares a string to be properly recognized as a string by SQL.

convert_string_to_sqlsyntax_nonull(string $data) : string
Parameters
$data : string

Input string

Return values
string

Properly escaped and trimmed string

convert_string_to_sqlsyntax_notrim_nonull()

Prepares a string to be properly recognized as a string by SQL.

convert_string_to_sqlsyntax_notrim_nonull(string $data) : string
Parameters
$data : string

Input string

Return values
string

Properly escaped string

convert_regexp_to_sqlsyntax()

convert_regexp_to_sqlsyntax(mixed $input) : string
Parameters
$input : mixed
Return values
string

validateLang()

Validate a language ID

validateLang(string $currentlang) : string
Parameters
$currentlang : string

Language ID to validate

Tags
global

string $tbpref Table name prefix

Return values
string

'' if the language is not valid, $currentlang otherwise

validateText()

Validate a text ID

validateText(string $currenttext) : string
Parameters
$currenttext : string

Text ID to validate

Tags
global

string '' if the text is not valid, $currenttext otherwise

global

string $tbpref Table name prefix

Return values
string

validateTag()

validateTag(mixed $currenttag, mixed $currentlang) : mixed
Parameters
$currenttag : mixed
$currentlang : mixed

validateArchTextTag()

validateArchTextTag(mixed $currenttag, mixed $currentlang) : mixed
Parameters
$currenttag : mixed
$currentlang : mixed

validateTextTag()

validateTextTag(mixed $currenttag, mixed $currentlang) : mixed
Parameters
$currenttag : mixed
$currentlang : mixed

getSettingZeroOrOne()

Convert a setting to 0 or 1

getSettingZeroOrOne(string $key, string|int $dft) : int
Parameters
$key : string

The input value

$dft : string|int

Default value to use, should be convertible to string

Tags
psalm-return

0|1

Return values
int

getSetting()

Get a setting from the database. It can also check for its validity.

getSetting(string $key) : string
Parameters
$key : string

Setting key. If $key is 'currentlanguage' or 'currenttext', we validate language/text.

Tags
global

string $tbpref Table name prefix

Return values
string

$val Value in the database if found, or an empty string

getSettingWithDefault()

Get the settings value for a specific key. Return a default value when possible

getSettingWithDefault(string $key) : string
Parameters
$key : string

Settings key

Tags
global

string $tbpref Table name prefix

Return values
string

Requested setting, or default value, or ''

saveSetting()

Save the setting identified by a key with a specific value.

saveSetting(string $k, mixed $v) : string
Parameters
$k : string

Setting key

$v : mixed

Setting value, will get converted to string

Tags
global

string $tbpref Table name prefix

since
2.9.0

Success message starts by "OK: "

Return values
string

Success message (starts by "OK: "), or error message

LWTTableCheck()

Check if the _lwtgeneral table exists, create it if not.

LWTTableCheck() : void

LWTTableSet()

LWTTableSet(mixed $key, mixed $val) : void
Parameters
$key : mixed
$val : mixed

LWTTableGet()

LWTTableGet(mixed $key) : string
Parameters
$key : mixed
Return values
string

adjust_autoincr()

Adjust the auto-incrementation in the database.

adjust_autoincr(mixed $table, mixed $key) : void
Parameters
$table : mixed
$key : mixed
Tags
global

string $tbpref Database table prefix

optimizedb()

Optimize the database.

optimizedb() : void
Tags
global

string $trbpref Table prefix

update_japanese_word_count()

Update the word count for Japanese language (using MeCab only).

update_japanese_word_count(int $japid) : void
Parameters
$japid : int

Japanese language ID

Tags
global

string $tbpref Database table prefix.

init_word_count()

Initiate the number of words in terms for all languages.

init_word_count() : void

Only terms with a word count set to 0 are changed.

Tags
global

string $tbpref Database table prefix

set_word_count()

Initiate the number of words in terms for all languages

set_word_count() : void

Only terms with a word count set to 0 are changed.

Tags
global

string $tbpref Database table prefix

deprecated

Use init_word_count: same effect, but more logical name. Will be removed in version 3.0.0.

parse_japanese_text()

Parse a Japanese text using MeCab and add it to the database.

parse_japanese_text(string $text, int $id) : null|array<string|int, string>
Parameters
$text : string

Text to parse.

$id : int

Text ID. If $id = -1 print results, if $id = -2 return splitted texts

Tags
since
2.5.1-fork

Works even if LOAD DATA LOCAL INFILE operator is disabled.

since
2.6.0-fork

Use PHP instead of SQL, slower but works better.

global

string $tbpref Database table prefix

psalm-return

non-empty-list|null

Return values
null|array<string|int, string>

Splitted sentence if $id = -2

save_processed_text_with_sql()

Insert a processed text in the data in pure SQL way.

save_processed_text_with_sql(string $text, int $id) : void
Parameters
$text : string

Preprocessed text to insert

$id : int

Text ID

Tags
global

string $tbpref Database table prefix

parse_standard_text()

Parse a text using the default tools. It is a not-japanese text.

parse_standard_text(string $text, int $id, int $lid) : null|array<string|int, string>
Parameters
$text : string

Text to parse

$id : int

Text ID. If $id == -2, only split the text.

$lid : int

Language ID.

Tags
since
2.5.1-fork

Works even if LOAD DATA LOCAL INFILE operator is disabled.

global

string $tbpref Database table prefix

psalm-return

non-empty-list|null

Return values
null|array<string|int, string>

If $id == -2 return a splitted version of the text.

prepare_text_parsing()

Pre-parse the input text before a definitive parsing by a specialized parser.

prepare_text_parsing(string $text, int $id, int $lid) : null|array<string|int, string>
Parameters
$text : string

Text to parse

$id : int

Text ID

$lid : int

Language ID

Tags
global

string $tbpref Database table prefix

psalm-return

non-empty-list|null

Return values
null|array<string|int, string>

If $id = -2 return a splitted version of the text

check_text_valid()

Echo the sentences in a text. Prepare JS data for words and word count.

check_text_valid(int $lid) : void
Parameters
$lid : int

Language ID

Tags
global

string $tbpref Database table prefix

registerSentencesTextItems()

Append sentences and text items in the database.

registerSentencesTextItems(int $tid, int $lid, bool $hasmultiword) : void
Parameters
$tid : int

ID of text from which insert data

$lid : int

ID of the language of the text

$hasmultiword : bool

Set to true to insert multi-words as well.

Tags
global

string $tbpref Database table prefix

update_default_values()

Append sentences and text items in the database.

update_default_values(int $id, int $lid, string $_sql) : void
Parameters
$id : int

New default text ID

$lid : int

New default language ID

$_sql : string

Unnused since 2.10.0. Will be removed in 3.0.0.

Tags
global

string $tbpref Database table prefix

deprecated

Since 2.10.0, use registerSentencesTextItems instead

displayTextStatistics()

Display statistics about a text.

displayTextStatistics(int $lid, bool $rtlScript, bool $multiwords) : void
Parameters
$lid : int

Language ID

$rtlScript : bool

true if language is right-to-left

$multiwords : bool

Display if text has multi-words

Tags
global

$tbpref

check_text()

Check a text and display statistics about it.

check_text(string $sql, bool $rtlScript, array<string|int, int> $wl) : void
Parameters
$sql : string
$rtlScript : bool

true if language is right-to-left

$wl : array<string|int, int>

Words lengths

Tags
deprecated

Use displayTextStatistics instead. Will be removed in 3.0.0.

checkExpressions()

Check a language that contains expressions.

checkExpressions(array<string|int, int> $wl) : void
Parameters
$wl : array<string|int, int>

All the different expression length in the language.

Tags
global

string $tbpref Database table prefix

check_text_with_expressions()

Check a language that contains expressions.

check_text_with_expressions(int $id, int $lid, array<string|int, int> $wl, int $wl_max, string $mw_sql) : string
Parameters
$id : int

Text ID

$lid : int

Language ID

$wl : array<string|int, int>

Word length

$wl_max : int

Maximum word length

$mw_sql : string

SQL-formatted string

Tags
global

string $tbpref Database table prefix

deprecated

Since 2.10.0-fork use checkExpressions. It does not modify SQL globals.

Return values
string

SQL-formatted query string

splitCheckText()

Parse the input text.

splitCheckText(string $text, string|int $lid, int $id) : null|array<string|int, string>
Parameters
$text : string

Text to parse

$lid : string|int

Language ID (LgID from languages table)

$id : int

References whether the text is new to the database $id = -1 => Check, return protocol $id = -2 => Only return sentence array $id = TextID => Split: insert sentences/textitems entries in DB

Tags
global

string $tbpref Database table prefix

psalm-return

non-empty-list|null

Return values
null|array<string|int, string>

The sentence array if $id = -2

reparse_all_texts()

Reparse all texts in order.

reparse_all_texts() : void
Tags
global

string $tbpref Database table prefix

update_database()

Update the database if it is using an outdate version.

update_database(string $dbname) : void
Parameters
$dbname : string

Name of the database

Tags
global

string $tbpref Database table prefix

global

0|1 $debug Output debug messages.

since
2.10.0-fork

Migrations are defined thourgh SQL, and not directly here

prefixSQLQuery()

Add a prefix to table in a SQL query string.

prefixSQLQuery(string $sql_line, string $prefix) : mixed
Parameters
$sql_line : string

SQL string to prefix.

$prefix : string

Prefix to add

check_update_db()

Check and/or update the database.

check_update_db(mixed $debug, mixed $tbpref, mixed $dbname) : void
Parameters
$debug : mixed
$tbpref : mixed
$dbname : mixed
Tags
global

mysqli $DBCONNECTION Connection to the database.

since
2.10.0

Use confiduration files instead of containing all the data.

connect_to_database()

Make the connection to the database.

connect_to_database(string $server, string $userid, string $passwd, string $dbname[, string $socket = "" ]) : mysqli
Parameters
$server : string

Server name

$userid : string

Database user ID

$passwd : string

User password

$dbname : string

Database name

$socket : string = ""

Database socket

Tags
psalm-suppress

UndefinedDocblockClass

since
2.6.0-fork

Use mysqli_init and mysql_real_connect instead of deprecated mysql_connect

since
2.6.0-fork

Tries to allow local infiles for the connection.

since
2.9.0

Can accept a $socket as an optional argument

Return values
mysqli

Connection to the database

getDatabasePrefix()

Get the prefixes for the database.

getDatabasePrefix(mixed $dbconnection) : array<string|int, mixed>

Is $tbpref set in connect.inc.php? Take it and $fixed_tbpref=1. If not: $fixed_tbpref=0. Is it set in table "_lwtgeneral"? Take it. If not: Use $tbpref = '' (no prefix, old/standard behaviour).

Parameters
$dbconnection : mixed
Return values
array<string|int, mixed>

Table prefix, and if table prefix should be fixed

get_database_prefixes()

Get the prefixes for the database.

get_database_prefixes(string|null &$tbpref) : 0|1

Is $tbpref set in connect.inc.php? Take it and $fixed_tbpref=1. If not: $fixed_tbpref=0. Is it set in table "_lwtgeneral"? Take it. If not: Use $tbpref = '' (no prefix, old/standard behaviour).

Parameters
$tbpref : string|null

Temporary database table prefix

Tags
deprecated

Since 2.10.0-fork, use getDatabasePrefix instead

Return values
0|1

Table Prefix is fixed, no changes possible.


        
On this page

Search results