I need to connect to a central db (a different one to the drupal site runs on), but when i insert the code before anything else (ie) in index.php, i get this error:

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/*******/public_html/index.php:18) in /home/*******/public_html/includes/session.inc on line 10

I am literally just doing a connect and 1 query...

How can i avoid this error?

Comments

cmsproducer’s picture

Without looking into detail, this is what comes to mind.

PHP will not allow you to send certain information within the page content because that information is expected in the header (before the page code begins). If this is the cause of your error, then you can change your php.ini settings (if you own the server or if you can arm-twist the host) to allow caching of headers so that they do not only have to be sent before anything else

Just to check: I hope that the DB that you are trying to connect to allows connections from places other than itself (localhost) - a common security config. - although this normally gives a clear mySQL centric error

Please remember to postt he solution when you find it so that we can all learn from it.

-----
iDonny - Web Content Management System Design, Development. & CRM

murph’s picture

The central db is on the same server, so thats no problem. I can look into the php.ini as i have a dedicated :)

coreyp_1’s picture

Drupal has functions for dealing with other databases (http://api.drupal.org/api/4.7/group/database), so you could put your code into your template files, and all should be well.

- Corey

murph’s picture

Hi, does this relate to 4.6 as well? I notice the link you gave refers to 4.7...

coreyp_1’s picture

9802008’s picture

Check the <?php tag at the start of index.php . Is it on the first line of the script?

eg:

line 1. <?php
line 2. # connect to database
line 3. # Drupal starts
...

www.jws.co.za/we_install_modify_and_maintain_drupal_web_sites

murph’s picture

...it is :)

heine’s picture

I guess there's an error / mistake in the code, or the way you inserted it. Please post the resulting file index.php between <code> or &lt?php tags.
--
The Manual | Troubleshooting FAQ | Tips for posting | Make Backups! | Consider creating a Test site.

murph’s picture

<?php
// $Id: index.php,v 1.82 2004-08-21 06:42:34 dries Exp $

/**
 * @file
 * The PHP page that serves all page requests on a Drupal installation.
 *
 * The routines here dispatch control to the appropriate handler, which then
 * prints the appropriate page.
 */

include_once("/home/***myuser***/public_html/mydbconnectfile.php");
$resultLINK = mysql_query("SELECT * FROM table WHERE site='field' AND field2 >= CURDATE()") or die(mysql_error());

include_once 'includes/bootstrap.inc';
drupal_page_header();
include_once 'includes/common.inc';

fix_gpc_magic();

$status = menu_execute_active_handler();
switch ($status) {
  case MENU_NOT_FOUND:
    drupal_not_found();
    break;
  case MENU_ACCESS_DENIED:
    drupal_access_denied();
    break;
}

drupal_page_footer();

?>

I originally had the following code just after my php code in the main root index.php file above (and gave me error in 1st post)...but i realised that i was putting the html outside of the page itself by doing that, so i added the following code to the theme template instead...(page.tpl.php):

while($row = mysql_fetch_array( $resultLINK )) {

echo " <a href='http://".$row['field3]."' title='".$row['field4']."'>".$row['field4']."</a> | ";
}

But that throws up a php error and stops the page loading...i guess you can't do that! (BTW i realise i shouldn't use mysql_query...i just want to get it to work first and then i'll replace with db_query :)

heine’s picture

Guess you have whitespace / an error in the included file.

Not sure why you do it this way though. Even apart from not using the drupal db_functions, why not make a small module and use something like:

  $connection = @mysql_connect($host, $username, $password, TRUE, 2);
  
  if (!mysql_select_db($database_name, $connection)) {
    drupal_set_message("Unable to select the database");
    return;
  }

--
The Manual | Troubleshooting FAQ | Tips for posting | Make Backups! | Consider creating a Test site.