Mise à jour du regexp pour se connecter à une base mysql depuis un string. Il y avait le bon regexp dans le functions_mysql.php mais pas dans le functions_mysqli.php (et c'est meilleur d'utiliser mysqli car mysql est déprécié dans les php récent/même plus inclus)

This commit is contained in:
Sit Melai 2017-01-11 20:44:44 +01:00
parent 642c47c180
commit 630efbdeb9

View file

@ -1,295 +1,296 @@
<?php <?php
/*************************************************************************** /***************************************************************************
* mysql.php * mysql.php
* ------------------- * -------------------
* begin : Saturday, Feb 13, 2001 * begin : Saturday, Feb 13, 2001
* copyright : (C) 2001 The phpBB Group * copyright : (C) 2001 The phpBB Group
* email : support@phpbb.com * email : support@phpbb.com
* *
* $Id: functions_mysql.php,v 1.2 2006/07/06 15:17:22 powles Exp $ * $Id: functions_mysql.php,v 1.2 2006/07/06 15:17:22 powles Exp $
* *
***************************************************************************/ ***************************************************************************/
/*************************************************************************** /***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
***************************************************************************/ ***************************************************************************/
/*************************************************************************** /***************************************************************************
* *
* NOTE: this is striped down version from phpBB mysql.php file * NOTE: this is striped down version from phpBB mysql.php file
* modified to work with mysqli * modified to work with mysqli
* *
***************************************************************************/ ***************************************************************************/
if(!defined("SQL_LAYER")) if(!defined("SQL_LAYER"))
{ {
define("SQL_LAYER","mysqli"); define("SQL_LAYER","mysqli");
class sql_db class sql_db
{ {
var $db_connect_id; var $db_connect_id;
var $query_result; var $query_result;
var $num_queries = 0; var $num_queries = 0;
// //
// Constructor // Constructor
// //
function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true) function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
{ {
$this->persistency = $persistency; $this->persistency = $persistency;
$this->user = $sqluser; $this->user = $sqluser;
$this->password = $sqlpassword; $this->password = $sqlpassword;
$this->server = $sqlserver; $this->server = $sqlserver;
$this->dbname = $database; $this->dbname = $database;
if($this->persistency) if($this->persistency)
{ {
$this->server = 'p:'.$this->server; $this->server = 'p:'.$this->server;
} }
$this->db_connect_id = mysqli_connect($this->server, $this->user, $this->password); $this->db_connect_id = mysqli_connect($this->server, $this->user, $this->password);
if($this->db_connect_id) if($this->db_connect_id)
{ {
if($database != "") if($database != "")
{ {
$this->dbname = $database; $this->dbname = $database;
$dbselect = mysqli_select_db($this->db_connect_id, $this->dbname); $dbselect = mysqli_select_db($this->db_connect_id, $this->dbname);
if(!$dbselect) if(!$dbselect)
{ {
mysqli_close($this->db_connect_id); mysqli_close($this->db_connect_id);
$this->db_connect_id = $dbselect; $this->db_connect_id = $dbselect;
} }
} }
return $this->db_connect_id; return $this->db_connect_id;
} }
else else
{ {
echo "Connection to mySQL failed!"; echo "Connection to mySQL failed!";
exit; exit;
} }
} }
// //
// Other base methods // Other base methods
// //
function sql_close() function sql_close()
{ {
if($this->db_connect_id) if($this->db_connect_id)
{ {
if($this->query_result) if($this->query_result)
{ {
@mysqli_free_result($this->query_result); @mysqli_free_result($this->query_result);
} }
$result = mysqli_close($this->db_connect_id); $result = mysqli_close($this->db_connect_id);
return $result; return $result;
} }
else else
{ {
return false; return false;
} }
} }
// //
// Base query method // Base query method
// //
function sql_query($query = "", $transaction = FALSE) function sql_query($query = "", $transaction = FALSE)
{ {
// Remove any pre-existing queries // Remove any pre-existing queries
unset($this->query_result); unset($this->query_result);
if($query != "") if($query != "")
{ {
nt_common_add_debug($query); nt_common_add_debug($query);
$this->num_queries++; $this->num_queries++;
$this->query_result = mysqli_query($this->db_connect_id, $query); $this->query_result = mysqli_query($this->db_connect_id, $query);
} }
if($this->query_result) if($this->query_result)
{ {
return $this->query_result; return $this->query_result;
} }
else else
{ {
return ( $transaction == 'END_TRANSACTION' ) ? true : false; return ( $transaction == 'END_TRANSACTION' ) ? true : false;
} }
} }
function sql_select_db($dbname) function sql_select_db($dbname)
{ {
if($this->db_connect_id) if($this->db_connect_id)
{ {
$result = mysqli_select_db($this->db_connect_id, $dbname); $result = mysqli_select_db($this->db_connect_id, $dbname);
return $result; return $result;
} }
return false; return false;
} }
function sql_reselect_db() function sql_reselect_db()
{ {
if($this->db_connect_id) if($this->db_connect_id)
{ {
$result = mysqli_select_db($this->db_connect_id, $this->dbname); $result = mysqli_select_db($this->db_connect_id, $this->dbname);
return $result; return $result;
} }
return false; return false;
} }
// //
// Other query methods // Other query methods
// //
function sql_numrows($query_id = 0) function sql_numrows($query_id = 0)
{ {
if(!$query_id) if(!$query_id)
{ {
$query_id = $this->query_result; $query_id = $this->query_result;
} }
if($query_id) if($query_id)
{ {
$result = mysqli_num_rows($query_id); $result = mysqli_num_rows($query_id);
return $result; return $result;
} }
else else
{ {
return false; return false;
} }
} }
function sql_affectedrows() function sql_affectedrows()
{ {
if($this->db_connect_id) if($this->db_connect_id)
{ {
$result = mysqli_affected_rows($this->db_connect_id); $result = mysqli_affected_rows($this->db_connect_id);
return $result; return $result;
} }
else else
{ {
return false; return false;
} }
} }
function sql_numfields($query_id = 0) function sql_numfields($query_id = 0)
{ {
if(!$query_id) if(!$query_id)
{ {
$query_id = $this->query_result; $query_id = $this->query_result;
} }
if($query_id) if($query_id)
{ {
$result = mysqli_num_fields($query_id); $result = mysqli_num_fields($query_id);
return $result; return $result;
} }
else else
{ {
return false; return false;
} }
} }
// function sql_fieldname($query_id = 0){} // function sql_fieldname($query_id = 0){}
// function sql_fieldtype($offset, $query_id = 0){} // function sql_fieldtype($offset, $query_id = 0){}
function sql_fetchrow($query_id = 0) function sql_fetchrow($query_id = 0)
{ {
if(!$query_id) if(!$query_id)
{ {
$query_id = $this->query_result; $query_id = $this->query_result;
} }
if($query_id) if($query_id)
{ {
return mysqli_fetch_array($query_id); return mysqli_fetch_array($query_id);
} }
else else
{ {
return false; return false;
} }
} }
function sql_fetchrowset($query_id = 0) function sql_fetchrowset($query_id = 0)
{ {
if(!$query_id) if(!$query_id)
{ {
$query_id = $this->query_result; $query_id = $this->query_result;
} }
if($query_id) if($query_id)
{ {
while($row = mysqli_fetch_array($query_id)) while($row = mysqli_fetch_array($query_id))
{ {
$result[] = $row; $result[] = $row;
} }
return $result; return $result;
} }
else else
{ {
return false; return false;
} }
} }
// function sql_fetchfield($field, $rownum = -1, $query_id = 0){} // function sql_fetchfield($field, $rownum = -1, $query_id = 0){}
// function sql_rowseek($rownum, $query_id = 0){} // function sql_rowseek($rownum, $query_id = 0){}
function sql_nextid(){ function sql_nextid(){
if($this->db_connect_id) if($this->db_connect_id)
{ {
$result = mysqli_insert_id($this->db_connect_id); $result = mysqli_insert_id($this->db_connect_id);
return $result; return $result;
} }
else else
{ {
return false; return false;
} }
} }
function sql_freeresult($query_id = 0){ function sql_freeresult($query_id = 0){
if(!$query_id) if(!$query_id)
{ {
$query_id = $this->query_result; $query_id = $this->query_result;
} }
if ( $query_id ) if ( $query_id )
{ {
@mysqli_free_result($query_id); @mysqli_free_result($query_id);
return true; return true;
} }
else else
{ {
return false; return false;
} }
} }
function sql_escape_string($str) function sql_escape_string($str)
{ {
return mysqli_real_escape_string($this->db_connect_id, $str); return mysqli_real_escape_string($this->db_connect_id, $str);
} }
function sql_error($query_id = 0) function sql_error($query_id = 0)
{ {
$result["message"] = mysqli_error($this->db_connect_id); $result["message"] = mysqli_error($this->db_connect_id);
$result["code"] = mysqli_errno($this->db_connect_id); $result["code"] = mysqli_errno($this->db_connect_id);
return $result; return $result;
} }
} // class sql_db } // class sql_db
class sql_db_string extends sql_db class sql_db_string extends sql_db
{ {
// //
// Constructor ($connstring format : mysql://user:password@host/dbname) // Constructor ($connstring format : mysql://user:password@host/dbname)
// //
function sql_db_string($connstring, $persistency = true) function sql_db_string($connstring, $persistency = true)
{ {
$ret = false; $ret = false;
if ($connstring != '') if ($connstring != '')
{ {
if (preg_match("#^mysqli?://([^:]+)(?::([^@]*))?@([^\\/]+)/([^/]+)[/]?$#", $connstring, $params)) //if (preg_match("#^mysqli?://([^:]+)(?::([^@]*))?@([^\\/]+)/([^/]+)[/]?$#", $connstring, $params))
{ if (ereg("^mysql\:\/\/([^\:]+)\:([^\@]+)\@([^\\]+)\/([^\/]+)[\/]?$", $connstring, $params))
$sqlserver = $params[3]; {
$sqluser = $params[1]; $sqlserver = $params[3];
$sqlpassword = $params[2]; $sqluser = $params[1];
$database = $params[4]; $sqlpassword = $params[2];
$database = $params[4];
$ret = $this->sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency);
} $ret = $this->sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency);
} }
}
return $ret;
} return $ret;
} // class sql_db_string }
} // class sql_db_string
} // if ... define
} // if ... define