Da dein Verbindungsaufbau zu unsicher ist, was daran liegt, dass du, wie du selbst sagst, wenig Erfahrung in diesem Bereich vorweisen kannst, möchte ich dir an dieser Stelle meine Datenbankklasse, basierend auf MySQLi, zur Verfügung stellen.
Beispiele:
PHP Code:
$this->sql->Escape( $array );
PHP Code:
$this->sql->Send( "SELECT ..." );
Für das einfügen, löschen oder überschreiben von Daten solltest du diese Funktion nutzen, da diese mit der Funktion "Rollback" zusammen arbeitet.
PHP Code:
$this->sql->Send2( "INSERT ..." );
$this->sql->Send2( "UPDATE..." );
$this->sql->Send2( "DELETE..." );
$this->sql->Commit();
Meine Datenbanklasse:
PHP Code:
<?php
class DB
{
private $sql;
private $result;
private $exec;
function __construct( $database = null )
{
$this->sql = @new MySQLi( DB_HOST, DB_USER, DB_PASS, $database == null ? DB_BASE : $database );
$this->sql->autocommit( false );
if( $this->sql->connect_errno )
{
//change
die( "Unable to connect with the database. Error: " . $this->sql->connect_errno . " - " . $this->sql->connect_error);
}
}
public function Escape( $post )
{
foreach($post as $key => $value)
{
$value = htmlspecialchars( $value );
$value = htmlentities( $value );
$value = strip_tags( $value );
$value = $this->sql->escape_string( $value );
}
return $post;
}
public function Send ( $qry )
{
$this->result = DB::Exec( $qry );
if( $this->sql->errno )
{
die( "Error: " . $this->sql->errno . " - " . $this->sql->error);
}
return DB::Result();
}
public function Send2 ( $qry )
{
$this->result = DB::Exec( $qry );
}
public function Commit()
{
if( $this->sql->errno )
{
$this->sql->rollback();
die( "Error: " . $this->sql->errno . " - " . $this->sql->error);
}
$this->sql->commit();
}
private function Exec( $qry )
{
$exec = @$this->sql->query( $qry );
return $exec;
}
private function Result()
{
if( $this->result != "1" )
{
$arr = array();
if (!function_exists('mysqli_fetch_all')){
$arr['obj'] = [];
while ($row = $this->result->fetch_assoc()) {
$arr['obj'][] = $row;
}
}else {
$arr['obj'] = $this->result->fetch_all(MYSQLI_ASSOC);
}
$arr['rows'] = $this->result->num_rows;
return $arr;
}
else
{
return 3;
}
}
}