Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hello,
 
I'm working through exercise 12.5 and had been getting the following errors:
 
To double check, I downloaded the exercise code and got the same error messages. 
 
1.mysql_close() expects parameter 1 to be resource
mysql_close($dbc);
 
2. Warning: mysql_select_db() expects parameter 2 to be resource

mysql_select_db('myblog', $dbc);
 

 

Any suggestions?

 

Thanks,

K

Link to post
Share on other sites

Hello Hartley,

 

I tried your suggestion.  In fact, I've been messing with this for quite some time now.  I've pasted Larry's code, along with how the MySQL database is set up.  I'm using PHP 5.5.12 and MySQL 5.6.17.   I get the following error:

 

Could not add the entry because:

The query being run was: INSERT INTO entries (entry_id, title, entry, date_entered) VALUES (0, 'test title','test text', NOW())

 

 

Database:

entry_id INT UNSIGNED NOT NULL AUTO_INCREMENT

title VARCHAR(100) NOT NULL

entry TEXT NOT NULL

date_entered DATETIME

 

 

<!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" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Add a Blog Entry</title>
</head>
<body>
<h1>Add a Blog Entry</h1>
<?php // Script 12.5 - add_entry.php
/* This script adds a blog entry to the database. */

if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form.

    // Connect and select:
    $dbc = mysqli_connect('localhost', '**', 'password');
    mysqli_select_db('myblog', $dbc);
    
    // Validate the form data:
    $problem = FALSE;
    if (!empty($_POST['title']) && !empty($_POST['entry'])) {
        $title = trim(strip_tags($_POST['title']));
        $entry = trim(strip_tags($_POST['entry']));
    } else {
        print '<p style="color: red;">Please submit both a title and an entry.</p>';
        $problem = TRUE;
    }

    if (!$problem) {

        // Define the query:
        $query = "INSERT INTO entries (entry_id, title, entry, date_entered) VALUES (0, '$title', '$entry', NOW())";
        
        // Execute the query:
        if (@mysqli_query($query, $dbc)) {
            print '<p>The blog entry has been added!</p>';
        } else {
            print '<p style="color: red;">Could not add the entry because:<br />' . mysqli_error($dbc) . '.</p><p>The query being run was: ' . $query . '</p>';
        }
    
    } // No problem!

    mysqli_close($dbc); // Close the connection.
    
} // End of form submission IF.

// Display the form:
?>
<form action="add_entry.php" method="post">
    <p>Entry Title: <input type="text" name="title" size="40" maxsize="100" /></p>
    <p>Entry Text: <textarea name="entry" cols="40" rows="5"></textarea></p>
    <input type="submit" name="submit" value="Post This Entry!" />
</form>
</body>
</html>

Link to post
Share on other sites

SOLVED.

 

OK...in the book, on page 354, line 35 of the code it is written: if(@mysql_query($query, $dbc)).

 

It should be if(@mysqli_query($dbc, $query)).  I made this change after reading more about mysqli_query.  Apparently the syntax is mysqli_query(connection,query,resultmode).

 

Thanks for the assistance though.

K

  • Upvote 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...