Page 1 of 1

OO code not working :(

Posted: Tue Oct 28, 2008 6:54 am
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:

Re: OO code not working :(

Posted: Tue Oct 28, 2008 7:07 am
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

Re: OO code not working :(

Posted: Tue Oct 28, 2008 8:08 am
by onion2k
That looks like you're writing PHP 4 code ... it won't run on PHP 5.

Re: OO code not working :(

Posted: Tue Oct 28, 2008 8:16 am
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);