Page 1 of 1

Calling class from within function

Posted: Thu Sep 28, 2006 9:32 pm
by GeXus
Is it not posible to call an outside class within a function? I'm using a DB class and trying to call it within a function i made, and it does not work.. any ideas?

Posted: Thu Sep 28, 2006 9:34 pm
by s.dot
Could be an issue with the scope... although I am not sure.

Posted: Thu Sep 28, 2006 9:37 pm
by Luke
post your code

Posted: Thu Sep 28, 2006 9:44 pm
by GeXus
The $db class is what is not working..

Code: Select all

function imageUpload($image, $user_id)
{
	
  $smallimage = new Upload($image);
  $mediumimage = new Upload($image);
  $largeimage = new Upload($image);
  $mktime = mktime();
 
    if ($smallimage->uploaded) 
    {
				$smallimage_path = 'img/90/1';
				$smallimage->file_new_name_body =  $user_id . $mktime . '_90';
				$smallimage_name = $smallimage->file_new_name_body.'.jpg';
				$smallimage->image_resize          = true;
				$smallimage->image_ratio_y         = true;
				$smallimage->image_x               = 90;
				$smallimage->image_convert = 'jpg';
				getimagesize($smallimage->file_dst_pathname);
				$smallimage->Process('/home/xxx/public_html/'.$smallimage_path);
				$smallimage_name = $smallimage_path."/".$smallimage_name;
    }
    if ($mediumimage->uploaded) 
    {    
				$mediumimage_path = 'img/170/1';
				$mediumimage->file_new_name_body =  $user_id . $mktime . '_170';
				$mediumimage_name = $mediumimage->file_new_name_body.'.jpg';
				$mediumimage->image_resize          = true;
				$mediumimage->image_ratio_y         = true;
				$mediumimage->image_x               = 170;
				$mediumimage->image_convert = 'jpg';
				getimagesize($mediumimage->file_dst_pathname);
				$mediumimage->Process('/home/xxx/public_html/'.$mediumimage_path);
				$mediumimage_name = $mediumimage_path."/".$mediumimage_name;
    }  
    
    if ($largeimage->uploaded) 
    {     
				$largeimage_path = 'img/600/1';
				$largeimage->file_new_name_body =  $user_id . $mktime . '_600';
				$largeimage_name = $largeimage->file_new_name_body.'.jpg';		
				$largeimage->image_convert = 'jpg';
				$largeimage->image_resize          = true;
				$largeimage->image_ratio_y         = true;
				$largeimage->image_x               = 600;
				getimagesize($largeimage->file_dst_pathname);
				$largeimage->Process('/home/xxx/public_html/'.$largeimage_path);     
				$largeimage_name = $largeimage_path."/".$largeimage_name;

		}

	   		 if ($smallimage->processed && $mediumimage->processed && $largeimage->processed) 
   				 {
    					[b]$db->sql_query("INSERT INTO user_images (user_id, image_size_90, image_size_170, image_size_600, default_image, created_date) VALUES ('$user_id','$smallimage_name', '$mediumimage_name', '$largeimage_name', '0', now())");[/b]
  				 }
  	
}
this is the sql class

Code: Select all

class sql

{

    var $dbhost;
    var $dbuser;
    var $dbpass;
    var $dbase;
    var $sql_query;
    var $mysql_link;
    var $sql_result;
    var $query_count;



    function sql()
    {


        $this->mysql_link = '0';

        $this->query_count = '0';

        $this->sql_result = '';

        $this->connection();

    }



    function connection()
    {

        $this->mysql_link = @mysql_connect( $this->dbhost, $this->dbuser, $this->dbpass ) or $this->error( mysql_error( $this->mysql_link ), $this->sql_query, mysql_errno( $this->mysql_link ) );

        @mysql_select_db( $this->dbase ) or $this->error( mysql_error( $this->mysql_link ), $this->sql_query, mysql_errno( $this->mysql_link ) );

    }



    function error( $error_msg, $sql_query, $error_no )
    {
        
        $error_date = date("F j, Y, g:i a");

//Heredoc Strings must be on the far left lines        
$error_page = <<<EOD
<html><head><title>mySQL database Error</title>
<style>P,BODY{ font-family:arial,sans-serif; font-size:11px; }</style></head><body>
&nbsp;<br><br><blockquote><b>There appears to be an error while trying to complete your request.</b><br>
You can try to go back and try again by clicking <a href="javascript:history.go(-1);">here</a>, if you
still get the error you can contact the site administrator by clicking <a href='mailto:haiden@asciant.net?subject=SQL+Error'>here</a>
<br><br><b>Error Returned</b><br>
<form name='mysql'><textarea rows="15" cols="60">mySQL query error: {$sql_query}

mySQL error: {$error_msg}
mySQL error code: {$error_no}
Date: {$error_date}</textarea></form></blockquote></body></html>
EOD;

echo $error_page;

    }



    function close()
    {

        mysql_close( $this->mysql_link );

    }



    function sql_query( $sql_query )
    {

        $this->sql_query = $sql_query;
        
        $this->sql_result = @mysql_query( $sql_query, $this->mysql_link );

        if (!$this->sql_result)

        {

            $this->error( mysql_error( $this->mysql_link ), $this->sql_query, mysql_errno( $this->mysql_link ) );

        }

    
            $count = $this->query_count;

            $count = ($count + 1);

            $this->query_count = $count;

    }



    function num_rows()
    {

        $mysql_rows = mysql_num_rows( $this->sql_result );

        //if (!$mysql_rows)

        //{

            //$this->error( mysql_error( $this->mysql_link ), $this->sql_query, mysql_errno( $this->mysql_link ) );

        //}

        return $mysql_rows;

    }



    function affected_rows()
    {

        $mysql_affected_rows = mysql_affected_rows( $this->mysql_link );

        return $mysql_affected_rows;

    }        



    function fetch_array()
    {

        if ( $this->num_rows() > 0 )

        {

            $mysql_array = mysql_fetch_assoc( $this->sql_result );

            if (!is_array( $mysql_array ))
            {
                return FALSE;
            }
    
            return $mysql_array;
        } 
    }


    function fetch_rows()
    {
        if ( $this->num_rows() > 0 )

        {

            $mysql_array = mysql_fetch_row( $this->sql_result );

            if (!is_array( $mysql_array ))
            {
                return FALSE;
            }
        
            return $mysql_array;
        }
    

    function query_count()
    {

        return $this->query_count;

    }

}
}

Posted: Thu Sep 28, 2006 9:58 pm
by Luke
man, you need to kinda go over the posing code guidelines or something... we aren't mind readers. "This code doesn't work" could mean a lot of things..

Posted: Thu Sep 28, 2006 10:07 pm
by GeXus
I actually just got it to work.. but thanks anyways!

Posted: Thu Sep 28, 2006 10:14 pm
by Luke
sorry I couldn't be of more help... but just so ya know, I wasn't trying to be rude at all... I just can't help you without a good description of what's wrong. :)

Posted: Fri Sep 29, 2006 2:29 am
by jmut
obviously, from code posted, the problem was scope ...not passing $db object or declaring it global.
@GeXus This would have been not problem detecting if you develop on error_reporting(E_ALL)
happy coding