OO code not working :(

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yoji
Forum Commoner
Posts: 25
Joined: Sun Oct 19, 2008 3:09 am

OO code not working :(

Post by yoji »

I just started objected oriented programming. Wrote first code today but it's creating problem... Here's the code

Code: Select all

 
class sqlize
{
    var $host="localhost";
    var $user="root";
    var $password="";
    function sqlize($databaseName)
    {
        global $con;
        $con=mysql_connect($this->host,$this->user,$this->pass);
        $que="mysql_select_db($databaseName,$con)";
        if (!$con)
        {
            echo "Couldn't connect : ".mysql_error();
        }
        else
        {
            if(!$que)
            {
                mysql_query("CREATE DATABASE $name",$con);
            }
    }
    
    function close()
    {
        mysql_close($con);
    }
    
}
$demo = new sqlize();
 
This code is giving error. The error is "Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in J:\index.php on line 30".. The error is in creating the instance... I might have done some foolish mistake; I really want to know what's the prob... :cry:
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: OO code not working :(

Post by watson516 »

yoji wrote:I just started objected oriented programming. Wrote first code today but it's creating problem... Here's the code

Code: Select all

 
class sqlize
{
    var $host="localhost";
    var $user="root";
    var $password="";
    function sqlize($databaseName)
    {
        global $con;
        $con=mysql_connect($this->host,$this->user,$this->pass);
        $que="mysql_select_db($databaseName,$con)";
        if (!$con)
        {
            echo "Couldn't connect : ".mysql_error();
        }
        else
        {
            if(!$que)
            {
                mysql_query("CREATE DATABASE $name",$con);
            }
        }
    }
    
    function close()
    {
        mysql_close($con);
    }
    
}
$demo = new sqlize();
 
This code is giving error. The error is "Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in J:\index.php on line 30".. The error is in creating the instance... I might have done some foolish mistake; I really want to know what's the prob... :cry:

You forgot a } to close the first if statement
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: OO code not working :(

Post by onion2k »

That looks like you're writing PHP 4 code ... it won't run on PHP 5.
pointer
Forum Newbie
Posts: 3
Joined: Tue Oct 28, 2008 8:04 am

Re: OO code not working :(

Post by pointer »

line 11. use $que=mysql_select_db($databaseName,$con); dont use with quotes.
append } line 30
and
if your class constructor will use parameter, you must use parameter when creating the object.
$demo = new sqlize($dbName);
Post Reply