1
0
Fork 0
mirror of https://github.com/DanielnetoDotCom/YouPHPTube synced 2025-10-03 09:49:28 +02:00
This commit is contained in:
DanieL 2022-08-11 17:04:52 -03:00
parent e81ee866ad
commit fc3bfefaf8

View file

@ -1,4 +1,5 @@
<?php <?php
/* /*
tester-execution-code tester-execution-code
$sql = "SELECT * FROM users WHERE id=?;"; $sql = "SELECT * FROM users WHERE id=?;";
@ -18,11 +19,12 @@
* Internal used class * Internal used class
*/ */
class iimysqli_result class iimysqli_result {
{
public $stmt; public $stmt;
public $nCols; public $nCols;
public $fields; public $fields;
} }
global $disableMysqlNdMethods; global $disableMysqlNdMethods;
@ -34,10 +36,9 @@ $disableMysqlNdMethods = false;
* It wouldn't be possible without Daan on https://stackoverflow.com/questions/31562359/workaround-for-mysqlnd-missing-driver * It wouldn't be possible without Daan on https://stackoverflow.com/questions/31562359/workaround-for-mysqlnd-missing-driver
*/ */
class sqlDAL class sqlDAL {
{
public static function executeFile($filename) public static function executeFile($filename) {
{
global $global; global $global;
$templine = ''; $templine = '';
// Read in entire file // Read in entire file
@ -71,8 +72,7 @@ class sqlDAL
* @return boolean true on success, false on fail * @return boolean true on success, false on fail
*/ */
public static function writeSql($preparedStatement, $formats = "", $values = []) public static function writeSql($preparedStatement, $formats = "", $values = []) {
{
global $global, $disableMysqlNdMethods; global $global, $disableMysqlNdMethods;
if (empty($preparedStatement)) { if (empty($preparedStatement)) {
return false; return false;
@ -95,9 +95,9 @@ class sqlDAL
} }
if (!($stmt = $global['mysqli']->prepare($preparedStatement))) { if (!($stmt = $global['mysqli']->prepare($preparedStatement))) {
log_error("[sqlDAL::writeSql] Prepare failed: (" . $global['mysqli']->errno . ") " . $global['mysqli']->error . log_error("[sqlDAL::writeSql] Prepare failed: (" . $global['mysqli']->errno . ") " . $global['mysqli']->error .
" preparedStatement = ". json_encode($preparedStatement). " preparedStatement = " . json_encode($preparedStatement) .
" formats = ". json_encode($formats)); " formats = " . json_encode($formats));
return false; return false;
} }
//var_dump($preparedStatement, $formats, $values);exit; //var_dump($preparedStatement, $formats, $values);exit;
@ -109,7 +109,7 @@ class sqlDAL
$stmt->execute(); $stmt->execute();
} catch (Exception $exc) { } catch (Exception $exc) {
log_error($exc->getTraceAsString()); log_error($exc->getTraceAsString());
log_error('Error in writeSql stmt->execute: '.$preparedStatement); log_error('Error in writeSql stmt->execute: ' . $preparedStatement);
} }
if ($stmt->errno !== 0) { if ($stmt->errno !== 0) {
@ -136,8 +136,7 @@ class sqlDAL
* @return Object Depend if mysqlnd is active or not, a object, but always false on fail * @return Object Depend if mysqlnd is active or not, a object, but always false on fail
*/ */
public static function readSql($preparedStatement, $formats = "", $values = [], $refreshCache = false) public static function readSql($preparedStatement, $formats = "", $values = [], $refreshCache = false) {
{
// $refreshCache = true; // $refreshCache = true;
global $global, $disableMysqlNdMethods, $readSqlCached, $crc; global $global, $disableMysqlNdMethods, $readSqlCached, $crc;
// need to add dechex because some times it return an negative value and make it fails on javascript playlists // need to add dechex because some times it return an negative value and make it fails on javascript playlists
@ -156,22 +155,24 @@ class sqlDAL
$readSqlCached[$crc] = "false"; $readSqlCached[$crc] = "false";
_mysql_connect(); _mysql_connect();
try {
if (!($stmt = $global['mysqli']->prepare($preparedStatement))) { $stmt = $global['mysqli']->prepare($preparedStatement);
log_error("[sqlDAL::readSql] (mysqlnd) Prepare failed: ({$global['mysqli']->errno}) ({$global['mysqli']->error}) ". } catch (Exception $exc) {
" preparedStatement = ". json_encode($preparedStatement). log_error("[sqlDAL::readSql] (mysqlnd) Prepare failed: ({$global['mysqli']->errno}) ({$global['mysqli']->error}) " .
" formats = ". json_encode($formats). " preparedStatement = " . json_encode($preparedStatement) .
" values = ". json_encode($values). " formats = " . json_encode($formats) .
" refreshCache = ". json_encode($refreshCache). " values = " . json_encode($values) .
" stmt = ". json_encode($stmt)); " refreshCache = " . json_encode($refreshCache) .
" stmt = " . json_encode($stmt));
//log_error("[sqlDAL::readSql] trying close and reconnect"); //log_error("[sqlDAL::readSql] trying close and reconnect");
_mysql_close(); _mysql_close();
_mysql_connect(); _mysql_connect();
if (!($stmt = $global['mysqli']->prepare($preparedStatement))) { try {
$stmt = $global['mysqli']->prepare($preparedStatement);
log_error("[sqlDAL::readSql] SUCCESS close and reconnect works!");
} catch (Exception $exc) {
log_error("[sqlDAL::readSql] (mysqlnd) Prepare failed again return false"); log_error("[sqlDAL::readSql] (mysqlnd) Prepare failed again return false");
return false; return false;
} else {
log_error("[sqlDAL::readSql] SUCCESS close and reconnect works!");
} }
} }
if (!sqlDAL::eval_mysql_bind($stmt, $formats, $values)) { if (!sqlDAL::eval_mysql_bind($stmt, $formats, $values)) {
@ -255,8 +256,7 @@ class sqlDAL
* @param Object $result A object from sqlDAL::readSql * @param Object $result A object from sqlDAL::readSql
*/ */
public static function close($result) public static function close($result) {
{
global $disableMysqlNdMethods, $global; global $disableMysqlNdMethods, $global;
if ((!function_exists('mysqli_fetch_all')) || ($disableMysqlNdMethods !== false)) { if ((!function_exists('mysqli_fetch_all')) || ($disableMysqlNdMethods !== false)) {
if (!empty($result->stmt)) { if (!empty($result->stmt)) {
@ -271,8 +271,7 @@ class sqlDAL
* @return int The nr of rows * @return int The nr of rows
*/ */
public static function num_rows($res) public static function num_rows($res) {
{
global $global, $disableMysqlNdMethods, $crc, $num_row_cache; global $global, $disableMysqlNdMethods, $crc, $num_row_cache;
if (!isset($num_row_cache)) { if (!isset($num_row_cache)) {
$num_row_cache = []; $num_row_cache = [];
@ -295,8 +294,7 @@ class sqlDAL
} }
// unused // unused
public static function cached_num_rows($data) public static function cached_num_rows($data) {
{
return sizeof($data); return sizeof($data);
} }
@ -306,8 +304,7 @@ class sqlDAL
* @return array A array filled with all rows as a assoc array * @return array A array filled with all rows as a assoc array
*/ */
public static function fetchAllAssoc($result) public static function fetchAllAssoc($result) {
{
global $crc, $fetchAllAssoc_cache; global $crc, $fetchAllAssoc_cache;
if (!isset($fetchAllAssoc_cache)) { if (!isset($fetchAllAssoc_cache)) {
$fetchAllAssoc_cache = []; $fetchAllAssoc_cache = [];
@ -328,8 +325,7 @@ class sqlDAL
* @return int A single row in a assoc array * @return int A single row in a assoc array
*/ */
public static function fetchAssoc($result) public static function fetchAssoc($result) {
{
global $global, $disableMysqlNdMethods; global $global, $disableMysqlNdMethods;
ini_set('memory_limit', '-1'); ini_set('memory_limit', '-1');
// here, a cache is more/too difficult, because fetch gives always a next. with this kind of cache, we would give always the same. // here, a cache is more/too difficult, because fetch gives always a next. with this kind of cache, we would give always the same.
@ -349,8 +345,7 @@ class sqlDAL
* @return array A array filled with all rows * @return array A array filled with all rows
*/ */
public static function fetchAllArray($result) public static function fetchAllArray($result) {
{
global $crc, $fetchAllArray_cache; global $crc, $fetchAllArray_cache;
if (!isset($fetchAllArray_cache)) { if (!isset($fetchAllArray_cache)) {
$fetchAllArray_cache = []; $fetchAllArray_cache = [];
@ -374,8 +369,7 @@ class sqlDAL
* @return int A single row in a array * @return int A single row in a array
*/ */
public static function fetchArray($result) public static function fetchArray($result) {
{
global $global, $disableMysqlNdMethods; global $global, $disableMysqlNdMethods;
if ((function_exists('mysqli_fetch_all')) && ($disableMysqlNdMethods == false)) { if ((function_exists('mysqli_fetch_all')) && ($disableMysqlNdMethods == false)) {
return $result->fetch_array(); return $result->fetch_array();
@ -385,8 +379,7 @@ class sqlDAL
return false; return false;
} }
private static function eval_mysql_bind($stmt, $formats, $values) private static function eval_mysql_bind($stmt, $formats, $values) {
{
if (($stmt->param_count != sizeof($values)) || ($stmt->param_count != strlen($formats))) { if (($stmt->param_count != sizeof($values)) || ($stmt->param_count != strlen($formats))) {
return false; return false;
} }
@ -404,8 +397,7 @@ class sqlDAL
return true; return true;
} }
private static function iimysqli_stmt_get_result($stmt) private static function iimysqli_stmt_get_result($stmt) {
{
global $global; global $global;
$metadata = mysqli_stmt_result_metadata($stmt); $metadata = mysqli_stmt_result_metadata($stmt);
$ret = new iimysqli_result(); $ret = new iimysqli_result();
@ -432,8 +424,7 @@ class sqlDAL
return $ret; return $ret;
} }
private static function iimysqli_result_fetch_assoc(&$result) private static function iimysqli_result_fetch_assoc(&$result) {
{
global $global; global $global;
$ret = []; $ret = [];
$code = "return mysqli_stmt_bind_result(\$result->stmt "; $code = "return mysqli_stmt_bind_result(\$result->stmt ";
@ -452,8 +443,7 @@ class sqlDAL
return $ret; return $ret;
} }
private static function iimysqli_result_fetch_array(&$result) private static function iimysqli_result_fetch_array(&$result) {
{
$ret = []; $ret = [];
$code = "return mysqli_stmt_bind_result(\$result->stmt "; $code = "return mysqli_stmt_bind_result(\$result->stmt ";
@ -470,14 +460,14 @@ class sqlDAL
}; };
return $ret; return $ret;
} }
} }
function log_error($err) function log_error($err) {
{
if (!empty($global['debug']) || isCommandLineInterface()) { if (!empty($global['debug']) || isCommandLineInterface()) {
echo $err; echo $err;
} }
_error_log("MySQL ERROR: ".json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 5)), AVideoLog::$ERROR); _error_log("MySQL ERROR: " . json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 5)), AVideoLog::$ERROR);
_error_log($err, AVideoLog::$ERROR); _error_log($err, AVideoLog::$ERROR);
} }