Database connection errors

Hello. I have a database with all the correct attributes (host, user, password etc), but I receive error messages when trying to connect. At least, I think the problem in in the code used, not the connection itself - best guess.

When I open my index page I get the following error messages:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at #myhostsite/www/#mysite/index.php:3) in /www/#mysite/#mydirectory/init.php on line 1

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at #myhostsite/www/#mysite/index.php:3) in /www/#mysite/#mydirectory/init.php on line 1

I include part of the HTML for index.php:

[code]<!doctype html>

<?php include 'core/init.php'; ?> <?php include 'includes/head.php'; ?>
    <!-- nav bar -->
    <?php include 'includes/navbar.php'; ?>[/code]

Code for core/init.php:

<?php session_start(); require 'database/connect.php'; ?>

Code for core/database/connect.php:

$conn = new mysqli($servername, $username, $password, $database);

I have tried putting the PHP on a single line, uploading as utf-8, and I still get the same errors.

If it helps the hostsite php is 5.5.33.

I don’t know what the problem is caused by. I am at a loss as to how to solve the problem.

Any help will be appreciated.

Please not I an a novice as far as PHP is concerned.

Thanks in advance.

Well the first thing I would do is

[php]<?php
/* Put your configuration file (I’m assuming this is what it is) at the very top */
include ‘core/init.php’;
?>
<!doctype html>

<?php include 'includes/head.php'; ?>
     <!-- nav bar -->
     <?php include 'includes/navbar.php'; ?>[/php]

I would also do the top portion of your html a little differently, I can better show you than explain it, so here’s what I’m talking about. I’ll use one of my own projects.

Here’s my contact page (contact.php) :
[php]<?php
require_once ‘lib/includes/utilities.inc.php’; // This is my configuration file

use website_project\email\EmailData as Email;
use website_project\email\Contact as SendForm;

$submit = filter_input(INPUT_POST, ‘submit’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);

if (isset($submit) && $submit === ‘submit’) {
$first_name = filter_input(INPUT_POST, ‘first_name’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$last_name = filter_input(INPUT_POST, ‘last_name’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$name = $first_name . ’ ’ . $last_name;
$email = filter_input(INPUT_POST, ‘email’, FILTER_SANITIZE_EMAIL);
$message = filter_input(INPUT_POST, ‘comment’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$topic = filter_input(INPUT_POST, ‘reason’, FILTER_SANITIZE_SPECIAL_CHARS);

/* Spambot check this uses a hidden Captcah technique */
$spambot = filter_input(INPUT_POST, 'subject', FILTER_SANITIZE_SPECIAL_CHARS);


$send = new SendForm(new Email($name, $email, $topic, $message, $spambot));
if (!$send->sendMail()) {
    if (!$send->getName()) {
        $nameError = $send->getNameError();
    } else {
        $displayName = $send->getName();
    }

    if (!$send->getEmail()) {

        $emailError = $send->getEmailError();
    } else {
        $displayEmail = $send->getEmail();
    }
    if (!$send->getMessage()) {
        $messageError = $send->getMessageError();
    } else {
        $displayMessage = $send->getMessage();
    }
} else {
    $success = 'Data has been successfully emailed to John Pepp';
}

}

/* This is where I have my top portion of my html */
require_once ‘lib/includes/header.inc.php’;
?>

[/php]

Here’s my top portion of my html (header.inc.php):
[php]<?php
use website_project\users\Members as gameLogin;
$gameLogin = new gameLogin($db);
$today = new DateTime(“Now”, new DateTimeZone(‘America/Detroit’));
$login = filter_input(INPUT_POST, ‘login’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);

if (isset($login) && $login === ‘login’) {
$data[‘username’] = filter_input(INPUT_POST, ‘username’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$data[‘password’] = filter_input(INPUT_POST, ‘password’, FILTER_SANITIZE_FULL_SPECIAL_CHARS);

$result = $gameLogin->read($data);
if ($result) {
    header("Location: index.php");
    exit();
} else {
    $errorMessage = '<span id="register">Invalid Login, Please Try Again! Need to <a href="registerPage.php">Register?</a></span>';
}

}
?>

<?php echo ($pageName === "Index") ? "Design & Development" : $pageName . " Page"; ?>

Pepster's Place

  • Pepster's Place Facebook Page
  • Pepster's Twitter Profile
  • John Pepp's LinkedIn Profile
  • John Pepp's Flickr Profile
<?php if (!$user) { ?> Username Password <?php echo (isset($errorMessage) && $errorMessage) ? $errorMessage : 'Need to Register?'; ?> <?php } ?> <?php if ($user) { ?>
<?php echo ($user) ? "

Hello " . $user->username . "!

\n" : NULL; ?>
<?php } ?>[/php]

I’m not expecting you to understand most of the code, but the main thing I trying to do is to keep the php separated as much as possible from the HTML. Maybe by getting things in order will resolve some of the issues, but I would also post more of your connection code (mysqli_) and how you connect to it. It will better help anyone trying to help you. HTH John

Your error has nothing to do with the database connection. Your issue is, you are including files that need to come before your html. Move your includes to the top of the file first. A header has to be sent to the browser before the other content, meaning no html before it.

Sponsor our Newsletter | Privacy Policy | Terms of Service