Convert this specific mysql bd class to mysqli - php

Can you please help me to fully convert this class to work with mysqli?
It's a class working on an old system, I just want to make it work with mysqli without having to modify the existing code on all the system.
I tried but with no success.
Thanks in advance!
class BD {
var $sServidor = "host";
var $sBaseDeDatos = "DB";
var $sUsuario = "user";
var $sClave = "pass";
function Conectar() {
if (($this->sServidor != "") && ($this->sUsuario != "")) {
$this->oConexion = mysql_connect($this->sServidor, $this->sUsuario, $this->sClave);
mysql_select_db($this->sBaseDeDatos, $this->oConexion);
mysql_set_charset("utf8", $this->oConexion);
}
}
function RetornarConexion() {
return $this->oConexion;
}
function Seleccionar($pSQL, $pRetornarFila = false) {
$oResultado = $this->Ejecutar($pSQL);
return (($pRetornarFila) ? $this->RetornarFila($oResultado) : $oResultado);
}
function RetornarFila($pResultado) {
return mysql_fetch_array($pResultado);
}
function ContarFilas($pResultado) {
$lFilas = 0;
if ($pResultado) {
$lFilas = mysql_num_rows($pResultado);
}
return $lFilas;
}
function Ejecutar($pSQL) {
$this->Conectar();
$oResultado = mysql_query($pSQL, $this->oConexion);
if ($oResultado) {
if (strpos(strtoupper($pSQL), "INSERT INTO") !== false) {
$oResultado = mysql_insert_id();
} else if (strpos(strtoupper($pSQL), "UPDATE") !== false) {
$oResultado = mysql_affected_rows();
}
}
return $oResultado;
}
function RetornarTipo($pResultado, $pCampo) {
$sTipo = "";
if ($pResultado) {
$sTipo = mysql_field_type($pResultado, $pCampo);
}
return $sTipo;
}
function RetornarLongitud($pResultado, $pCampo) {
$lLongitud = 0;
if ($pResultado) {
$lLongitud = mysql_field_len($pResultado, $pCampo);
}
return $lLongitud;
}
function Desconectar() {
mysql_close($this->oConexion);
}
}

Okay I'm testing it and think it works okay. Here's the code:
P / D: Sorry for wasting your time. Conclusion: I have to sleep a few more hours per day. Thanks and sorry again.
class BD {
var $sServidor = "host";
var $sBaseDeDatos = "DB";
var $sUsuario = "user";
var $sClave = "pass";
function Conectar() {
if (($this->sServidor != "") && ($this->sUsuario != "")) {
$this->oConexion = mysqli_connect($this->sServidor, $this->sUsuario, $this->sClave);
mysqli_select_db($this->oConexion, $this->sBaseDeDatos);
mysqli_set_charset($this->oConexion, "utf8");
}
}
function RetornarConexion() {
return $this->oConexion;
}
function Seleccionar($pSQL, $pRetornarFila = false) {
$oResultado = $this->Ejecutar($pSQL);
return (($pRetornarFila) ? $this->RetornarFila($oResultado) : $oResultado);
}
function RetornarFila($pResultado) {
return mysqli_fetch_array($pResultado);
}
function ContarFilas($pResultado) {
$lFilas = 0;
if ($pResultado) {
$lFilas = mysqli_num_rows($pResultado);
}
return $lFilas;
}
function Ejecutar($pSQL) {
$this->Conectar();
$oResultado = mysqli_query($this->oConexion, $pSQL);
if ($oResultado) {
if (strpos(strtoupper($pSQL), "INSERT INTO") !== false) {
$oResultado = mysqli_insert_id($this->oConexion);
} else if (strpos(strtoupper($pSQL), "UPDATE") !== false) {
$oResultado = mysqli_affected_rows($this->oConexion);
}
}
return $oResultado;
}
function RetornarTipo($pResultado, $pCampo) {
$sTipo = "";
if ($pResultado) {
$sTipo = mysqli_field_type($pResultado, $pCampo);
}
return $sTipo;
}
function RetornarLongitud($pResultado, $pCampo) {
$lLongitud = 0;
if ($pResultado) {
$lLongitud = mysqli_field_len($pResultado, $pCampo);
}
return $lLongitud;
}
function Desconectar() {
mysqli_close($this->oConexion);
}
}

Related

Include Function File in function.php wordpress

I just started using wordpress.
I created this function inside function.php, and include file below, works!:
function is_live_2020() {
if(is_page_template('landing.php') || is_page_template('collection.php')) {
return true;
}
return false;
}
include get_template_directory().'/model-filter.php';
but when I created new function again, include file doesn't work:
function is_staging() {
if(is_page_template('landing-staging.php') || is_page_template('collection-staging.php')) {
return true;
}
return false;
}
function load_model_filter() {
if (is_live_2020()) {
include get_template_directory().'/model-filter.php';
} else {
include get_template_directory().'/model-filter-new.php';
}
}
add_action('wp', 'load_model_filter');
how is the correct way to include those function files?
btw.. here model-filter.php:
<?php
function collectionFilter() {
global $wpdb;
$where = array();
if (location_slug()=='my') {
if (1 == $_POST['fman'] && 0 == $_POST['fwoman']) {
$where[] = "filter_Gender like '".(_x_filter_strings($_POST['en'],"men"))."%'";
} else if (0 == $_POST['fman'] && 1 == $_POST['fwoman']) {
$where[] = "filter_Gender like '%".(_x_filter_strings($_POST['en'],"women"))."'";
}
$size = array();
if (1 == $_POST['f28']) {
$size[] = (_x_filter_strings($_POST['en'],"small_size"));
}
if (1 == $_POST['f36']) {
$size[] = (_x_filter_strings($_POST['en'],"mid_size"));
}
if (1 == $_POST['f40']) {
$size[] = (_x_filter_strings($_POST['en'],"large_size"));
}
if (0 < count($size)) {
$where[] = "filter_Size IN ('" . implode("', '", $size) . "')";
etc.....

A database has already be specified for this key. multiple database

Hello world I'm new so sorry for my question. but I need your help
I have this code
public function GetSyncTransaksi ($ip_add_Get,$DBIDGet='db_ique',$table_get='transaksi') {
$temp_get = NULL;
if ($DBIDGet == 'db_ique') {
$dbhost_get = $ip_add_Get;
$dbuser_get = USER_CABANG; //Harus diganti
$dbpwd_get = PASS_CABANG; //Harus diganti
$dbname_get = $DBIDGet;
} else {
$dbhost_get = $ip_add_Get;
$dbuser_get = USER_PUSAT; //Harus diganti
$dbpwd_get = PASS_PUSAT; //Harus diganti
$dbname_get = "ique_".$DBIDGet;
}
R::addDatabase($dbname_get, 'mysql:host='.$dbhost_get.';dbname='.$dbname_get, $dbuser_get, $dbpwd_get, TRUE);
R::selectDatabase($dbname_get);
$qget = "SELECT * FROM sync_$table_get";
$temp_get = R::getAll($qget);
return $temp_get;
}
public function UpdateSyncTransaksi ($ip_add,$DBID='db_ique',$table='transaksi') {
$temp = NULL;
if ($DBID == 'db_ique') {
$dbhost = $ip_add;
$dbuser = USER_CABANG;
$dbpwd = PASS_CABANG;
$dbname = $DBID;
} else {
$dbhost = $ip_add;
$dbuser = USER_PUSAT;
$dbpwd = PASS_PUSAT;
$dbname = "ique_".$DBID;
}
R::addDatabase($dbname, 'mysql:host='.$dbhost.';dbname='.$dbname, $dbuser, $dbpwd, TRUE);
R::selectDatabase($dbname);
$q = "TRUNCATE TABLE sync_$table";
$temp = R::exec($q);
$q = "INSERT INTO sync_$table SELECT tanggal,COUNT(*) FROM $table GROUP BY tanggal";
$temp = R::exec($q);
return $temp;
}
And I call to this Function
public function CompareSyncTransaksi ($server,$id,$table='transaksi') {
$difference = NULL;
$updatesynccabang = $this->UpdateSyncTransaksi($server,'db_ique',$table); //Update sync_transaksi cabang
$updatesyncpusat = $this->UpdateSyncTransaksi("localhost",$id,$table); //Update sync_transaksi pusat
$synccabang = $this->GetSyncTransaksi($server,'db_ique',$table);
$syncpusat = $this->GetSyncTransaksi("localhost",$id,$table);
$tglcabang = NULL;
if ($synccabang != NULL) {
foreach ($synccabang as $temp) {
$tglcabang[] = $temp->tanggal;
}
}
$tglpusat = NULL;
if ($syncpusat != NULL) {
foreach ($syncpusat as $temp) {
$tglpusat[] = $temp->tanggal;
}
}
$intersect = NULL;
if($tglcabang != NULL && $tglpusat != NULL) {
$difference = array_diff($tglcabang,$tglpusat);
$intersect = array_intersect($tglcabang,$tglpusat);
}
if($intersect != NULL) {
foreach ($intersect as $temp) {
foreach ($synccabang as $temp1) {
if ($temp1->tanggal == $temp) {
$jumlahcabang = $temp1->jumlah;
}
}
foreach ($syncpusat as $temp2) {
if ($temp2->tanggal == $temp) {
$jumlahpusat = $temp2->jumlah;
}
}
if ($jumlahcabang != $jumlahpusat) {
$difference[] = $temp;
}
}
}
if ($difference) {
asort($difference);
}
return $difference;
}
The problem is I got Error "A database has already be specified for this key."
I think the problem is in function GetSyncTransaksi and function UpdateSyncTransaksi.
How can I fix this. please help thank you

How to override a variable in PHP that's in two functions

function d20() {
$rollAll = false;
$roll = rand(1,20);
if ($rollAll == false) {
echo $roll;
}
}
function rollAll() {
$rollAll = true;
d20();
}
rollAll();
I want to make it so that whenever I call the rollAll function, $rollAll will be true and it won't echo the roll. Sorry if this problem seems really stupid, but I'm new to PHP.
Thanks.
#Rizier123 Answered just i pass the variable from rollAll to d20
function d20($rollAll) {
$roll = rand(1,20);
if ($rollAll == false) {
echo $roll;
}
}
function rollAll() {
$rollAll = true;
d20($rollAll );
}
rollAll();
set the variable $rollAll in global scope or pass as function params
function d20($rollAll) {
$rollAll = false;
$roll = rand(1,20);
if ($rollAll == false) {
echo $roll;
}
}
function rollAll() {
$rollAll = true;
d20( $rollAll );
}
rollAll();
or
$rollAll = true;// or false
function d20() {
$rollAll = false;
$roll = rand(1,20);
if ($rollAll == false) {
echo $roll;
}
}
function rollAll() {
$rollAll = true;
d20( );
}
rollAll();

php class _Construct empty

<?
session_start();
/*
echo $_SESSION['SQLIP'];
echo "<br>";
echo $_SESSION['SQLDB'];
echo "<br>";
echo $_SESSION['SQLUSER'];
echo "<br>";
echo $_SESSION['SQLPASS'];
echo "<br>";
*/
class DB_MSSQL {
private $Host;
private $Database;
private $User;
private $Password;
public $Link_ID = 0;
public $Query_ID = 0;
public $Record = array();
public $Row = 0;
public $Errno = 0;
public $Error = "";
public $Halt_On_Error = "yes";
public $Auto_Free = 1;
public $PConnect = 0;
public function _construct(){
$this->Host = $_SESSION['SQLIP'];
$this->Database = $_SESSION['SQLDB'];
$this->User = $_SESSION['SQLUSER'];
$this->Password = $_SESSION['SQLPASS'];
}
function DB_MSSQL($query = "") {
if($query) {
$this->query($query);
}
}
function connect() {
if ( 0 == $this->Link_ID ) {
if(!$this->PConnect) {
$this->Link_ID = mssql_connect($this->Host, $this->User, $this->Password);
} else {
$this->Link_ID = mssql_pconnect($this->Host, $this->User, $this->Password);
}
if (!$this->Link_ID)
$this->connect_failed("connect ($this->Host, $this->User, \$Password) failed");
else
if (!mssql_select_db($this->Database, $this->Link_ID)) {
$this->connect_failed("cannot use database ".$this->Database);
}
}
}
function connect_failed($message) {
$this->Halt_On_Error = "yes";
$this->halt($message);
}
function free_result(){
mssql_free_result($this->Query_ID);
$this->Query_ID = 0;
}
function query($Query_String)
{
/* No empty queries, please, since PHP4 chokes on them. */
if ($Query_String == "")
/* The empty query string is passed on from the constructor,
* when calling the class without a query, e.g. in situations
* like these: '$db = new DB_Sql_Subclass;'
*/
return 0;
if (!$this->Link_ID)
$this->connect();
// printf("<br>Debug: query = %s<br>\n", $Query_String);
$this->Query_ID = mssql_query($Query_String, $this->Link_ID);
$this->Row = 0;
if (!$this->Query_ID) {
$this->Errno = 1;
$this->Error = "General Error (The MSSQL interface cannot return detailed error messages).";
$this->halt("Invalid SQL: ");
}
return $this->Query_ID;
}
function next_record() {
if ($this->Record = mssql_fetch_row($this->Query_ID)) {
// add to Record[<key>]
$count = mssql_num_fields($this->Query_ID);
for ($i=0; $i<$count; $i++){
$fieldinfo = mssql_fetch_field($this->Query_ID,$i);
$this->Record[strtolower($fieldinfo->name)] = $this->Record[$i];
}
$this->Row += 1;
$stat = 1;
} else {
if ($this->Auto_Free) {
$this->free_result();
}
$stat = 0;
}
return $stat;
}
function seek($pos) {
mssql_data_seek($this->Query_ID,$pos);
$this->Row = $pos;
}
function metadata($table) {
$count = 0;
$id = 0;
$res = array();
$this->connect();
$id = mssql_query("select * from $table", $this->Link_ID);
if (!$id) {
$this->Errno = 1;
$this->Error = "General Error (The MSSQL interface cannot return detailed error messages).";
$this->halt("Metadata query failed.");
}
$count = mssql_num_fields($id);
for ($i=0; $i<$count; $i++) {
$info = mssql_fetch_field($id, $i);
$res[$i]["table"] = $table;
$res[$i]["name"] = $info->name;
$res[$i]["len"] = $info->max_length;
$res[$i]["flags"] = $info->numeric;
}
$this->free_result();
return $res;
}
function affected_rows() {
// Not a supported function in PHP3/4. Chris Johnson, 16May2001.
// return mssql_affected_rows($this->Query_ID);
$rsRows = mssql_query("Select ##rowcount as rows", $this->Link_ID);
if ($rsRows) {
return mssql_result($rsRows, 0, "rows");
}
}
function num_rows() {
return mssql_num_rows($this->Query_ID);
}
function num_fields() {
return mssql_num_fields($this->Query_ID);
}
function nf() {
return $this->num_rows();
}
function np() {
print $this->num_rows();
}
function f($Field_Name) {
return $this->Record[strtolower($Field_Name)];
}
function p($Field_Name) {
print $this->f($Field_Name);
}
function halt($msg) {
if ("no" == $this->Halt_On_Error)
return;
$this->haltmsg($msg);
if ("report" != $this->Halt_On_Error)
die("Session halted.");
}
function haltmsg($msg) {
printf("<p>Server have a critical error!<br><br><br>We are very sorry for any inconvenience!<br><br>\n", $msg);
printf("<b>MSSQL Error</b>: %s (%s)</p>\n",
$this->Errno,
$this->Error);
}
}
$_php_major_version = substr(phpversion(), 0, 1);
if((4 > $_php_major_version) or !class_exists("DB_Sql"))
{
class DB_Sql extends DB_MSSQL
{
function DB_Sql($query = "")
{
$this->DB_MSSQL($query);
}
}
}
unset($_php_major_version);
?>
I have a question, why on DB_MSSQL my $Host,$Datebase,$User,$Password are empty?
If i test $_SESSIONS Between DB_MSSQL are OK.
Class don't have errors but this variables are empty... and i don't know why..
Can anybody help me?
Thank you verry much!
You have missed one underscore, you should have write :
public function __construct()
It should work like this.

Weird behavior with a class level variable

I've looked at this code for over an hour, and it looks fine to me. However, the description variable is losing scope when I use the insertSubmit() function. The variable is set during construction but not when I use the insertSubmit(). Am I missing something here?
<?php session_start();
class SubmitNew {
var $title = "";
var $category = "";
var $summary = "";
var $description = "";
var $username = "";
var $timestamp = "";
var $errors = "";
var $errorStr = "";
var $link = "";
var $db = "";
public function __construct() {
$this->setVariables();
$this->errors = 0;
$this->p_id = 1;
}
public function setVariables() {
$this->title = "1";
$this->category = "2";
$this->summary = "3";
$this->description = "4";
echo $this->description;
$this->timestamp = mktime(date("G"), date("i"),
date("s"), date("m") , date("d"), date("Y"));
}
public function errorBlank() {
if($this->title == null) {
$this->errorStr = $this->errorStr."blanktitle";
$this->errors++;
} if($this->summary == null) {
$this->erroStr = $this->errorStr."blanksummary";
$this->errors++;
} if($this->description = null) {
$this->erroStr = $this->errorStr."blankdescription";
$this->errors++;
}
}
public function errorAll() {
if($this->errors == 0) {
return "success";
} else {
return $this->errorStr;
}
}
public function insertSubmit() {
echo $this->description;
}
}
The problem is located at errorBlank() function, when you do this:
} if($this->description = null) {
Instead of doing this:
} if($this->description == null) {
Note the extra equal sign! You were assigning null to $this->description, instead of comparing them!

Categories