so Olde,
ich habe für dich mal ein beispiel gebastelt, was mir schon nen bissel Zeit gekostet hat
1. Erstelle eine neue PHP Datei mit dem Namen
db.Class.php
<?php
function escape($query)
{
return mysql_escape_string($query);
}
class DB_Result
{
var $_result;
function DB_Result($result)
{
$this->_result = $result;
}
function FetchArray()
{
return @mysql_fetch_array($this->_result);
}
function dataseek() {
return @mysql_data_seek($this->_result);
}
function fetchrow_assoc() {
return @mysql_fetch_assoc($this->_result);
}
function FetchAssocArray()
{
return @mysql_fetch_assoc($this->_result);
}
function FetchObject()
{
return mysql_fetch_object($this->_result);
}
function FetchRow()
{
return @mysql_fetch_object($this->_result);
}
function NumRows()
{
return @mysql_num_rows($this->_result);
}
function smysql_version() {
return mysql_get_server_info($this->_result);
}
function NumFields()
{
return mysql_num_fields($this->_result);
}
function FieldName($i)
{
return mysql_field_name($this->_result, $i);
}
function close()
{
$r = @mysql_free_result($this->_result);
unset($this);
return $r;
}
}
class DB
{
var $_handle;
function DB($host, $user, $pass, $db)
{
$this->_handle = @mysql_connect($host, $user, $pass);
if(!$this->_handle)
{
$this->Error('connect');
return false;
}
if(!@mysql_select_db($db, $this->_handle))
{
$this->Error('selectdb');
return false;
}
return true;
}
function Query($query)
{
$res = @mysql_query($query, $this->_handle);
if(!$res)
{
$this->Error('query', $query);
}
return new DB_Result($res);
}
function Error($type, $query = '')
{
global $pref;
$error = mysql_error();
$errno = mysql_errno();
if($errno != 1091 && $errno != 1050 && $errno != 1062)
{
die("MySQL Error! "."\nQuery\n".addslashes($query)."\n\nError\n".addslashes($error));
}
return true;
}
function Disconnect()
{
mysql_close($this->_handle);
}
}
?>
2. Und nun die
daten.php ( muss so heißen ! )
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Daten</title>
<style type="text/css">
<!--
body {
margin:0px;
background:#141414;
font-size:10px;
font-family:Arial, Helvetica, sans-serif;
}
.daten {
width:502px;
margin:5px;
float:left;
padding:1px;
background-color:#00CCFF;
border:1px solid #3e5e84;
}
.daten_read {
width:494px;
height:394px;
float:left;
margin-left:5px;
margin-top:5px;
padding:3px;
background-color:#00CCFF;
border:1px solid #3e5e84;
}
.daten ul{
padding:0px;
margin: 0px 0px 0px 0px;
list-style-type:none;
border:1px solid #3e5e84;
background:#66FFFF;
width:500px;
}
.daten ul li {
background-color:#00CCFF;
border: 1px dashed #333300;
margin: 1px;
padding: 2px 0px 2px 5px;
}
.daten ul li a {
color: #000000;
text-decoration: none;
}
.daten ul li:hover{
font-size:12px;
}
-->
</style>
</head>
<body>
<?php
require_once('db.Class.php');
$tabellenname = "DATEN????????";
$dbhost = " ";
$dbuser = " ";
$dbpass = " ";
$dbname = " ";
$db = new DB($dbhost, $dbuser, $dbpass, $dbname);
switch($_REQUEST['do'])
{
case 'read' :
echo '<div class="daten_read">';
if ( is_numeric($_REQUEST['id']) ) {
$id = escape($_REQUEST['id']);
$abf = "SELECT text
FROM $tabellenname
WHERE id = '".$id."'";
$erg = $db->Query($abf);
$row = $erg->fetchrow();
if ($erg->NumRows() > 0) {
echo $row->text;
} else {
echo 'Kein eintrag gefunden !'."\n";
}
} else {
echo 'ID ist keine Zahl!'."\n";
}
echo '</div>';
break;
default :
echo '<div class="daten"><ul>'."\n";
$abf = 'SELECT id, title
FROM '.$tabellenname.'
ORDER BY id DESC
LIMIT 0,30';
$erg = $db->Query($abf);
while ($row = $erg->FetchAssocArray()) {
echo '<li><a href="http://'.$_SERVER['SERVER_NAME'].'/daten.php?do=read&id='.$row['id'].'">'.$row['title'].'</a></li>'."\n";
}
echo '</ul></div>'."\n";
break;
}
?>
</body>
</html>
Beide Files müssen in dem
selben Ordner sein.
Jetzt musst du natürlich noch die Variablen anpassen...( DB name, DB Host, DB PW und den $tabellenname
Als Spalten habe ich
id, title und
text genommen
Wenn du noch fragen hast...frage einfach
MfG OloX
Zuletzt modifiziert von OloX am 10.10.2008 - 23:38:24