Jump to content

[SOLVED] Calling a class within a function


steelmanronald06

Recommended Posts

Right, so I'm new to O.O.P but I've been doing a fair job of it.  So, what I have is the following:

 

lib/classes.php

<?php
class ErrorReporting {

// Define some variables
var $page;
var $error_msg;

function databaseInsert() {

	$page 	   = $this->page;
	$error_msg = $this->error_msg;

	if (getenv('HTTP_X_FORWARDED_FOR')) {

		$ip = getenv('HTTP_X_FORWARDED_FOR');

	} else {

		$ip = $_SERVER['REMOTE_ADDR'];

	}

	$errorMsgQuery = mysql_query("INSERT INTO errors (date, page, ip, error_msg) VALUES (now(), '$page', '$ip', '$error_msg')");

}

}
?>

 

lib/functions.php

<?php
function queryErrorCheck($query) {

//
// Error checks queries
//

global $db;
global $path;

$report = new ErrorReporting;
$errorMsg = $db->ErrorMsg();
$report->page  = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}";
$report->error_msg = "The following error: ' $errorMsg ' was recieved on the query:  $query ";
$report->databaseInsert();

echo '
	<img src="' , $path , 'includes/img/error.jpg" alt="Error" />
	<p>We are sorry, but an unexpected error has occured. Please try your request again.  If the problem
	continues, the administrators will be notified and the problem will be addressed. If you have had a loss
	of money, items, or your stats are no longer correct because of this error, please contact an
	administrator and we will investigate our logs and refresh your stats to the last correct settings.</p>
';

}
?>

 

access/market.php

<?php
			$itemStockQuery = $db->GetAll("SELECT * FROM items WHERE item_id=$item' AND stock > '0' LIMIT 1");

			// Error check
			if (!$itemStockQuery) {

				queryErrorCheck($itemStockQuery);

				daFooter();

				exit();

			};
?>

 

Right, so all the files are included properly, everything is going good. The function echos out the error to the page and everything. The only problem is the class isn't inserting the error into the database.  If I run the class by itself, outside the function, then it works, so there is nothing wrong with the query. It has to be the function. I just don't see what.  Any ideas???

Link to comment
Share on other sites

<?php
class ErrorReporting {

// Define some variables
var $page;
var $error_msg;

function databaseInsert() {

	$page 	   = $this->page;
	$error_msg = $this->error_msg;

	if (getenv('HTTP_X_FORWARDED_FOR')) {

		$ip = getenv('HTTP_X_FORWARDED_FOR');

	} else {

		$ip = $_SERVER['REMOTE_ADDR'];

	}

                 return mysql_query("INSERT INTO errors (date, page, ip, error_msg) VALUES (now(), '$page', '$ip', '$error_msg')");

}

}
?>

 

 

<?php
function queryErrorCheck($query) {

//
// Error checks queries
//

global $db;
global $path;

$report = new ErrorReporting;
$errorMsg = $db->ErrorMsg();
$report->page  = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}";
$report->error_msg = "The following error: ' $errorMsg ' was recieved on the query:  $query ";
if (!$report->databaseInsert()) {
             die("MySQL Encountered a fatal error:" . mysql_error());
       }

echo '
	<img src="' , $path , 'includes/img/error.jpg" alt="Error" />
	<p>We are sorry, but an unexpected error has occured. Please try your request again.  If the problem
	continues, the administrators will be notified and the problem will be addressed. If you have had a loss
	of money, items, or your stats are no longer correct because of this error, please contact an
	administrator and we will investigate our logs and refresh your stats to the last correct settings.</p>
';

}
?>

 

Try that see where it gets you. If I was a betting man I would say it is in the "date" column, maybe try to surround that in ` ` or rename it.

 

--FrosT

Link to comment
Share on other sites

updated code throws this:

 

MySQL Encountered a fatal error:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'You have an error in your SQL syntax; check the manual that corresponds to your ' at line 1

Link to comment
Share on other sites

Put the backticks and i still get this:

 

MySQL Encountered a fatal error:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'You have an error in your SQL syntax; check the manual that corresponds to your ' at line 1

Link to comment
Share on other sites

Try renaming the date column to dates or something like that and see if it still craps out.

 

Whoa, that error you are getting, it looks like you are trying to run a the query "You have an error in your SQL syntax;"

 

That is weird dude, I have never in my life seen that before....lol

 

 

Link to comment
Share on other sites

Could you echo your SQL like:

 

<?php
   //...
   $sql = "INSERT INTO `errors` (`date`, `page`, `ip`, `error_msg`) VALUES (now(), '$page', '$ip', '$error_msg')";
   echo $sql;
   return mysql_query($sql);
   //...
?>

 

I'm guessing that there might be an apostrophe in the $error_msg variable such that you're trying to run something like this:

 

INSERT INTO errors (date, page, ip, error_msg) VALUES (2007-03-14 00:00:00, 'foo.php', '127.0.0.1', 'you've entered something invalid')")

Link to comment
Share on other sites

Ah utexas_pjm is right, try this

 

$sql = "INSERT INTO `errors` (`date`, `page`, `ip`, `error_msg`) VALUES (now(), '$page', '$ip', '".mysql_real_escape_string($error_msg)."')";

 

Use that mysql_real_escape_string();

 

see if that helps.

 

 

Link to comment
Share on other sites

looking at it closely, $query is not the actual query once it reaches that point. look here:

 

$itemStockQuery = $db->GetAll("SELECT * FROM items WHERE item_id=$item' AND stock > '0' LIMIT 1");

			// Error check
			if (!$itemStockQuery) {

				queryErrorCheck($itemStockQuery);

unless you're doing things differently, $itemStockQuery looks like it contains the RESULTS of a query, not the query itself.

 

try replacing above with:

$query = "SELECT * FROM items WHERE item_id=$item' AND stock > '0' LIMIT 1";
$itemStockQuery = $db->GetAll($query);

			// Error check
			if (!$itemStockQuery) {

				queryErrorCheck($query);

 

hope that helps!

cheers

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.