PHP-Projekt/Testdatenbank: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Michi (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung |
Michi (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung |
||
Zeile 15: | Zeile 15: | ||
! colspan=2 | database.md.base.inc.php | ! colspan=2 | database.md.base.inc.php | ||
|- | |- | ||
| | | Erfordert || server.mysql.inc.php | ||
|- | |- | ||
| Bereitgestellte Variablen || | | Bereitgestellte Variablen || | ||
Zeile 79: | Zeile 79: | ||
* $mysqlpassword | * $mysqlpassword | ||
* $mysqlserver | * $mysqlserver | ||
|- | |||
| colspan=2 | | |||
<pre class=wiki> | |||
<?php | |||
## Name server.mysql.inc.php | |||
## Description Basic MySQL input and output functions | |||
### FUNCTIONS ### | |||
## Connecting with MySQL, activating database "md"; in case of a | |||
## connection error, show a complete HTML document with a short error message | |||
function mysqlconnection($mysqldatabase,$passwordfile) { | |||
## In order to increase the security of this project, | |||
## move ...password.inc.php to another directory which is protected against | |||
## general download by .htaccess and change the include line accordingly | |||
require($passwordfile); | |||
$connection = mysql_connect($mysqlserver, $mysqluser, $mysqlpassword); | |||
if ($connection) { | |||
mysql_select_db($mysqldatabase); | |||
return $connection; | |||
} | |||
else { | |||
echo "<html>"; | |||
echo "<head>"; | |||
echo "<title>No connection</title>"; | |||
echo "</head>"; | |||
echo "<body>"; | |||
echo "<p>No connection to database</p>"; | |||
echo "</body>"; | |||
echo "</html>"; | |||
exit(); | |||
} | |||
} | |||
function mysqldie($errortext) | |||
{ | |||
$message = mysql_error(); | |||
$number = mysql_errno(); | |||
return "[{$errortext}] ( {$number} : {$message} )<br />\n"; | |||
exit; | |||
} | |||
function mysqlinsert($tarray) { | |||
## number of insert fields | |||
$noif = count($tarray["insert"]); | |||
$insert = "insert into {$tarray["t"]} ("; | |||
for ($i = 0; $i < $noif; $i++) { | |||
$insert .= $tarray["fname"][$tarray["insert"][$i]]; | |||
if ($i < $noif - 1) { | |||
$insert .= ","; | |||
} | |||
} | |||
$insert .= ") values ("; | |||
for ($i = 0; $i < $noif; $i++) { | |||
$insert .= "'{$tarray["fvalue"][$tarray["insert"][$i]]}'"; | |||
if ($i < $noif - 1) { | |||
$insert .= ","; | |||
} | |||
} | |||
$insert .= ")"; | |||
return $insert; | |||
} | |||
function mysqlselect($resultfields,$tarray) { | |||
## number of clauses | |||
$noc = count($tarray["clause"]); | |||
## number of select fields | |||
$nosf = count($resultfields); | |||
$select = "select "; | |||
for ($i = 0; $i < $nosf; $i++) { | |||
$select .= $tarray["fname"][$resultfields[$i]]; | |||
if ($i < $nosf - 1) { | |||
$select .= ","; | |||
} | |||
} | |||
$select .= " from {$tarray["t"]} where "; | |||
for ($i = 0; $i < $noc; $i++) { | |||
$select .= $tarray["fname"][$tarray["clause"][$i]]; | |||
$select .= " = '{$tarray["fvalue"][$tarray["clause"][$i]]}'"; | |||
if ($i < $noc - 1) { | |||
$select .= " and "; | |||
} | |||
} | |||
return $select; | |||
} | |||
?> | |||
</pre> | |||
|} | |} | ||
Apache-Errorlog unter "/var/log/apache2/error.log" | Apache-Errorlog unter "/var/log/apache2/error.log" |
Version vom 31. März 2009, 20:25 Uhr
Auf Rechner "kanzler".
Anlegen der Datenbank "md".
$ mysql -u root -p mysql> create database md; mysql> quit
Anlegen Verzeichnis mit PHP-Skripten.
$ cd /WWW $ sudo mkdir md && sudo chown mik:mik md
database.md.base.inc.php | |
---|---|
Erfordert | server.mysql.inc.php |
Bereitgestellte Variablen |
|
Verwendete Funktionen |
|
<?php ### Name database.md.base.inc.php ### Description Standard variables and functions with database "md" require("server.mysql.inc.php"); ### VARIABLES ### $database = "md"; $connection = mysqlconnection("{$database}", "database.{$database}.password.inc.php") or mysqldie("Unable to connect"); ?> | |
database.md.password.inc.php | |
Diese Datei sollte später in ein anderes Verzeichnis verschoben werden, das mit ".htaccess" gegen allgemeines Herunterladen geschützt ist. Die include-Zeilen in den anderen Dateien müssen dann entsprechend angepasst werden. | |
Bereitgestellte Variablen |
|
<?php ### Name database.md.password.inc.php ### Description ### VARIABLES ### $mysqluser = "root"; # MySQL user $mysqlpassword = "xxxxxx"; # password $mysqlserver = "kanzler"; # name of host on which MySQL is running ?> | |
server.mysql.inc.php | |
Bereitgestellte Funktionen |
|
Verwendete Variablen |
|
<?php ## Name server.mysql.inc.php ## Description Basic MySQL input and output functions ### FUNCTIONS ### ## Connecting with MySQL, activating database "md"; in case of a ## connection error, show a complete HTML document with a short error message function mysqlconnection($mysqldatabase,$passwordfile) { ## In order to increase the security of this project, ## move ...password.inc.php to another directory which is protected against ## general download by .htaccess and change the include line accordingly require($passwordfile); $connection = mysql_connect($mysqlserver, $mysqluser, $mysqlpassword); if ($connection) { mysql_select_db($mysqldatabase); return $connection; } else { echo "<html>"; echo "<head>"; echo "<title>No connection</title>"; echo "</head>"; echo "<body>"; echo "<p>No connection to database</p>"; echo "</body>"; echo "</html>"; exit(); } } function mysqldie($errortext) { $message = mysql_error(); $number = mysql_errno(); return "[{$errortext}] ( {$number} : {$message} )<br />\n"; exit; } function mysqlinsert($tarray) { ## number of insert fields $noif = count($tarray["insert"]); $insert = "insert into {$tarray["t"]} ("; for ($i = 0; $i < $noif; $i++) { $insert .= $tarray["fname"][$tarray["insert"][$i]]; if ($i < $noif - 1) { $insert .= ","; } } $insert .= ") values ("; for ($i = 0; $i < $noif; $i++) { $insert .= "'{$tarray["fvalue"][$tarray["insert"][$i]]}'"; if ($i < $noif - 1) { $insert .= ","; } } $insert .= ")"; return $insert; } function mysqlselect($resultfields,$tarray) { ## number of clauses $noc = count($tarray["clause"]); ## number of select fields $nosf = count($resultfields); $select = "select "; for ($i = 0; $i < $nosf; $i++) { $select .= $tarray["fname"][$resultfields[$i]]; if ($i < $nosf - 1) { $select .= ","; } } $select .= " from {$tarray["t"]} where "; for ($i = 0; $i < $noc; $i++) { $select .= $tarray["fname"][$tarray["clause"][$i]]; $select .= " = '{$tarray["fvalue"][$tarray["clause"][$i]]}'"; if ($i < $noc - 1) { $select .= " and "; } } return $select; } ?> |
Apache-Errorlog unter "/var/log/apache2/error.log"