Help needed on nearly complete site

I have a non-profit Christian school site that asked me to create a custom CMS so they could update the main parts of their site in-house. I’m a front-end developer, but wanted to learn more PHP, so I created the CMS and the site to read the database as needed. Through assistance from this forum, I was able to get it working correctly on my computer’s MAMP server.

Unfortunately, when I moved it to the live server, more errors came up. Two scripts are coming up with undefined variables, but despite multiple checks, I can’t see the error. The variables are defined and work on MAMP, just not the live server. I have asked for assistance here, but it seems to no avail.

As school is starting in a few weeks for them, this project needs to be fully functioning. After completely volunteering my time and spending four months working on it through other work, I’m tired of it. I need either assistance in finding the problem or another developer that’s willing to step in and work on it.

Ideally, it needs to be done by August 9, but earlier is better. The offending scripts are listed below, but anyone willing to offer help is appreciated. The DbFactory was totally new to me, which wouldn’t take much, but since I had everything working, I don’t believe it’s the problem.

DbFactory.php


<?php
class DbFactory{
  private static $factory;
  public static function getFactory(){
      if (!self::$factory){
        self::$factory = new DbFactory();
        return self::$factory;
      } else {
        throw new exception('DbFactory could not return database instance.');
      }
  }
  private $db;
  public function getConnection(){
      if (!$db)
           $db = new PDO("mysql:host=216.51.232.202;dbname=name", "user", "pass");
      return $db;
  }
}
?>

getschedule.php


<?php
$o_Db = DbFactory::getFactory()->getConnection();

function GetSchedule(PDO $o_Db, $table_name, $fields){
  if(!$o_Db || !$table_name || !fields){
    throw new exception('GetData(PDO $o_Db, string $table_name, array or string $fields. You did not pass one of the variables');
  }
  if(is_array($fields)){
    $fields_count = count($fields);
    $i = 1;
    $sql_fields = null;
    foreach($fields as $value){
      if($i < $fields_count){
        $sql_fields .= "$value, ";
      } else {
        $sql_fields .= "$value";
      }
      $i++;
    }
  } else {
    $sql_fields = htmlentities($fields);
  }
  $table_name = htmlentities($table_name);

  $sql = "SELECT datetext FROM schedule";
  $stmt = $o_Db->prepare($sql);
  $stmt->execute();
  return $stmt->fetchAll(PDO::FETCH_ASSOC);
  }
?>

more errors came up.

This sounds as if you mean there were already some errors, were there? and if so what were they?

Two scripts are coming up with undefined variables

What are they then?

What else is going wrong, you posted some db connection stuff, is that the only thing that is wrong?

Did you compare the output of phpinfo() on both servers to see what differences there are? If not, do that now.

Just bad wording on my part. The errors I had when doing initial development were all resolved. No errors existed on my local MAMP version of the site when I uploaded it to the active server.

The two errors that are being reported are:

  1. Undefined variable: db in E:\Websites\1414\sullychristian.org\assets\includes\DbFactory.php on line 14

  2. Use of undefined constant fields - assumed ‘fields’ in E:\Websites\1414\sullychristian.org\assets\includes\getschedule.php on line 5

Those are the two files posted above. I haven’t done the comparison or knew you could, so doing it now. I did use PHPMyAdmin and MySQL in the host’s server to initially put some data in the database and it’s coming up on the site as it should be.


<?php  
class DbFactory{ 
  private static $factory; 
  public static function getFactory(){ 
      if (!self::$factory){ 
        self::$factory = new DbFactory(); 
        return self::$factory; 
      } else { 
        throw new exception('DbFactory could not return database instance.'); 
      } 
  } 
  private $db; 
  public function getConnection(){ // note $this-> was added
      if (!$this->db) 
           $this->db = new PDO("mysql:host=216.51.232.202;dbname=name", "user", "pass"); 
      return $this->db; 
  } 
} 
?>


   <?php 
$o_Db = DbFactory::getFactory()->getConnection(); 

function GetSchedule(PDO $o_Db, $table_name, $fields){ 
  if(!$o_Db || !$table_name || !$fields){ ... // note the $ before fields added

Those are both critical errors that should have not worked on your local box.

Thanks oddz, that fixed the error on the DbFactory, now on to getschedule.

I added “echo phpinfo()” to a page and got a lot of information. The only main thing I see is that the active server is on version 5.2.14 and MAMP is 5.3.6. With both being version 5, I wouldn’t think a large discrepancy would occur.

Not sure if you saw it, but oddz fixed your getschedule issue too, your first if statement is missing the $ in front of “fields”.

Are there any additional issues you need help with?

Nope I missed it oddz, sorry, but that also fixed it. It proves that more eyes are better, I’ve been looking at that line for weeks and didn’t see it.

I think that’s all, thanks everyone!

Sorry, but I ran into another error when logging in to the CMS.

Fatal error: Call to undefined function mysqli_connect() in E:\Websites\1414\sullychristian.org\assets\includes\connect.php on line 5

connect.php


<?php
// Removed comment
// Removed comment

$link = mysqli_connect('216.51.232.202', 'user', 'pass');
if (!$link)
{
	$error = 'Unable to connect to the database server.';
	include 'error.php';
	exit();
}

if (!mysqli_set_charset($link, 'utf8'))
{
	$output = 'Unable to set database connection encoding.';
	include 'error.php';
	exit();
}

if (!mysqli_select_db($link, 'database'))
{
	$error = 'Unable to locate the Sully database.';
	include 'error.php';
	exit();
}
?>

The comments are just reminders for the different database connections.

Check phpinfo() but it could be that the version of php on the server isn’t compiled against mysqli or isn’t setup to load it in the php.ini

Sorry, PHP novice, what line do I look at? It’s up at http://www.sullychristian.org/admin/

If mysqli is active, you should see something like this:

mysqli

MysqlI Support	enabled
Client API library version	5.1.63
Active Persistent Links	0
Inactive Persistent Links	0
Active Links	0
Client API header version	5.1.49
MYSQLI_SOCKET	/var/run/mysqld/mysqld.sock

Directive	Local Value	Master Value
mysqli.allow_local_infile	On	On
mysqli.allow_persistent	On	On
mysqli.default_host	no value	no value
mysqli.default_port	3306	3306
mysqli.default_pw	no value	no value
mysqli.default_socket	no value	no value
mysqli.default_user	no value	no value
mysqli.max_links	Unlimited	Unlimited
mysqli.max_persistent	Unlimited	Unlimited
mysqli.reconnect	Off	Off

When I went to your site, I did not see anything like the above. So that tells me mysqli is not setup on your remote server.

Great, back to the drawing board. Thanks again. Any suggestions where to go for the connection now? Is it possible to take the dbfactory’s connection and use it for this? Since the site displays what’s in the database, I’m assuming it’s connecting via DbFactory.

Change mysqli and any other related mysqli functions to mysql (note, some mysqli functions are named differently, so you may need to search php.net to find the correct mysql function)

So, this site depends on both PDO and Mysqli? - personally I’d forget converting Mysqli to mysql, if PDO is working as expected I’d stick to PDO for all db transactions.

EDIT

Is there some legacy code you imported that used Mysqli? I guess its a moot point which is easier to rewrite, Mysqli to mysql or Mysqli to PDO - maybe it will come down to how many lines of code there are.