Jump to content

Classes & PHP Versions


Gaia

Recommended Posts

Hi,

 

Not really sure how to direct this certain issue I am having.

 

I built a PHP script that uses OOP on a PHP 5.2.3 environment.  It is working fine in that environment. However, once I copied it over a PHP 5.2.5 enviromnet, it doesn't seem to like it. It comes up in IE as Programming Error or Unavailable Page.  Does not show any error messages at all either, I even tried setting the error reporting to E_ALL and still no luck.

 

Just hoping for some suggestions on how to pin-point the exact issue. I have been commenting out small pieces of code but it just does not seem to be parsing the classes correctly.

 

Are their any major updates from 5.2.3 to 5.2.5 that may cause issues? Like restrictions on how classes can be used?

 

Thanks in advanced for any info or suggestions anyone has.

Link to comment
Share on other sites

Yes, however, I was hoping there might be some general suggestions/thoughts of the issue.  It is a couple pages of code, so I wasn't sure that posting it all here would be efficient. I will go through it and try to copy/paste the main area's that I think are causing the issues. I am at work so I don't have the code handy. I will update once I have the exact code.

 

Until then, is it possible to initiate a class, within a class? For example, if i have class.one.php and class.two.php. Can I run an 'include' function to include class.one.php in class.two.php and initiate it without problems? I am currently doing that right now, and it works in my environment, but not the other.  Was another reason I was curious if there were any PHP updates that perhaps restricted that kind of coding for some reason.

 

I'm not the best at explaining things :P. Hopefully my code will be of more help.

Link to comment
Share on other sites

Here are the specific changes and bug fixes - http://www.php.net/ChangeLog-5.php

 

However, it is highly likely that your code is dependent on specific php.ini settings that are present on one server and not the other, such as short open tags or register globals.

 

The quickest way of getting a solution is if you post your code. Posting bits and pieces will just result in a game of twenty guesses, take up a couple of pages on the forum, and take a day or so. Your choice.

Link to comment
Share on other sites

I am still learning OOP, so I am bound to make noobish mistakes. See below:

 

class config {



private $config_path = '/home/gaia/public_html/xx/includes/config.php';



//

// Get a configuration value from the config file

//

public function get_config($config_name) {



        require($this->config_path);

        

        $value = $config[$config_name];



        return $value;

  

}
}

 

I think the private $config_path may be wrong or I need to declare it differently?

Link to comment
Share on other sites

The path seems to exist, when I do the following, it works:

 

class config {

private $config_path;

//
// Get a configuration value from the config file
//
public function get_config($config_name) {

        require( '/home/gaia/public_html/ae/includes/config.php');
        
        $value = $config[$config_name];

        return $value;
  
}
}

 

I tried the previous code on another server, and got the following error:

 

Fatal error: config::require() [function.require]: Failed opening required '' (include_path='.:/usr/local/php5/lib/php') in /home/gaia/public_html/xx/classes/class.config.php on line 28

 

So, it looks like the var isn't being passed into the module for some reason.

Link to comment
Share on other sites

Are you positive that /home/gaia/public_html/ae/includes/config.php exists? Because by the looks of it this is where you are bombing out.

It's also rather naughty to rely on your required file supplying your variables into your current context.

i.e. from my point of view as another developer I can only GUESS that the $config array comes from the required file. However I DO see your reasoning behind this and I don't have a better way for you to implement this (unless you want to go down the helper class road).

Link to comment
Share on other sites

I think I corrected the problem. The $config array is in a file called class.config.php. The config vars are in another file called config.php.

 

Hopefully you guys can help me with another issue now, with the same script.

 

I have a form where you can add things to a database, which uses a class called manage::add which is located in class.manage.php.  manage::add also uses a var named $db which initiates a PDO connection. $db is declared in a file called common.php.

 

common.php:

//
// Initiate the database connection
//
$db = new PDO('mysql:host=localhost;dbname=xx', 'xx', 'xx');

//
// Get the class that contains the config info
//
require_once("classes/class.config.php");
$config = new config();

//
// Get the class that controls entries
//
require_once("classes/class.entries.php");
$entries = new entries();

//
// Class the manages deleting, editing, etc
//
require_once("classes/class.manage.php");
$manage = new manage();

//
// Get the session class & start it
//
require_once("classes/class.session.php");
$session = new session;

$session->start();

//
// Set default timezone ( PHP 5+ only )
//
date_default_timezone_set('GMT');

 

class.manage.php:

class manage extends entries {

public $edit_form;

//
// Add an entry
//
public function add($post_info=array()) {

	global $db;


	//
	// Make sure there are no blank fields
	//
	if ( in_array("",$post_info) ) {

		echo '<div class="entry">You left a field blank!</div>';

	} else {

		$admin = $post_info['admin'];
		$admin_ip = $post_info['admin_ip'];
		$nickname = $post_info['name'];
		$nick_ip = $post_info['ip'];
		$date = $post_info['date'];
		$reason = $post_info['reason'];
		$action = $post_info['action'];

		$partial_ip = manage::break_ip($nick_ip);
		$partial_ip = $partial_ip[0].'.'.$partial_ip[1];

		$sql = "SELECT COUNT(*) FROM entries WHERE nickname LIKE '%$nickname%' OR ip LIKE '$partial_ip%' AND deleted = 0";
		if ($res = $db->query($sql)) {

			if ($res->fetchColumn() > 0) {

		       $is_dup = 1;
			   
		    } else {

		      $is_dup = 0;
			  
		   }

		}

		$query = $db->prepare("INSERT INTO entries VALUES ('0','$admin', '$admin_ip', '$nickname', '$nick_ip', '$date', '$reason', '$action','','','0','','','','$is_dup')");	
		$query->execute();

		//Send to home page
		$host  = $_SERVER['HTTP_HOST'];
		$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
		$extra = 'index.php';
		header("Location: http://$host$uri/$extra");
		exit();

	}

}
....... some more below, but only concerns the above method

 

When I attempt to submit the form, I get the following error saying that $db is non-object:

 

Fatal error: Call to a member function execute() on a non-object in /home/gaia/public_html/xx/classes/class.manage.php on line 67

 

I figured setting it as a global $db would allow the use of $db in another file?

Link to comment
Share on other sites

These are your problem lines:

 

$query = $db->prepare("INSERT INTO entries VALUES ('0','$admin', '$admin_ip', '$nickname', '$nick_ip', '$date', '$reason', '$action','','','0','','','','$is_dup')");	
$query->execute();

 

As you can see you're assigned the prepare statement to your $query variable. BUT then calling execute on the query variable. I think you probably want to do $db->execute() instead.

Link to comment
Share on other sites

But doesn't that defeat the purpose of assigning it to $query? Wouldn't I just do the following and it be the same then?

 

$db->prepare("INSERT INTO entries VALUES ('0','$admin', '$admin_ip', '$nickname', '$nick_ip', '$date', '$reason', '$action','','','0','','','','$is_dup')");	
$db->execute();

Link to comment
Share on other sites

Gaia, your original PHP syntax is correct, but my guess is your SQL is invalid. Try replacing those two lines with this for more debug info:

 

$query = $db->prepare("INSERT INTO entries VALUES ('0','$admin', '$admin_ip', '$nickname', '$nick_ip', '$date', '$reason', '$action','','','0','','','','$is_dup')");	
if(!$query){
    print "PDO::errorInfo():\n";
    print_r($db->errorInfo());
    exit;
}
$query->execute();

Link to comment
Share on other sites

It would be strange that it works in 5.2.3 as compared to 5.2.5? If that happens, what I see is a possible different configuration in 5.2.5. Double check to see if the configurations are all the same such as include_path. Check the error logs if no error messages are displayed because the settings might have been set to be written in the error log. You can do the following at the command line: tail -f path_to_error_log then load up the page to see any error messages.

Link to comment
Share on other sites

Hmm, I am getting this error from PDO. As far as i know,no other queries are active...

 

PDO::errorInfo(): Array ( [0] => HY000 [1] => 2014 [2] => Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute. )

 

I have a common.php that initiates the class, and at the end i include footer.php that nulls out $db ( $db=NULL; ).

Link to comment
Share on other sites

Couldn't locate the edit button:

 

However, I think it is saying this query was not complete, or closed?

 

		$sql = "SELECT COUNT(*) FROM entries WHERE nickname LIKE '%$nickname%' OR ip LIKE '$partial_ip%' AND deleted = 0";
		if ($res = $db->query($sql)) {

			if ($res->fetchColumn() > 0) {

		       $is_dup = 1;
			   
		    } else {

		      $is_dup = 0;
			  
		   }

		}

Link to comment
Share on other sites

Try using query() for both, so update these 2 lines:

 

$res = $db->query("INSERT INTO entries VALUES ('0','$admin', '$admin_ip', '$nickname', '$nick_ip', '$date', '$reason', '$action','','','0','','','','$is_dup')");	
//$query->execute(); //Remove this
if(!$res){
    print "PDO::errorInfo():\n";
    print_r($db->errorInfo());
    exit;
}

Link to comment
Share on other sites

It might have something to do with performing 1 query , not reaching the end of the resultset and then performing a 2nd query and then trying to loop through the resultset. At least this is the impression i'm getting from reading the last few posts.

As the error message suggests you should be performing your $db->query($sql) with an additional parameter, e.g. $db->query($sql, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY).

 

Lookup PDO and find the related constants you can use.

Link to comment
Share on other sites

I think the issues I am having are due to the following code:

 

$sql = "SELECT COUNT(*) FROM entries WHERE nickname LIKE '%$nickname%' OR ip LIKE '$partial_ip%' AND deleted = 0";
		if ($res = $db->query($sql)) {

			if ($res->fetchColumn() > 0) {

		       $is_dup = 1;
			   
		    } else {

		      $is_dup = 0;
			  
		   }

		}

 

When I attempt to re-build it using the example from PHP.NET docs, I get an error:

 

		$sql = "SELECT COUNT(*) FROM entries WHERE nickname LIKE '%$nickname%' OR ip LIKE '$partial_ip%' AND deleted = 0";
if ($res = $db->query($sql)) {

    // Check the number of rows that match the SELECT statement
    if ($res->fetchSingle() > 0) {

      echo 'Entries found';
    }
    // No rows matched -- do something else
    else {
        print "No rows matched the query.";
    }
}

 

Error: Fatal error: Call to undefined method PDOStatement::fetchSingle() in /home/gaia/public_html/xx/classes/class.manage.php on line 55

 

I am taking this directly from the PHP docs, so why would it not work?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.