ÒýÓÃ:mysql_connect(,, );
Unable to connect to the " .
"database server at this time.
ÒýÓÃ:if (! @mysql_select_db("jokes") ) {
echo( "Unable to locate the joke " .
" );
"database at this time.
exit();
}
ÒýÓÃ:$sql = "CREATE TABLE Jokes ( " .
"ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, " .
"JokeText TEXT, " .
"JokeDate DATE NOT NULL " .
")";
if ( mysql_query($sql) ) {
echo("Jokes table successfully created!
");
} else {
echo("Error creating Jokes table: " .
");
mysql_error() . "
}
ÒýÓÃ:if ( mysql_query($sql) ) {
echo("Update affected " .
");
mysql_affected_rows() . " rows.
} else {
echo("Error performing update: " .
");
mysql_error() . "
}
Error performing query: " .
mysql_error() . "
" . $row["JokeText"] . "
");ÒýÓÃ:
Our List of Jokes
<£¯HEAD>
$dbcnx = @mysql_connect("localhost",
"root", "mypasswd");
if (!$dbcnx) {
echo( "Unable to connect to the " .
" );
"database server at this time.
exit();
}
// Select the jokes database
if (! @mysql_select_db("jokes") ) {
echo( "Unable to locate the joke " .
" );
"database at this time.
exit();
}
?>Here are all the jokes in our database:
// Request the text of all the jokes
$result = mysql_query(
"SELECT JokeText FROM Jokes");
if (!$result) {
echo("Error performing query: " .
");
mysql_error() . "
exit();
}
// Display the text of each joke in a paragraph
while ( $row = mysql_fetch_array($result) ) {
echo("" . $row["JokeText"] . "
");
}
?>
ÒýÓÃ:
ÒýÓÃ:if ("SUBMIT" == $submitjoke) {
$sql = "INSERT INTO Jokes SET " .
"JokeText='$joketext', " .
"JokeDate=CURDATE()";
if (mysql_query($sql)) {
echo("Your joke has been added.
");
} else {
echo("Error adding submitted joke: " .
");
mysql_error() . "
}
}
ÒýÓÃ:
...
// If the user wants to add a joke
if (isset($addjoke)):
?>
else:
// Connect to the database server
$dbcnx = @mysql_connect("localhost",
"root", "mypasswd");
if (!$dbcnx) {
echo( "Unable to connect to the " .
" );
"database server at this time.
exit();
}
// Select the jokes database
if (! @mysql_select_db("jokes") ) {
echo( "Unable to locate the joke " .
" );
"database at this time.
exit();
}
// If a joke has been submitted,
// add it to the database.
if ("SUBMIT" == $submitjoke) {
$sql = "INSERT INTO Jokes SET " .
"JokeText='$joketext', " .
"JokeDate=CURDATE()";
if (mysql_query($sql)) {
echo("Your joke has been added.
");
} else {
echo("Error adding submitted joke: " .
");
mysql_error() . "
}
}
echo("Here are all the jokes " .
");
"in our database:
// Request the text of all the jokes
$result = mysql_query(
"SELECT JokeText FROM Jokes");
if (!$result) {
echo("Error performing query: " .
");
mysql_error() . "
exit();
}
// Display the text of each joke in a paragraph
while ( $row = mysql_fetch_array($result) ) {
echo("" . $row["JokeText"] . "
");
}
// When clicked, this link will load this page
// with the joke submission form displayed.
echo("");
endif;
?>
ÒýÓÃ:
...
// If the user wants to add a joke
if (isset($addjoke)):
?>
else:
// Connect to the database server
$dbcnx = @mysql_connect(
"localhost", "root", "mypasswd");
if (!$dbcnx) {
echo( "Unable to connect to the " .
" );
"database server at this time.
exit();
}
// Select the jokes database
if (! @mysql_select_db("jokes") ) {
echo( "Unable to locate the joke " .
" );
"database at this time.
exit();
}
// If a joke has been submitted,
// add it to the database.
if ("SUBMIT" == $submitjoke) {
$sql = "INSERT INTO Jokes SET " .
"JokeText='$joketext', " .
"JokeDate=CURDATE()";
if (mysql_query($sql)) {
echo("Your joke has been added.
");
} else {
echo("Error adding submitted joke: " .
");
mysql_error() . "
}
}
// If a joke has been deleted,
// remove it from the database.
if (isset($deletejoke)) {
$sql = "DELETE FROM Jokes " .
"WHERE ID=$deletejoke";
if (mysql_query($sql)) {
echo("The joke has been deleted.
");
} else {
echo("Error deleting joke: " .
");
mysql_error() . "
}
}
echo("Here are all the jokes " .
");
"in our database:
// Request the ID and text of all the jokes
$result = mysql_query(
"SELECT ID, JokeText FROM Jokes");
if (!$result) {
echo("Error performing query: " .
");
mysql_error() . "
exit();
}
// Display the text of each joke in a paragraph
// with a "Delete this Joke" link next to each.
while ( $row = mysql_fetch_array($result) ) {
$jokeid = $row["ID"];
$joketext = $row["JokeText"];
echo("$joketext " .
");
"" .
"Delete this Joke
}
// When clicked, this link will load this page
// with the joke submission form displayed.
echo("");
endif;
?>