Jump to content
MakeWebGames

Little help, I'm recieving an error from a custom code


Recommended Posts

Posted

Ok my friend was helping me with mutliple database connections and made me an error handler, but anyway I am now getting this error while logging in:

Parse error: syntax error, unexpected T_GLOBAL, expecting T_FUNCTION in /home/gangland/public_html/functions/db.php on line 4

Here is the code for the db.php file. Please do not steal this file (it won't work without the other section anyway).

<?
//db.php
class db {
global $error;
var $conn;
if(!isset($error)) {
	require_once('functions/error.php');
	$error = new error();
}
function connect($dbhost= 'localhost',$dbuser = 'root',$dbpass = '',$dbname = ''){
	$conn 		= @mysql_connect($dbhost,$dbuser,$dbpass);
	$dbselect 	= @mysql_select_db($dbname,$conn);
	if(!$conn) 				return $error->db(mysql_error(),true,true);
	else if(!$dbselect) 	return $error->db(mysql_error(),true,true);
	else 					$this->conn = $conn;	
}
function query($sql = ''){
	$result = @mysql_query($sql,$this->conn);
	if(!$result) return $error->db(mysql_error(),true);
	else return $result;
}
function freeresult($result = ''){
	$clear = @mysql_free_result($result);
	if(!$clear) return $error->db(mysql_error(),false);
}
function close(){
	mysql_close($this->conn);
}
}


?>
Posted

Re: Little help, I'm recieving an error from a custom code

Your line error is:

var $conn;

I am sure that the word "var" without a variable sign infront of it ($) would be javascript if i am not correct. How ever i see no need to use var $conn unless there is javascripting in the other section.

Try this and see if anything goes wrong:

 

<?php
//db.php
class db 
{
global $error,$conn;
if(!isset($error)) 
{
	require_once('functions/error.php');
	$error = new error();
}
function connect($dbhost= 'localhost',$dbuser = 'root',$dbpass = '',$dbname = '')}
{
	global $conn,$error,$this;
	$conn		= 	@mysql_connect($dbhost,$dbuser,$dbpass);
	$dbselect	= 	@mysql_select_db($dbname,$conn);
	if(!$conn)
	{
		return $error->db(mysql_error(),true,true);
	}
	else if(!$dbselect)
	{
		return $error->db(mysql_error(),true,true);
	}
	else
	{
		$this->conn = $conn;
	}  
}
function query($sql = '')
{
	global $error,$this;
	$result = @mysql_query($sql,$this->conn);
	if(!$result)
	{
		return $error->db(mysql_error(),true);
	}
     		else
	{
		return $result;
	}
}
function freeresult($result = '')
{
	global $error;
	$clear = @mysql_free_result($result);
	if(!$clear)
	{
		return $error->db(mysql_error(),false);
	}
}
function close()
{
	global $this;
	mysql_close($this->conn);
}
}


?>
Posted

Re: Little help, I'm recieving an error from a custom code

Come on killah, you didn't even try there.

You don't use 'global $anything' inside a class unless it's inside a function.

'var $conn' is correct and makes it a property (?) of the class

Posted

Re: Little help, I'm recieving an error from a custom code

Does he not need to use:

public var = 'the var here in other words $conn';

so it would be:

public $var = $conn;

------

Edit:

I think i now know why. I have tried putting a function right after the class db { and i now get this error:

Parse error: syntax error, unexpected T_VAR in /**/**/**/**/**.php on line 8

Posted

Re: Little help, I'm recieving an error from a custom code

Well yeah, I mean you can use var but you should be using public/private/protected.

You wouldn't have 'public $var = $conn' though because it isn't defined, you'd just have 'public $conn = null;', then in the first function, set '$this->conn = mysql_connect() ... '

Posted

Re: Little help, I'm recieving an error from a custom code

very good using clases but on the first few lines of it is where your going wrong. Try having everything inside the class wrapped in a function, so instead of getting a file on error posting an error message. There are many tutorials and examples on the internet which you can learn from/

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.   Paste as plain text instead

  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...