Ohne den kompletten Code zu kennen, kann man dir da kaum helfen. Ich habe vor einigen Jahren eine Klasse geschrieben, die sich um das Handling mit MySQL kümmert. Ist zwar auch nicht das Gelbe vom Ei, aber vielleicht hilft dir das weiter, kannst du gerne übernehmen:
<?php
/********************************************************************
*
* File: class.mysql.php
*
* Kurzbeschreibung: Klasse fuer MySQL-Handling
*
* Erstellungsdatum: 17.01.2008
*
* Letzte Aenderung: 04.03.2018
*
* Autor: M.Scatello
*
* Copyright 2018 by M.Scatello
*
********************************************************************/
class Cmysql
{
private $mysqli;
private $debug;
private $log;
private $clearLogFile;
private $filePointer;
//---------------------------------------------------------------
function __construct()
{
$this->mysqli = 0;
$this->debug = false;
$this->log = false;
$this->clearLogFile = false;
$this->filePointer = NULL;
}
//---------------------------------------------------------------
function __destruct()
{
if ($this->filePointer)
fclose($this->filePointer);
}
//---------------------------------------------------------------
function enable_debug()
{
$this->debug = true;
}
//---------------------------------------------------------------
function enable_log($clearLogFile=false)
{
$this->log = true;
$this->clearLogFile = $clearLogFile;
if ($this->clearLogFile)
$this->filePointer = fopen ("mysql.log", "w");
else
$this->filePointer = fopen ("mysql.log", "a+");
}
//---------------------------------------------------------------
function connect_db($hostName, $userName, $pw, $db_name)
{
$this->mysqli = @mysqli_connect($hostName, $userName, $pw, $db_name);
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
return $this->mysqli;
}
//---------------------------------------------------------------
function db_close()
{
if ($this->mysqli)
{
mysqli_close($this->mysqli);
$this->mysqli = 0;
}
}
//---------------------------------------------------------------
function select_db($dbName)
{
$result = false;
if ($this->mysqli)
$result = mysqli_select_db($this->mysqli, $dbname);
if (! result && $this->debug)
die ("MySQL-Error: " . $this->error_text());
if ($this->log)
$this->logging("Select DB: $dbName");
return $result;
}
//---------------------------------------------------------------
function real_escape_string($value)
{
$result = $value;
if ($this->mysqli)
$result = mysqli_real_escape_string($this->mysqli,$value);
return $result;
}
//---------------------------------------------------------------
function query($query="")
{
$result = false;
if (strlen ($query) && $this->mysqli)
$result = @mysqli_query ($this->mysqli,$query);
if (! $result && $this->debug)
die ("MySQL-Error: <br>Query: $query<br>" . $this->error_text());
if ($this->log)
{
if (strpos(strtolower($query), "select") !== false)
$treffer = @mysqli_num_rows($result);
else
$treffer = @mysqli_affected_rows($this->mysqli);
$this->logging("Query: $query\nTreffer: $treffer");
}
return $result;
}
//---------------------------------------------------------------
function num_fields($value=0)
{
$result = 0;
if ($this->mysqli && $value)
$result = @mysqli_num_fields ($value);
return $result;
}
//---------------------------------------------------------------
function get_field_list_from_table($table)
{
$result = array();
$query = "SHOW FIELDS from `$table`";
$data = $this->query($query);
while ($row = $this->fetch_row($data))
$result[] = $row[0];
return $result;
}
//---------------------------------------------------------------
function num_rows($value)
{
$result = 0;
if ($this->mysqli && $value)
$result = @mysqli_num_rows ($value);
return $result;
}
//---------------------------------------------------------------
function has_error()
{
$result = true;
if ($this->mysqli)
$result = @mysqli_errno($this->mysqli);
return $result;
}
//---------------------------------------------------------------
function error_text()
{
$result = false;
if ($this->mysqli)
$result = @mysqli_error($this->mysqli);
return $result;
}
//---------------------------------------------------------------
function data_seek($value=0, $position=-1)
{
$result = false;
if ($value && $position >= 0 && $this->mysqli)
$result = mysqli_data_seek($value, $position);
return $result;
}
//---------------------------------------------------------------
function fetch_array($value=0)
{
$result = false;
if ($value && $this->mysqli)
$result = @mysqli_fetch_array($value);
return $result;
}
//---------------------------------------------------------------
function fetch_object($value=0)
{
$result = false;
if ($value && $this->mysqli)
$result = @mysqli_fetch_object($value);
return $result;
}
//---------------------------------------------------------------
function fetch_row($value=0)
{
$result = false;
if ($value && $this->mysqli)
$result = @mysqli_fetch_row($value);
return $result;
}
//---------------------------------------------------------------
function fetch_all($value=0)
{
$result = false;
if ($value && $this->mysqli)
$result = @mysqli_fetch_all($value, MYSQLI_ASSOC);
return $result;
}
//---------------------------------------------------------------
function fetch_assoc($value=0)
{
$result = false;
if ($value && $this->mysqli)
$result = @mysqli_fetch_assoc($value);
return $result;
}
//---------------------------------------------------------------
function result($value=0, $num=-1)
{
$result = false;
if ($value && $num >= 0 && $this->mysqli)
{
mysqli_data_seek($value,$num);
$datarow = mysqli_fetch_array($value);
$result = $datarow[$num];
}
return $result;
}
//---------------------------------------------------------------
function inserted_id()
{
$result = false;
if ($this->mysqli)
$result = mysqli_insert_id($this->mysqli);
return $result;
}
//---------------------------------------------------------------
function check_error()
{
if ($this->mysqli)
{
if (mysqli_errno ($this->mysqli))
die ("Datenbankfehler: " . mysqli_error($this->mysqli));
}
}
//---------------------------------------------------------------
function logging($data)
{
if ($this->filePointer)
{
fputs ($this->filePointer, date("d.m.Y - H:i:s") . "\n");
fputs ($this->filePointer, $data . "\n");
fputs ($this->filePointer, "-------------------------------------------------------------------------------------" . "\n");
}
}
//---------------------------------------------------------------
function export_record($table, $id)
{
$result = false;
$columns = $this->get_field_list_from_table($table);
$query = "Select " . implode(", ", $columns) . " from `$table` where `id`=$id";
$result = $this->query($query);
if ($this->has_error())
return $result;
$data = $this->fetch_assoc($result);
$columns = array();
foreach ($data as $key => $value)
$columns[] = "`$key`='$value'";
$output = implode(",", $columns);
$fp = @fopen ("export.txt", "a+");
if ($fp)
{
fputs($fp, $output . "\n");
fclose($fp);
$result = true;
}
return $result;
}
}
?>
Alles anzeigen