persistency = $persistency; $this->user = $sqluser; $this->password = $sqlpassword; $this->server = $sqlserver; $this->dbname = $database; if($this->persistency) { $this->server = 'p:'.$this->server; } $this->db_connect_id = mysqli_connect($this->server, $this->user, $this->password); if($this->db_connect_id) { if($database != "") { $this->dbname = $database; $dbselect = mysqli_select_db($this->db_connect_id, $this->dbname); if(!$dbselect) { mysqli_close($this->db_connect_id); $this->db_connect_id = $dbselect; } } return $this->db_connect_id; } else { throw new \RuntimeException('Connection to mySQL failed!'); } } // // Other base methods // function sql_close() { if($this->db_connect_id) { if($this->query_result) { @mysqli_free_result($this->query_result); } $result = mysqli_close($this->db_connect_id); return $result; } else { return false; } } // // Base query method // function sql_query($query = "", $transaction = FALSE) { // Remove any pre-existing queries unset($this->query_result); if($query != "") { nt_common_add_debug($query); $this->num_queries++; $this->query_result = mysqli_query($this->db_connect_id, $query); } if($this->query_result) { return $this->query_result; } else { return ( $transaction == 'END_TRANSACTION' ) ? true : false; } } function sql_select_db($dbname) { if($this->db_connect_id) { $result = mysqli_select_db($this->db_connect_id, $dbname); return $result; } return false; } function sql_reselect_db() { if($this->db_connect_id) { $result = mysqli_select_db($this->db_connect_id, $this->dbname); return $result; } return false; } // // Other query methods // function sql_numrows($query_id = 0) { if(!$query_id) { $query_id = $this->query_result; } if($query_id) { $result = mysqli_num_rows($query_id); return $result; } else { return false; } } function sql_affectedrows() { if($this->db_connect_id) { $result = mysqli_affected_rows($this->db_connect_id); return $result; } else { return false; } } function sql_numfields($query_id = 0) { if(!$query_id) { $query_id = $this->query_result; } if($query_id) { $result = mysqli_num_fields($query_id); return $result; } else { return false; } } // function sql_fieldname($query_id = 0){} // function sql_fieldtype($offset, $query_id = 0){} function sql_fetchrow($query_id = 0) { if(!$query_id) { $query_id = $this->query_result; } if($query_id) { return mysqli_fetch_array($query_id); } else { return false; } } function sql_fetchrowset($query_id = 0) { if(!$query_id) { $query_id = $this->query_result; } if($query_id) { while($row = mysqli_fetch_array($query_id)) { $result[] = $row; } return $result; } else { return false; } } // function sql_fetchfield($field, $rownum = -1, $query_id = 0){} // function sql_rowseek($rownum, $query_id = 0){} function sql_nextid(){ if($this->db_connect_id) { $result = mysqli_insert_id($this->db_connect_id); return $result; } else { return false; } } function sql_freeresult($query_id = 0){ if(!$query_id) { $query_id = $this->query_result; } if ( $query_id ) { @mysqli_free_result($query_id); return true; } else { return false; } } function sql_escape_string($str) { return mysqli_real_escape_string($this->db_connect_id, $str); } function sql_error($query_id = 0) { $result["message"] = mysqli_error($this->db_connect_id); $result["code"] = mysqli_errno($this->db_connect_id); return $result; } } // class sql_db class sql_db_string extends sql_db { // // Constructor ($connstring format : mysql://user:password@host/dbname) // function __construct($connstring, $persistency = true) { $ret = false; if ($connstring != '') { if (preg_match("#^mysqli?://([^:]+)(?::([^@]*))?@([^\\/]+)/([^/]+)[/]?$#", $connstring, $params)) { $sqlserver = $params[3]; $sqluser = $params[1]; $sqlpassword = $params[2]; $database = $params[4]; $ret = parent::__construct($sqlserver, $sqluser, $sqlpassword, $database, $persistency); } } return $ret; } } // class sql_db_string } // if ... define