What is wrong in this? Error

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
indianopendirectory
Forum Newbie
Posts: 5
Joined: Thu Feb 10, 2005 3:12 pm

What is wrong in this? Error

Post by indianopendirectory »

PLEASE HELP

CODE:

Code: Select all

<?php

class admin &#123; 
    var $result; 

function dbinit() &#123;
   $link = mysql_connect("localhost", "XX", "XX")
       or die('counter CONNECT error: '.mysql_errno().', '.mysql_error());
   mysql_select_db("XX");
&#125;

function query($q)&#123;

	$this->result = mysql_query($q, $link) 
    or die('counter SELECT error: '.mysql_errno().', '.mysql_error());

	return $this->result;
&#125;
&#125;

class out extends admin &#123;
var $sql;
$this->sql = 'select * from TABLE';
$this->dbinit();
$this->query($this->sql);

&#125;

$output = new out();
print ("$output");
?>
ERROR I AM GETTING:

Parse error: parse error, unexpected T_VARIABLE, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in ............. on line 33
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Seeing as your code only shows 31 lines.. I have a feeling you didnt show us everything.
The first thing I saw, although not line 33, was this:

Code: Select all

$this->result = mysql_query($q, $link) or die('counter SELECT error: '.mysql_errno().', '.mysql_error());
It won't be able to access the $link variable seeing as it is only set inside the dbinit() function making it inassecible to other functions.

[edit]Also, to my knowledge, you can't just use commands inside of a class. They must be inside of a function.
Last edited by d3ad1ysp0rk on Thu Feb 10, 2005 3:37 pm, edited 1 time in total.
indianopendirectory
Forum Newbie
Posts: 5
Joined: Thu Feb 10, 2005 3:12 pm

Post by indianopendirectory »

Sorry I did it, ERROR WAS ON line 24, Thanks for help

and I did some changes...

Code: Select all

class admin &#123; 
    var $result;
	var $link;

function dbinit() &#123;
   $this->link = mysql_connect("localhost", "XX", "XX")
       or die('counter CONNECT error: '.mysql_errno().', '.mysql_error());
   mysql_select_db("XX");
&#125;

function query($q)&#123;

	$this->result = mysql_query($q, $this->link) 
    or die('counter SELECT error: '.mysql_errno().', '.mysql_error());

	return $this->result;
&#125;
&#125;
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Try this:

Code: Select all

<?php
class admin &#123;
    var $link;
    function dbinit() &#123;
        $this->link = mysql_connect("localhost", "XX", "XX") or die('counter CONNECT error: '.mysql_errno().', '.mysql_error());
        mysql_select_db("XX");
    &#125;

    function query($q)&#123;
        $result = mysql_query($q, $this->link) or die('counter SELECT error: '.mysql_errno().', '.mysql_error());
        return $result;
    &#125;
&#125;

class out extends admin &#123;
    var $sql;
    function out()&#123;
        $this->sql = "SELECT * FROM table";
        $this->dbinit();
        $result = $this->query($this->sql);
        $my_array = array();
        while($row = mysql_fetch_assoc($result))&#123;
            $my_array = $row;
        &#125;
        return $my_array;
    &#125;
&#125;

$output = new out();
print_r($output);
?>
I think that's what you were trying to do at least..
indianopendirectory
Forum Newbie
Posts: 5
Joined: Thu Feb 10, 2005 3:12 pm

Post by indianopendirectory »

Used your code , Now getting

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in ...... on line 10
counter SELECT error: 0,

10 th LINE is

Code: Select all

$result = mysql_query($q, $link) or die('counter SELECT error: '.mysql_errno().', '.mysql_error());
Many Thanks :)
Last edited by indianopendirectory on Thu Feb 10, 2005 4:04 pm, edited 1 time in total.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

try mine again, I edited it like a second after I posted it and was afraid you'd copy the original. :p
indianopendirectory
Forum Newbie
Posts: 5
Joined: Thu Feb 10, 2005 3:12 pm

Post by indianopendirectory »

Yes it's working but not fetching record (only one record, in phpMyAdmin, sql working fine)

Code: Select all

class out extends admin &#123; 
    var $sql; 
    function out()&#123; 
        $this->sql = "SELECT iod_l1_st.cat FROM iod_l1_st where iod_l1_st.parent = 'l0_root'"; 
        $this->dbinit(); 
        $result = $this->query($this->sql); 
        $my_array = array(); 
        while($row = mysql_fetch_assoc($result))&#123; 
            $my_array = $row; 
        &#125;  
        return $result; 
    &#125; 
&#125; 

$output = new out(); 
echo("Output = $output");
Result =
Output : Object
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

My apologies :)

Code: Select all

class out extends admin &#123;
    var $sql;
    function get_out()&#123;
        $this->sql = "SELECT iod_l1_st.cat FROM iod_l1_st where iod_l1_st.parent = 'l0_root'";
        $this->dbinit();
        $result = $this->query($this->sql);
        $my_array = array();
        while($row = mysql_fetch_assoc($result))&#123;
            $my_array = $row;
        &#125; 
        return $my_array;
    &#125;
&#125;

$newobj = new out();
print_r($newobj->get_out);
Should work.
indianopendirectory
Forum Newbie
Posts: 5
Joined: Thu Feb 10, 2005 3:12 pm

Post by indianopendirectory »

:) I will try this, Thanks for your every support. Many Thanks.
Post Reply