Hi,
Ich hab mal an einer etwas eleganteren Möglichkeit eine Verbindung zu einem mysql server herzustellen. Mit funktionen. Meine Funktion liefert eine ressource zurück die sie aus einer datei ( config.php) bezieht (include_once()).
Leider schiesst das ding quer und als fehler wird angegeben:
Code:
Konnte Datenbank nicht auswählen: No Database Selected
Die config.php :
Code:
<?php
$mysql_host="localhost";
$mysql_name="richard";
$mysql_pw="xxx";
$mysql_db="richard";
?>
die functions.php :
Code:
<?php
function connect_to_db() {
include_once( "config.php" );
$res=mysql_connect( $mysql_host, $mysql_name, $mysql_pw );
if( ! $res ) {
die( "Konnte Verbindung zum Datenbankserver nicht herstellen: ".mysql_error() );
return( false );
}
if( ! mysql_select_db( $mysql_db ) ) {
die( "Konnte Datenbank nicht auswählen: ".mysql_error() );
return( false );
}
return( $res );
}
function myquery( $query, $res ) {
include_once( "config.php" );
if( $query == "" ) {
die( "Leerer Query" );
}
if( ! is_ressource( $res ) ) {
die( "Fehlerhafte Ressource" );
}
if( ! $result = mysql_query( $query, $res ) ) {
die( "Konnte Query nich ausführen: ".mysql_error() );
}
else {
return( $result );
}
}
?>
und einer der codes in denen die funktionen benutzt werden.
install.php :
Code:
<?php
include_once( "./includes/config.php" );
include_once( "./includes/admin_config.php" );
include_once( "./includes/functions.php" );
$query1="
CREATE TABLE `news` (
`news_id` INT NOT NULL AUTO_INCREMENT ,
`title` VARCHAR( 255 ) NOT NULL ,
`content` TEXT NOT NULL ,
`nick` VARCHAR( 255 ) NOT NULL ,
`date` BIGINT NOT NULL ,
`views` INT NOT NULL ,
PRIMARY KEY ( `news_id` )
);
";
$query2="
CREATE TABLE `members` (
`user_id` INT NOT NULL AUTO_INCREMENT ,
`nick` VARCHAR( 255 ) NOT NULL ,
`email` VARCHAR( 255 ) NOT NULL ,
`pw` VARCHAR( 255 ) NOT NULL ,
`admin` ENUM( '1', '0' ) NOT NULL
PRIMARY KEY ( `user_id` )
);
";
$query3="INSERT INTO members ( user_id, nick, email, pw, admin ) VALUES ( '', '".$admin_nick."', '".$admin_email."', '".$admin_pw."', '1' )";
$res=connect_to_db();
myquery( $query1, $res );
myquery( $query2, $res );
myquery( $query3, $res );
print "Installation abgeschlossen, bitte löschen sie nun die admin_config.php";
mysql_close( $res );
?>
Warscheinlich ist der Fehler so dämlich das es mir noch jahrelang peinlich ist aber ich find ihn einfach net