Skip to content

Instantly share code, notes, and snippets.

@NrI3
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save NrI3/29bebca1b211bb56f3c0 to your computer and use it in GitHub Desktop.

Select an option

Save NrI3/29bebca1b211bb56f3c0 to your computer and use it in GitHub Desktop.
Php mysql connector
<?php
class MYDB {
var $user;
var $pass;
var $data;
var $port;
var $host;
var $conx;
var $error;
var $successVector;
public function getConnexion(){
return $this->conx;
}
public function MYDB($host,$db,$user,$pass){
$this->host = $host;
$this->data = $db;
$this->user = $user;
$this->pass = $pass;
$this->reconexion();
}
private function reconexion(){
$this->conx = new mysqli(
$this->host,
$this->user,
$this->pass,
$this->data
);
if ($this->conx->connect_error) {
die("Conexion fallida: ".$this->conx->connect_error);
}
}
public function close(){
$this->conx->close();
}
/*
Function by ZeuszAfk
https://gist.github.com/ZeusAFK/024614b55a87ddb1bf96
*/
/*
Added multiple params vector by es3
*/
function ExecutePreparedCallableStatement($query, $types, $params, $results, $callback = false, $mysqli = false){
$this->error = array();
$mysqli = $this->conx;
$new_prepared_statement = true;
if($query instanceof mysqli_stmt){
$stmt = $query;
$new_prepared_statement = false;
}else if(!($stmt = $mysqli->prepare($query))){
array_push($this->error, $mysqli->error);
//echo $mysqli->error;
}
if (sizeof($params) > 0){
if (is_array($params[0])){
$this->successVector = array();
foreach ($params as $key => $value) {
$referencedParams = array();
foreach($params[$key] as $k => $param){
$referencesParams[$k] = &$params[$key][$k];
}
call_user_func_array(array($stmt, "bind_param"), array_merge(array($types), $referencesParams));
$executed = $stmt->execute();
if(!$executed){
global $response;
array_push($this->error, $mysqli->error);
array_push($this->successVector, false);
$response['mysqli_error'] = $mysqli->error;
}else{
array_push($this->successVector, true);
}
}
}else{
$referencedParams = array();
foreach($params as $k => $param){
$referencesParams[$k] = &$params[$k];
}
call_user_func_array(array($stmt, "bind_param"), array_merge(array($types), $referencesParams));
$executed = $stmt->execute();
if(!$executed){
global $response;
$response['mysqli_error'] = $mysqli->error;
array_push($this->error, $mysqli->error);
}
}
}else{
$executed = $stmt->execute();
if(!$executed){
global $response;
$response['mysqli_error'] = $mysqli->error;
array_push($this->error, $mysqli->error);
}
}
$formattedResults = array();
$valuesContainer = array();
foreach($results as $k => $value){
$valuesContainer[$k] = null;
$formattedResults[$value] = &$valuesContainer[$k];
}
if(sizeof($results) > 0){
call_user_func_array(array($stmt, "bind_result"), $formattedResults);
}
if(function_exists('mysqli_fetch_all')){
do {
$stmt->store_result();
while($stmt->fetch()){
if($callback) $callback($formattedResults);
}
} while ($stmt->more_results() && $stmt->next_result());
}else{
$stmt->store_result();
while($stmt->fetch()){
if($callback) $callback($formattedResults);
}
$stmt->free_result();
if($new_prepared_statement){
$stmt->close();
}
while ($mysqli->more_results()){
$mysqli->next_result();
$result = $mysqli->use_result();
if ($result instanceof mysqli_result) {
$result->free();
}
}
}
return $executed ? true : false;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment