Jump to content
Larry Ullman's Book Forums

Catchable Fatal Error In Chapter 8 Script 8.3


Recommended Posts

Good day, nice book! I ve been getting along fine with it up to chapter 8. At first i wrote the codes then cross checked it with yours and ran it after clicking register on my web browser i get this error:

catchable fatal error: object of class mysqli_result could not be converted to string in c:\wamp\www\tutorials\mysqli_connect.php on line 17

 

i copied your script from the downloads and replaced my script but still same error dont know what to do. I checked my database to see if the name was included but nada. Pls help me. I m using wamp and my mysqli version is 5.5.20

Link to comment
Share on other sites

thanks for replying back at such short notice.

here is the code i tried earlier.

<?php # Script 8.3 - register.php

$page_title = 'Register';
include ('/includes/header.html');

// Check if the form has been submitted:
if (isset($_POST['submitted'])) {

    $errors = array(); // Initialize an error array.
    
    // Check for a first name:
    if (empty($_POST['first_name'])) {
        $errors[] = 'You forgot to enter your first name.';
    } else {
        $fn = trim($_POST['first_name']);
    }
    
    // Check for a last name:
    if (empty($_POST['last_name'])) {
        $errors[] = 'You forgot to enter your last name.';
    } else {
        $ln = trim($_POST['last_name']);
    }
    
    // Check for an email address:
    if (empty($_POST['email'])) {
        $errors[] = 'You forgot to enter your email address.';
    } else {
        $e = trim($_POST['email']);
    }
    
    // Check for a password and match against the confirmed password:
    if (!empty($_POST['pass1'])) {
        if ($_POST['pass1'] != $_POST['pass2']) {
            $errors[] = 'Your password did not match the confirmed password.';
        } else {
            $p = trim($_POST['pass1']);
        }
    } else {
        $errors[] = 'You forgot to enter your password.';
    }
    
    if (empty($errors)) { // If everything's OK.
    
        // Register the user in the database...
        
        require_once('../mysqli_connect.php'); // Connect to the db.
        
        // Make the query:
        $q ="INSERT INTO user (first_name, last_name, email, pass, registration_date) VALUES ('$fn', '$ln', '$e', SHA1('$p'), NOW() )" ;
                
        $r = @mysqli_query ($dbc, $q); // Run the query.
        if ($r) { // If it ran OK.
        
            // Print a message:
            echo '<h1>Thank you!</h1>
        <p>You are now registered. In Chapter 11 you will actually be able to log in!</p><p><br /></p>';    
        
        } else { // If it did not run OK.
            
            // Public message:
            echo '<h1>System Error</h1>
            <p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>';
            
            // Debugging message:
            echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';
                        
        } // End of if ($r) IF.
        
        mysqli_close($dbc); // Close the database connection.
        
        // Include the footer and quit the script:
        include ('/includes/footer.html');
        exit();
        
    } else { // Report the errors.
    
        echo '<h1>Error!</h1>
        <p class="error">The following error(s) occurred:<br />';
        foreach ($errors as $msg) { // Print each error.
            echo " - $msg<br />\n";
        }
        echo '</p><p>Please try again.</p><p><br /></p>';
        
    } // End of if (empty($errors)) IF.

} // End of the main Submit conditional.
?>
<link href="includes/style.css" rel="stylesheet" type="text/css" />

<h1>Register</h1>
<form action="register.php" method="post">
    <p>First Name: <input type="text" name="first_name" size="15" maxlength="20" value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>
    <p>Last Name: <input type="text" name="last_name" size="15" maxlength="20" value="<?php if (isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p>
    <p>Email Address: <input type="text" name="email" size="20" maxlength="40" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>"  /> </p>
    <p>Password: <input type="password" name="pass1" size="10" maxlength="20" /></p>
    <p>Confirm Password: <input type="password" name="pass2" size="10" maxlength="20" /></p>
    <p><input type="submit" name="submit" value="Register" /></p>
    <input type="hidden" name="submitted" value="TRUE" />
</form>
<?php
include ('/includes/footer.html');
?>
 

Link to comment
Share on other sites

here it is

<?php $page_title='my dbase connect';

define ('DB_HOST','localhost');
define ('DB_USERNAME','root');
define ('DB_PASSWORD','');
define ('DB_NAME','sitename');
?>
<title><?php echo $page_title ;?></title>

<?php
//conecting to database
$dbc=@mysqli_connect(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME)
        OR die('could not connect to server at this time: '.mysqli_connect_error());
        
$q='select first_name from user limit 3';
$result=mysqli_query($dbc,$q);
echo $result;

?>

Link to comment
Share on other sites

  • 3 weeks later...

yeah i ve read the book. and so i thought maybe the error was not with my codes but either my server or database since the yours works for you perfectly so i started afresh deleting the old database, backedup my sites then uninstalled wamp and reinstalled it. Then i recreated the database and tried it this time with that same code and it worked. it was painstaking  but it was worth it. Thanks for everything.

Link to comment
Share on other sites

 Share

×
×
  • Create New...