19. december 2001 - 14:39
#1
Jeg kender ingen steder og jeg vil ikke hjælpe med access.
php giver adgang til den glimrende mysql database så hvorfor bekymre sig om benytte dem hamrende langsomme access som iøvrigt er mere besværlig at benytte da php ikke har indbyggede funktioner til den?
Af samme årsag tror jeg ikke at der er nogen som har gidet at skrive en tutorial om det...
19. december 2001 - 14:46
#4
Ved ikke hvordan access bruges, men jeg har skrevet et udkast til et tutorial om hvordan man lavet et debatforum til PHOP/MySQL. Det er langtfra færdigt, men det skulle give dig en ide om hvordan det kan gøres. Andre er velkomne til at kommentere hvad jeg har skrevet indtil videre.
Først kommer filen forum.inc.php der indeholder alle de funktioner jeg skal bruge for at lave et forum.
--------------- forum.inc.php
<?PHP
require (\"dbconnect.inc\");
// Example use
//createTopic(\'headline of your topic\', \'The actual text you write inside the topic\', \'Your name\');
//createTopic(\'Need examples of PHP\', \'Hi everyone. I need some examples of how to learn to program in PHP. Can you give me some nice links. English is preferred. Thanks in advance.\', \'the_newbie\');
/*
* Use this method to show all topics with links to view the topics.
*/
function showTopicList() {
// Get the actual messages from the database
$query = \"SELECT id, author, topic FROM forum_topics\";
$result = mysql_query($query) or die(\"Could not show topics\");
if ($row = mysql_fetch_array($result)) {
// Make the table
echo \"showing all topics\";
echo \"<TABLE BORDER=1 WIDTH=400><tr>
<td valign=\\\"top\\\">Topic</td>
<td valign=\\\"top\\\">Author</td>
</tr>\";
// List alle the headlines
do
{
$topic = $row[\"topic\"];
$author = $row[\"author\"];
$topic_id = $row[\"id\"];
// Convert the breaks to <BR> tags to show the breaks in the html as they were written to the database
// Show the message in a table
echo \"<tr>
<td valign=\\\"top\\\"><a href=\\\"read_topic.php?topic_id=$topic_id\\\">$topic</a></td>
<td valign=\\\"top\\\">$author</td>
</tr>\";
}
while ($row = mysql_fetch_array($result));
// Finish the table
echo \"</table>\";
}
}
/*
* Use this method to show ale the messages in a topic. You\'ll need to know which topic_id the topic has in advance.
*/
function showTopic($topic_id) {
$query = \"SELECT author, topic FROM forum_topics WHERE id = \'$topic_id\'\";
$result = mysql_query($query) or die(\"Could find topic\");
// Get the topic headline and author from the database and show it above the table
if ($row = mysql_fetch_array($result)) {
$topic = $row[\"topic\"];
$author = $row[\"author\"];
}
// Get the actual messages from the database
$query = \"SELECT author, message FROM forum_messages WHERE topic_id = \'$topic_id\'\";
$result = mysql_query($query) or die(\"Could not show topic\");
if ($row = mysql_fetch_array($result)) {
// Make the table
echo \"showing \\\"$topic\\\" by $author<br>\";
echo \"<TABLE BORDER=1 WIDTH=400><tr>
<td valign=\\\"top\\\">Author</td>
<td valign=\\\"top\\\">Message</td>
</tr>\";
// List alle the messages in the topic
do
{
$author = $row[\"author\"];
// Convert the breaks to <BR> tags to show the breaks in the html as they were written to the database
$message = nl2br($row[\"message\"]);
// Show the message in a table
echo \"<tr>
<td valign=\\\"top\\\">$author</td>
<td valign=\\\"top\\\">$message</td>
</tr>\";
}
while ($row = mysql_fetch_array($result));
// Finish the table
echo \"</table>\";
}
}
/*
* Use this method to answer an existing topic. YOu\'ll need to know which topic_id the topic has in advance.
*/
function answerTopic($topic_id, $message, $author) {
$query = \"INSERT INTO forum_messages (topic_id, message, author) VALUES (\'$topic_id\', \'$message\', \'$author\')\";
$result = mysql_query($query) or die(\"Could not create an answer to the topic\");
}
/*
* Use this method to create a new topic.
*/
function createTopic($topic, $message, $author) {
// First we create the topic in the topics table and get the created id from the database
$query = \"INSERT INTO forum_topics (topic, author) VALUES (\'$topic\', \'$author\')\";
$result = mysql_query($query) or die(\"Could not create new topic\");
$new_topic_id = mysql_insert_id();
// Then we use the returned id to write the first message in the topic
answerTopic($new_topic_id, $message, $author);
}
?>
--------------- forum.inc.php
Dernæst kan du bruge filen forum.php, der lister topics og lader dig oprette et nyt topic.
--------------- forum.php
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">
<html>
<head>
<title>Main page - forum</title>
</head>
<body>
<?PHP
// include our forum functions
require(\"forum.inc.php\");
showTopicList();
?>
<form action=\"create_topic.php\" method=\"POST\">
Enter your name<br>
<input type=\"text\" name=\"author\"><br>
Topic description<br>
<input type=\"text\" name=\"topic\"><br>
Message<br>
<TEXTAREA name=\"message\" cols=\"50\" rows=\"10\">
</TEXTAREA><br>
<input type=\"submit\" value=\"Create topic\" name=\"submit\">
</form>
</body>
</html>
--------------- forum.php
19. december 2001 - 14:47
#5
dbconnect.php er selvfølgelig den fil du bruger til at oprette forbindelse til din db :-)
Min egen ser nogenlunde sådan her ud!:
<?PHP
$server = \"localhost\";
$bruger = \"root\";
$kodeord = \"***\";
$database = \"dindatabase\";
if (!mysql_connect(\"$server\",\"$bruger\",\"$kodeord\")) {
udskriv_fejl(\"Kunne ikke oprette en forbindelse til MySQL.\"); }
if(!mysql_select_db(\"$database\")) {
udskriv_fejl(\"Kunne ikke vælge databasen: $database\"); }
?>