Updated the MySQL Wrapper to PHP7 (mysqli)

This commit is contained in:
Tim Windelschmidt 2016-07-18 19:40:52 +02:00
parent 667b753486
commit 919585828d
1 changed files with 50 additions and 47 deletions

View File

@ -1,13 +1,14 @@
<?php <?php
class MySQL { class MySQL
{
var $db_host; var $db_host;
var $db_user; var $db_user;
var $db_pwd; var $db_pwd;
var $db_name; var $db_name;
var $queries = 0; var $queries = 0;
var $connections = 0; var $connections = 0;
var $linki; var $db;
function set($db_host, $db_user, $db_pwd, $db_name) function set($db_host, $db_user, $db_pwd, $db_name)
{ {
@ -15,105 +16,107 @@ class MySQL {
$this->db_user = $db_user; $this->db_user = $db_user;
$this->db_pwd = $db_pwd; $this->db_pwd = $db_pwd;
$this->db_name = $db_name; $this->db_name = $db_name;
} }
function connect() function connect()
{ {
// connect to mysql $this->db = new mysqli($this->db_host, $this->db_user, $this->db_pwd, $this->db_name);
$this->linki = mysql_connect($this->db_host, $this->db_user, $this->db_pwd) $this->db = new mysqli($this->db_host, $this->db_user, $this->db_pwd, $this->db_name);
or die("Could not connect to mysql server"); if ($this->db->connect_errno > 0) {
// connect to the database die('Unable to connect to database [' . $this->db->connect_error . ']');
mysql_select_db($this->db_name, $this->linki) }
or die("Database: database not found"); $this->connections++;
$this->connections++;
// return $db_link for other functions // return $db_link for other functions
//return $linki; //return $db;
} }
function query($sql) function query($sql)
{ {
$db = $this->db;
//echo $sql . "<br>"; //echo $sql . "<br>";
if (!isset($this->linki)) { if (!isset($this->db)) {
$this->connect(); $this->connect();
}
if (!$result = $db->query($sql)) {
die('There was an error running the query [' . $this->db->error . ']');
} }
$result = mysql_query($sql, $this->linki)
or die("Invalid query: " . mysql_error());
// used for other functions // used for other functions
$this->queries++; $this->queries++;
return $result; return $result;
} }
function fetch_array($result) function fetch_array($result)
{ {
// create an array called $row // create an array called $row
$row = mysql_fetch_array($result); $row = mysqli_fetch_array($result);
// return the array $row or false if none found // return the array $row or false if none found
return $row; return $row;
} }
function escape($string) { function escape($string)
return mysql_real_escape_string($string,$this->linki); {
return mysqli_real_escape_string($this->db, $string);
} }
function fetch_assoc($result) function fetch_assoc($result)
{ {
// create an array called $row // create an array called $row
$row = mysql_fetch_assoc($result); $row = mysqli_fetch_assoc($result);
// return the array $row or false if none found // return the array $row or false if none found
return $row; return $row;
} }
// function fetch_row($result) // function fetch_row($result)
// { // {
// // create an array called $row // // create an array called $row
// $row = mysql_fetch_row($result); // $row = mysql_fetch_row($result);
// // return the array $row or false if none found // // return the array $row or false if none found
// return $row; // return $row;
// } // }
function num_rows($result) function num_rows($result)
{ {
// determine row count // determine row count
$num_rows = mysql_num_rows($result); $num_rows = mysqli_num_rows($result);
// return the row count or false if none foune // return the row count or false if none foune
return $num_rows; return $num_rows;
} }
function insert_id() function insert_id()
{ {
// connect to the database // connect to the database
//$link = $this->connect(); //$link = $this->connect();
// Get the ID generated from the previous INSERT operation // Get the ID generated from the previous INSERT operation
$last_id = mysql_insert_id($this->linki); $last_id = mysqli_insert_id($this->db);
// return last ID // return last ID
return $last_id; return $last_id;
} }
function num_fields($result) function num_fields($result)
{ {
$result = mysql_num_fields($result); $result = mysqli_num_fields($result);
return $result; return $result;
} }
// function field_name($result, $index) // function field_name($result, $index)
// { // {
// // query with the return of $result // // query with the return of $result
// $result = mysql_field_name($result, $index); // $result = mysql_field_name($result, $index);
// return $result; // return $result;
// } // }
// //
// function tablename($result, $index) // function tablename($result, $index)
// { // {
// // query with the return of $result // // query with the return of $result
// $result = mysql_tablename($result, $index); // $result = mysql_tablename($result, $index);
// return $result; // return $result;
// } // }
// //
// function list_tables($dbase) // function list_tables($dbase)
// { // {
// $result = mysql_list_tables($dbase); // $result = mysql_list_tables($dbase);
// return $result; // return $result;
// } // }
} }
?> ?>