Einfache MySQL-Klasse
Mit dieser MySQL-Klasse können Sie ganz einfach mit MySQL umgehen.
Hallo Benutzer!
Ich stelle dir gerne diese MySQL-Klasse zuverfügung! Viel spaß damit:D
Hallo Benutzer!
Ich stelle dir gerne diese MySQL-Klasse zuverfügung! Viel spaß damit:D
database.class.php
<?php
class database
{
/**
* MySQL Server normalerweise "localhost"
*
* @access private
* @var string
*/
private $db_host = 'localhost';
/**
* MySQL User bsp "root"
*
* @access private
* @var string
*/
private $db_user = 'root';
/**
* MySQL Kennwort
* @access private
* @var string
*/
private $db_pass;
/**
* MySQL Datenbank
*
* @access private
* @var string
*/
private $db_base;
/**
* MySQL query
*
* @access public
* @var string
*/
public $sql_injection;
/**
* MySQL-connect
*
* @access protected
* @var string
*/
protected $link;
/**
* MySQL-select db
*
* @access protected
* @var string
*/
protected $select;
/**
* Database : Construct, verbindung zum Server aufbauen
* @param string $host DB-HOST
* @param string $user DB-USER
* @param string $pass DB-PASS
* @param string $base DB-NAME
*/
public function __construct($host='', $user='', $pass='', $base='') {
if (!empty($host) && !empty($user) && !empty($base)) {
$this->db_host = trim($host);
$this->db_user = trim($user);
$this->db_pass = trim($pass);
$this->db_base = trim($base);
$this->link = mysql_connect($this->db_host, $this->db_user, $this->db_pass) or die(self::print_error("Verbindungs fehler"));
$this->select = mysql_select_db($this->db_base, $this->link);
}else {
self::print_error("MySQL - Verbindung kann nicht hergestellt werden {Connect daten nicht komplett ausgefüllt");
}
}
/**
* mysql_query()
* @param string $result result
* @param string $line Fehler line
* @return string mysql_query($result)
*/
public function query($result='', $line=0) {
$this->sql_injection = mysql_query($result) or die(self::print_error("query is false", $line));
return($this->sql_injection);
}
/**
* mysql_fetch_array()
* @param string $result result
* @return string mysql_fetch_array($result)
*/
public function result_array($result='', $assoc=MYSQL_ASSOC) {
$row = mysql_fetch_array($result);
return($row);
}
/**
* mysql_fetch_assoc()
* @param string $result result
* @return string mysql_fetch_array($result)
*/
public function result_assoc($result='', $assoc=MYSQL_ASSOC) {
$row = mysql_fetch_array($result, $assoc);
return($row);
}
/**
* mysql_fetch_object()
* @param string $result result
* @return string mysql_fetch_object($result)
*/
public function result_object($result='') {
$row = mysql_fetch_object($result);
return($row);
}
/**
* mysql_num_rows()
* @param string $result result
* @return string mysql_num_rows($result)
*/
public function result_counts($result='') {
$row = mysql_num_rows($result);
return($row);
}
/**
* mysql_insert_id() Letzte Insert kennung ausgeben
* @return ressourcen
*/
public function last_insert() {
$row = mysql_insert_id();
return($row);
}
/**
* mysql_close() Verbindung beenden
* @return ressourcen
*/
public function connect_close() {
mysql_close($this->select);
}
private function print_error($msg, $line=0) {
$error = "<h3>$msg</h3><hr />";
$error .= "SQL error in $line <br />";
$error .= "result = ".mysql_error();
$error .= "<br />";
die($error);
}
}
?>
index.php
<?php
include("database.class.php");
$db = new database("localhost", "root", "", "php");
//---- mysql_fetch_assoc verwenden
$res = $db->query("SELECT `name` FROM `news`", __LINE__);
$row = $db->result_assoc($res);
print($row["name"]);
//---- mysql_fetch_assoc mit MYSQL_NUM verwenden
$res = $db->query("SELECT `name` FROM `news`", __LINE__);
$row = $db->result_assoc($res, MYSQL_NUM);
print($row);
//---- mysql_fetch_array verwenden
$res = $db->query("SELECT `name` FROM `news`", __LINE__);
$row = $db->result_array($res);
print($row["name"]);
//---- mysql_fetch_object verwenden
$res = $db->query("SELECT `name` FROM `news`", __LINE__);
$row = $db->result_object($res);
print($row->name);
//---- mysql_num_rows verwenden
$res = $db->query("SELECT `id` FROM `news`", __LINE__);
print($db->result_counts($res));
//---- mysql_insert_id verwenden
$res = $db->last_insert();
print($res);
//---- mysql_close verwenden
$db->connect_close();
?>
Bitte markiere auch die Kommentare, die zur Lösung des Beitrags beigetragen haben
Content-ID: 121924
Url: https://administrator.de/knowledge/einfache-mysql-klasse-121924.html
Ausgedruckt am: 23.12.2024 um 05:12 Uhr
3 Kommentare
Neuester Kommentar