Hello,

As a short introduction, I primarily do graphic/web design for a small company and being the only computer savvy employee I've been tasked with remedying the situation. I have very limited PHP knowledge and would greatly appreciate any help.

The servers were updated to PHP 5.3 from 5.2 just prior to the new year and not surprisingly our meeting database went down due to incompatible/deprecated code.

The code, from what I understand, was done 3-4 years ago from a contract. I have google'd quite extensively to try and get a handle on PHP but it hasn't led to any meaningful results. I'm not looking for a thorough solution as it would probably result in rewriting quite a bit of code since I understand that there are more deprecated functions going from 5.3->5.5; I just want to understand why this particular instance is happening and work from there.

These are the errors that are showing up:

Deprecated: Function mysql_db_query() is deprecated in /sites/www.redacted.org/htdocs/register/include/sessions.php on line 46

Deprecated: mysql_db_query() [function.mysql-db-query]: This function is deprecated; use mysql_query() instead in sites/www.redacted.org/htdocs/register/include/sessions.php on line 46

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /sites/www.redacted.org/htdocs/register/include/sessions.php:46) in /sites/www.redacted.org/htdocs/register/include/funcs_db.php on line 11

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /sites/www.redacted/htdocs/register/include/sessions.php:46) in /sites/www.redacted.org/htdocs/register/include/funcs_db.php on line 11

Warning: Cannot modify header information - headers already sent by (output started at /sites/www.redacted.org/htdocs/register/include/sessions.php:46) in /egr/sites/www.redacted.org/htdocs/register/input.php on line 179

The following is the sessions.php code:

<?php
class SessionManager 
{
   var $life_time;

   function SessionManager() {

  // Read the maxlifetime setting from PHP

   $this->life_time = 3600;

  // Register this object as the session handler

   session_set_save_handler( 
     array( &$this, "open" ), 
     array( &$this, "close" ),
     array( &$this, "read" ),
     array( &$this, "write"),
     array( &$this, "destroy"),
     array( &$this, "gc" )
    );

   }

   function open( $save_path, $session_name ) {
      global $sess_save_path;
      $sess_save_path = $save_path;      // Don't need to do anything. Just return TRUE.
      return true;
   }

   function close() {
      return true;
   }    function read( $id ) {

  // Set empty result

   $data = '';

  // Fetch session data from the selected database

  $time = time();
  $newid = mysql_real_escape_string($id);
  $sql = "SELECT `session_data` FROM `reg_session` WHERE `session_id` = '$newid' AND `expires` > $time";

  $rs = mysql_db_query(preservation,$sql);
  $a = mysql_num_rows($rs);

  if($a > 0) {
    $row = mysql_fetch_assoc($rs);
    $data = $row['session_data'];
  }

  return $data;
   }

   function write( $id, $data ) 
   {

  // Build query                

	$time = time() + $this->life_time;       $newid = mysql_real_escape_string($id);
	$newdata = mysql_real_escape_string($data);
	$sql = "REPLACE `reg_session` (`session_id`,`session_data`,`expires`) VALUES('$newid', '$newdata', $time)";
	$rs = mysql_db_query(preservation,$sql);
	return TRUE;
   }

   function destroy( $id ) {

  // Build query
  $newid = mysql_real_escape_string($id);
  $sql = "DELETE FROM `reg_session` WHERE `session_id` = '$newid'";
  mysql_db_query(preservation,$sql);
  return TRUE;
   }

   function gc() {
		// Garbage Collection
		// Build DELETE query.  Delete all records who have passed
		//  the expiration time
		$sql = 'DELETE FROM `reg_session` WHERE `expires` < UNIX_TIMESTAMP();';
		mysql_db_query(preservation,$sql);

	// Always return TRUE
	return true;
}
}
?>

The following is the funcs_db.php code:

<?php
/*******
This include file is meant for any functions that involve database queries
********/

/*****Initializes session_set_save_handler*****/ 
function createSession()
{
	require_once("./include/sessions.php");
	$sess = new SessionManager();
	session_start();
}
//*******Determines additional fields in database***********//
function findExtraFields($meeting_id,$db_link)
{

$eventquery = 'SELECT * FROM preservation.meeting_info WHERE meeting_id=\''.$meeting_id.'\'';
$eventresults = mysql_query($eventquery, $db_link) or die (mysql_error());
$erow = mysql_fetch_assoc($eventresults);

$field_list = mysql_query('DESCRIBE preservation.'.$erow['meeting_database'],$db_link);

$common_fields = array('person_id', 'Date', 'First_Name', 'Last_Name', 'Title', 'Agency', 'Email', 'Phone', 'Address', 'City', 'State', 'Country', 'Zip', 'notes');
$extra_fields = array();
while ($i = mysql_fetch_assoc($field_list))
{
	if(!in_array($i['Field'],$common_fields))
	{
		array_push($extra_fields,$i['Field']);
	}
} 
return $extra_fields;
}

?>

So far I've tried replacing all the deprecated "mysql_db_query()" functions to "mysql_query" but that leads to a new error which I assume has to do with the "$sql" variable.

Warning: mysql_query() expects parameter 2 to be resource, string given in /sites/www.redacted.org/htdocs/register/include/sessions.php on line 46

Warning: mysql_num_rows() expects parameter 1 to be resource, null given in /sites/www.redacted.org/htdocs/register/include/sessions.php on line 47

I also tried a mysql connection check to the database and that seemed to work.

$db = mysql_connect("redacted", "redacted", "redacted"); 
mysql_select_db("preservation",$db); 
if (!$db)
  {
  echo "Failed to connect to MySQL: " . mysql_error();
  } else { echo "Connection was OK!\n";}

Thanks for reading.

    Compare [man]mysql_db_query[/man] with [man]mysql_query[/man] from the PHP manual:

    resource mysql_db_query ( string $database , string $query [, resource $link_identifier = NULL ] )
    
    mixed mysql_query ( string $query [, resource $link_identifier = NULL ] )

    As you can see, because mysql_db_query also selects the database, its first parameter is the database name. To change this to mysql_query, you should call mysql_select_db (which you did), and then change the call, e.g.,

    mysql_db_query(preservation,$sql);

    becomes:

    mysql_query($sql);

    After you have fixed these, your "Cannot send session cookie - headers already sent" error will probably also disappear: my guess is that you're getting that because of the printing of the mysql_db_query warning.

    If you're not using a version control system (but you should be), then save a copy of the code after making these fixes, upon which you can convert the code using the legacy MySQL extension to use the [man]MySQLi[/man] extension or the [man]PDO[/man] extension.

      If you have to continue using the deprecated mysql_ functions (instead of the preferred mysqli or PDO extensions), you'll either have to turn off "deprecated" warnings, or else set display_errors off entirely (which is probably what the production version should use, anyway, and just look in the PHP error log for errors, instead).

      <?php
      error_reporting(E_ALL & ~E_DEPRECATED);
      /* rest of script... */
      

        Thank you both for the quick replies.

        I did look through php.net quite a bit during my search but my reading comprehension must be off when it comes to syntax. Making that change did clear up the first two warnings but the "Cannot send session cookie - headers already sent" warnings still persist.

        The error reporting feature is quite useful as it brought up a bunch of Notices for "Undefined variable/index" but after reading about them it seems to be a minor error when it relates to user filled forms and can be hidden.

        Now the next hurdle is the review/submission page where the user checks that all the information provided is correct and enters it into the database. This is where things are definitely getting complicated for me and please excuse any incorrect wording/terminology I will probably use. First off the same two session cookie/cache limiter warnings are still appearing and a new one.

        Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /sites/www.redacted.org/htdocs/register/include/funcs_db.php on line 25

        On the review/ page I'm seeing that most of the previous page's forms are being pulled but I think there is a missing connection between the database and the review page. This page does not show the meeting name as it did in the input page and the previous forms that called to the database to fetch information are coming in as "Undefined index". I'm not sure if I'm making any sense but what I'm trying to say is that the input form has dropdowns/radio buttons that were pulled from the database but none of those dropdowns/radio buttons selected are being read out by the review page. I have a feeling that this is caused by the following code in the review.php page:

        //Set database information
        include '../scripts/db_select.php';
        $db = 'preservation';
        $db_link = selectDB($db);
        
        include "./include/funcs_db.php";
        include "./include/funcs_doc.php";
        createSession();
        
        
        //Set SESSION vars for POST data for database columns not specified
        $extra_fields = findExtraFields($_SESSION['meeting_id'],$db_link);
        
        foreach ($extra_fields as $extra_field)
        {
        	if($_POST[$extra_field] != '') $_SESSION[$extra_field] = $_POST[$extra_field];
        }
        
        

        The db_select.php contains logins to multiple databases (which we don't use most of). Here is the relevant code in the page:

        	
        <?php
        function selectDB($db_input)
        {
        	if ($db_input == 'preservation')
        	{
        		$dbname = 'redacted';
        		$dbuser = 'redacted';
        		$dbpass = 'redacted';
        	}
        $dbhost = "redacted";
        	$db = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
        	//mysql_select_db($dbname,$db);
        	return $db;
        }

        I've been staring at these php pages for about two days now without much progress so forgive me if I sound ridiculous. Please let me know if this is a lost cause for me to fix and kindly direct me to where I can get direct help.

        Thanks again for reading.

          Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /sites/www.redacted.org/htdocs/register/include/funcs_db.php on line 25

          Usually this means that the preceding call to query the database was rejected for some reason by that database, causing PHP's mysql_query() to return a Boolean FALSE instead of the normal query result resource. Therefore, it's always a good idea to check the result of mysql_query() before trying to use it, and if it's false, do whatever error reporting you need (along with some user-friendly "Oops!" result, at least in production.) A typical debugging technique I use:

          $sql = "SELECT blah blah blah...";
          $result = mysql_query($sql);
          if($result == false) {
              throw new Exception("Query failed\n".mysql_error()."\n$sql");
          }
          

          (You could then set up an exception-handler or use a higher level try/catch block to handle that exception if it occurs, perhaps with different options depending on development versus production mode.)

            Write a Reply...