No database selected...

RWBRWB Icrontian
edited August 2007 in Internet & Media
Anyone mind telling me what I did wrong here? It appears to be something with the $result as that's where the error is generated.

[PHP]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml&quot; xml:lang="en" lang="en">

<html>
<head>
<title>The Internet Joke Database</title>
****** http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>

<?php if (isset($_GET)):
?>

<form action="<?php echo $_SERVER; ?>" method="post'>
<label>Type you joke here:<br />
<textarea name="joketext' rows="10" cols="40">
</textarea></label><br />
<input type="submit" value="SUBMIT" />
</form>

<?php else: // Default page display

// Connect to the database server
$dbcnx = @mysql_connect('localhost', '*', '*');
if (!$dbcnx) {
exit ('<p>Unable to connect to the database server at this time.</p>');
}

// If a joke has been submitted, add it to the DB
if (isset($_POST)) {
$joketext = $_POST;
$sql = "INSERT INTO joke SET
joketext='$joketext',
jokedate=CURDATE()";
if (@mysql_query($sql)) {
echo '<p>Your joke has been added.</p>';
} else {
echo '<p>Error adding submitted joke: ' . mysql_error() . '</p>';
}
}

echo '<p>Here are all the jokes in our database:</p>';

// Request the text of all the joke
$result = @mysql_query('SELECT joketext FROM joke');
if (!$result) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}

// Display the text of each joke in a paragraph
while ($row = mysql_fetch_array($result)) {
echo '<p>' . $row . '</p>';
}

// When clicked, this link will load this page with the joke submission form displayed.
echo '<p><a href="' . $_SERVER . '?addjoke=1">Add a Joke!</a></p>';

endif;
?>

</body>
</html>[/PHP]

Comments

  • RWBRWB Icrontian
    edited August 2007
    I figured it out, once again.... I search and search and it's not until mere seconds after asking for help that I find it. WTF is that about.... I knew I needed to select the DB somehow, but didn't realize where or how...

    [php]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
    <html xmlns="http://www.w3.org/1999/xhtml&quot; xml:lang="en" lang="en">

    <html>
    <head>
    <title>The Internet Joke Database</title>
    ****** http-equiv="content-type" content="text/html; charset=utf-8" />
    </head>
    <body>

    <?php if (isset($_GET)):
    ?>

    <form action="<?php echo $_SERVER; ?>" method="post'>
    <label>Type you joke here:<br />
    <textarea name="joketext' rows="10" cols="40">
    </textarea></label><br />
    <input type="submit" value="SUBMIT" />
    </form>

    <?php else: // Default page display

    // Connect to the database server
    $dbcnx = @mysql_connect('localhost', '*', '*');
    if (!$dbcnx) {
    exit ('<p>Unable to connect to the database server at this time.</p>');
    }

    // Select the jokes database
    if (!@mysql_select_db('ijdb')) {
    exit('<p>Unable to locate the joke database at this time.</p>');
    }

    // If a joke has been submitted, add it to the DB
    if (isset($_POST)) {
    $joketext = $_POST;
    $sql = "INSERT INTO joke SET
    joketext='$joketext',
    jokedate=CURDATE()";
    if (@mysql_query($sql)) {
    echo '<p>Your joke has been added.</p>';
    } else {
    echo '<p>Error adding submitted joke: ' . mysql_error() . '</p>';
    }
    }

    echo '<p>Here are all the jokes in our database:</p>';

    // Request the text of all the joke
    $result = @mysql_query('SELECT joketext FROM joke');
    if (!$result) {
    exit('<p>Error performing query: ' . mysql_error() . '</p>');
    }

    // Display the text of each joke in a paragraph
    while ($row = mysql_fetch_array($result)) {
    echo '<p>' . $row . '</p>';
    }

    // When clicked, this link will load this page with the joke submission form displayed.
    echo '<p><a href="' . $_SERVER . '?addjoke=1">Add a Joke!</a></p>';

    endif;
    ?>

    </body>
    </html>
    [/php]
Sign In or Register to comment.