vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Damn Classes /; (the php type) (https://vborg.vbsupport.ru/showthread.php?t=102289)

pyro.699 12-05-2005 07:30 PM

Damn Classes /; (the php type)
 
ok, im working on a vb free site and im setting up a code so that inorder to write/read a query i need to only type
PHP Code:

$db->query(); 

now, here is what i hvae built so far!
PHP Code:

class Blizz { var $blizz; }

//set the function
    
$this->connection $resource;
    (
$resource mysql_connect());
 function 
query($query

    
$resource mysql_query($query$this->connection); 
    if (!
$resource
    { 
        
$this->debug_query($query); 
    }

    return 
$resource;
}


$db = new Blizz($dbhost$dbuser$dbpass$dbname); 

ok,
PHP Code:

$dbhost
$dbuser
$dbpass
$dbname 

are all defined above... in the part of the sript that you can't handle!

why isent this working? ill give you my current error:
Quote:

Originally Posted by PHP 5.0.4
Fatal error: Using $this when not in object context in C:\apachefriends\xampp\htdocs\blizz\connect.php on line 35

i dont know if this matters, but line #35 is
Quote:

Originally Posted by Notepad ++
$this->connection = $resource;

anyways, i would ask AJ but, i feel like ive bugged him enough ^^;

Ty in advance
~Cody Woolaver

Guest190829 12-05-2005 07:40 PM

$this should only be used inside methods AFAIK...

*I'm still very new to OOP*

pyro.699 12-05-2005 07:43 PM

Im new aswell ^^

umm, so, where what should i put it inside of?

Guest190829 12-05-2005 08:02 PM

Well that function isn't even in your class, you're defining a class with no methods in it. I don't know if the below will work as I haven't tested it....nor am I an OOP guru, as I am still learning myself.
PHP Code:

var $resource =mysql_connect();

class 
Blizz 

//set the function
    
 
function query($query

    
$this->connection $resource;
    
$this->resource mysql_query($query$this->connection); 
    if (!
$this->resource
    { 
        
$this->debug_query($query); 
    }

    return 
$this->resource;
}


$db = new Blizz;
$query $db->query("QUERY"); 

And also what do you mean by a vb free site?

pyro.699 12-05-2005 08:09 PM

Update of code:
PHP Code:

class Blizz { var $blizz;
         var 
$resource mysql_connect(); }

//set the function
    
$this->connection $resource;
    (
$resource mysql_connect());
 function 
query($query

    
$resource mysql_query($query$this->connection); 
    if (!
$resource
    { 
        
$this->debug_query($query); 
    }

    return 
$resource;
}


//Connect
$db = new Blizz($dbhost$dbuser$dbpass$dbname); 

Quote:

Originally Posted by PHP 5.0.4
Parse error: parse error, expecting `','' or `';'' in C:\apachefriends\xampp\htdocs\blizz\connect.php on line 34

Line 34
Quote:

Originally Posted by Notepad ++
var $resource = mysql_connect(); }


Guest190829 12-05-2005 08:12 PM

Your closing the class before defining any methods, you have to wrap the closing bracket "}" at the end of all you methods.

pyro.699 12-05-2005 08:27 PM

yeah, i had a feeling that was an answer
Updated Script
PHP Code:

class Blizz { var $blizz;
            var 
$resource mysql_connect();

//set the function
        
$this->connection $resource;
        (
$resource mysql_connect());
    function 
query($query
    { 
        
$resource mysql_query($query$this->connection); 
        if (!
$resource
        { 
            
$this->debug_query($query); 
        }

        return 
$resource;
    }
}

//Connect
$db = new Blizz($dbhost$dbuser$dbpass$dbname); 

Quote:

Originally Posted by PHP 5.0.4
Parse error: parse error, expecting `','' or `';'' in C:\apachefriends\xampp\htdocs\php_testing\blizz\co nnect.php on line 28

Line 28:
Quote:

Originally Posted by Notepad ++
var $resource = mysql_connect();


AN-net 12-05-2005 09:14 PM

you can not run a function or set a var when initialzing it, except for setting it as an array. if you want to set that var as mysql_connect create a constructor function at the beginning of your class.

The Geek 12-05-2005 09:20 PM

Just to build on AN-nets excellent pointer:

PHP Code:

class myclass
{
 
var 
myvariable;
 
function 
myclass($var)
{
//your constructor is a function with the same name as your class
$this->myvariable $var;
}


HTH's

pyro.699 12-05-2005 09:26 PM

so, after that, is there anyhting else i need to do? or did i have a whole bunch of shit in mine that wsent neded?

----
ok, scrap what i just said

i am completely lost, can you guys amke me an example? that fully works!

i want to type
PHP Code:

$db->blizz->query("QUERY"); 

and for that query to be ran

you should see all the backup files ive made for this project.. lol.. ig to about 20... and none of em work! i just need this made, idc how much space it takes! i really need this ;) it will save me a lot of time!

AN-net 12-05-2005 10:29 PM

i dont know what version you are running but why not just call another instance of what ever class file vbulletin uses for mysql communication. no need to rewrite that redundant code.

pyro.699 12-05-2005 11:02 PM

im making another site for somone else, who dosent have vbulletin ^^

im using php version 5.0.4

Alan @ CIT 12-06-2005 08:48 AM

Something along the lines of the following should (untested) work:

PHP Code:

// The database class
class Blizz
{
// The database link pointer
var $link 0;
 
// The constructor - connect to the sql server and select the database
function Blizz($dbhost$dbuser$dbpass$dbname)
{
$this->link mysql_connect($dbhost$dbuser$dbpass);
mysql_select_db($dbname$this->link);
}
// Query the database
function query($query)
{
$result mysql_query($query$this->link); 
return 
$result;
}
}
// The code that you would use in your app
$db = new Blizz($dbhost$dbuser$dbpass$dbname);
$result $db->query("SELECT ... whatever ..."); 

You should probably add some sort of error checking / handling in there :)

AN-net 12-06-2005 07:52 PM

your not susposed to do var $link= 0;
you are susposed to set this in a constructor function, the first function of a class just used for setting variables.
I.E.:
PHP Code:

class foo
{
var 
$variable1;
var 
$variable2= array();
var 
$variable3;

function 
foobar()
{
$this->variable1'';
$this->variables3'george';
}



Guest190829 12-06-2005 07:54 PM

Don't you mean

PHP Code:

function foo()
{
// blah blah constructor


?

And I thought all variables have to be declared(not set) outside the class?

Adrian Schneider 12-06-2005 08:03 PM

Here is my DB class for one of my sites.

PHP Code:

<?php

class Database
{
    var 
$host;
    var 
$username;
    var 
$password;
    var 
$databasename;
    var 
$connection;
    
    
/**
     * Constructor Method
     * Initializes connection and selects database.
    **/
    
function Database($host$username$password$databasename)
    {
        
$this->host $host;
        
$this->username $username;
        
$this->password $password;
        
$this->databasename $databasename;

        
$this->connect($this->host$this->username$this->password);
    }
    
    
/**
     * Establishes MySQL connection.
    **/
    
function connect()
    {
        
$this->connection = @mysql_connect($this->host$this->username$this->password);
        if (!
$this->connection)
        {
            
$error = new Page;
            
$error->error('Could not establish database connection.</p><p>' mysql_error());
        }
        return 
$this->select_db($this->databasename);
    }
    
    
/**
     * Selects MySQL database.
    **/
    
function select_db()
    {
        
$resource = @mysql_select_db($this->databasename$this->connection);
        if (!
$resource)
        {
            
$error = new Page;
            
$error->error('Could not select database "' $this->databasename '".');
        }
        return 
$resource;
    }
    
    
/**
     * Executes a MySQL query.
    **/
    
function query($querystring)
    {
        if (
strpos($querystring"\t\t\t\t") !== false)
        {
            
$querystring str_replace("\t\t\t\t""\t\t"$querystring);
            
$querystring str_replace("\t\t\t""\t"$querystring);
        }
        
$resource mysql_query($querystring$this->connection);
        if (!
$resource)
        {
            
$this->debug_query($querystring);
        }
        return 
$resource;
    }
    
    
/**
     * Fetches rows from the database.
    **/
    
function fetch($result)
    {
        if (
mysql_num_rows($result))
        {
            
$resource mysql_fetch_assoc($result);
            return 
$resource;
        }
        return 
false;
    }
    
    
/**
     * Halts execution of the script and displays the problem.
    **/
    
function debug_query($query)
    {
        
$error = new Page;
        
$errormessage '<h2 class="queryfail">Failed Query:</h2><div style="background-color: #EEEEEE; width:640px; padding:5px; margin: 0 auto;">';
        
$errormessage .= '<pre class="query">' $this->highlight_sql($query) . '</pre></div>';
        if (
mysql_error())
        {
            
$errormessage .= '<h2 class="queryfailHeader">Reason:</h2>';
            
$errormessage .= '<p class="queryfailReason">' mysql_error() . '</p>';
        }

        
$error->error($errormessage'Database Error');
    }

    
/**
     * Highlights MySQL Code
    **/
    
function highlight_sql($query)
    {
        
$query preg_replace("/['\"]([^'\"]*)['\"]/i""'<span style='color: #FF6600'>$1</span>'"$query, -1);

        
$find = array(
            
'*',
            
'SELECT ',
            
'UPDATE ',
            
'DELETE ',
            
'INSERT ',
            
'CREATE ',
            
'TABLE ',
            
'INTO',
            
'VALUES',
            
'FROM',
            
'LEFT',
            
'JOIN',
            
'WHERE',
            
'LIMIT',
            
'ORDER BY',
            
'AND',
            
'OR ',
            
'DESC',
            
'ASC',
            
'ON ',
            
"\t\t\t",
            
'INT ',
            
'varchar ',
            
'VARCHAR ',
            
'NOT NULL',
            
'AUTO_INCREMENT',
            
'UNSIGNED',
            
'PRIMARY KEY '
        
);
        
        
$replace = array(
            
'<span style="#FF6600; font-weight: bold;">*</span>',
            
'<span style="color: #00AA00; font-weight: bold;">SELECT </span>',
            
'<span style="color: #00AA00; font-weight: bold;">UPDATE </span>',
            
'<span style="color: #00AA00; font-weight: bold;">DELETE </span>',
            
'<span style="color: #00AA00; font-weight: bold;">INSERT </span>',
            
'<span style="color: #00AA00; font-weight: bold;">CREATE </span>',
            
'<span style="color: #00AA00; font-weight: bold;">TABLE </span>',
            
'<span style="color: #00AA00; font-weight: bold;">INTO</span>',
            
'<span style="color: #00AA00; font-weight: bold;">VALUES</span>',
            
'<span style="color: #00AA00; font-weight: bold;">FROM</span>',
            
'<span style="color: #00CC00; font-weight: bold;">LEFT</span>',
            
'<span style="color: #00CC00; font-weight: bold;">JOIN</span>',
            
'<span style="color: #00AA00; font-weight: bold;">WHERE</span>',
            
'<span style="color: #AA0000; font-weight: bold;">LIMIT</span>',
            
'<span style="color: #00AA00; font-weight: bold;">ORDER BY</span>',
            
'<span style="color: #0000AA; font-weight: bold;">AND</span>',
            
'<span style="color: #0000AA; font-weight: bold;">OR </span>',
            
'<span style="color: #0000AA; font-weight: bold;">DESC</span>',
            
'<span style="color: #0000AA; font-weight: bold;">ASC</span>',
            
'<span style="color: #00DD00; font-weight: bold;">ON </span>',
            
"\t",
            
'<span style="color: #0000AA; font-weight: bold;">INT </span>',
            
'<span style="color: #0000AA; font-weight: bold;">varchar </span>',
            
'<span style="color: #0000AA; font-weight: bold;">VARCHAR </span>',
            
'<span style="color: #0000AA; font-weight: bold;">NOT NULL</span>',
            
'<span style="color: #0000AA; font-weight: bold;">AUTO_INCREMENT</span>',
            
'<span style="color: #0000AA; font-weight: bold;">UNSIGNED</span>',
            
'<span style="color: #0000AA; font-weight: bold;">PRIMARY KEY </span>',
        );
        return 
str_replace($find$replace$query);
    }
    
    
/**
     * Escapes data when going into the database.
     * This is not for SQL Injections.
    **/
    
function escape($value)
    {
        if (!
is_int($value))
        {
            if (
is_numeric($value))
            {
                
$value floatval($value);
            }
            else
            {
                
$value = (get_magic_quotes_gpc()) ? $value  addslashes($value);
            }
            
$value "'" $value "'";
        }
        return 
$value
    }
    
    
/**
     * Checks how many rows were found from a query.
    **/
    
function num_rows($resource)
    {
        return 
mysql_num_rows($resource);
    }
    
    
/**
     * Insert data into the database.
     * Generates query based on $data array (field => val)
    **/
    
function insert($table$data)
    {
        
// Field Names
        
$columnbit '';
        
$columns array_keys($data);
        for (
$i=0$i<count($columns); $i++)
        {
            
$columnbit .= (($i) ? ', ' '') . '`' $columns[$i] . '`';
        }

        
// Values
        
$valuebit '';
        
$values array_values($data);
        for (
$i=0$i<count($values); $i++)
        {
            
$value $this->escape($values[$i]);
        
            
$valuebit .= "\t\t\t\t" $value . (($i != count($values)-1) ? ', ' "\n" '');
        }
        
        
// Run Query
        
$this->query('
            INSERT INTO ' 
$table.  '
                (' 
$columnbit ')
            VALUES (' 
"\n" .  
                
$valuebit '
            )'
        
);
            
    }
    
    
/**
     * Update data in the database.
     * Generates query based on $data array (field => val)
    **/
    
function update($table$data$whereclause)
    {
        
$valuestofields '';
        
$i 0;
        foreach (
$data as $field => $value)
        {
            
$i++;
            
$valuestofields .= $field ' = ' $this->escape($value) . (($i != count($data)) ? ', ' '') . "\n\t\t\t\t";
        }
        
// Run Query
        
$this->query('
            UPDATE ' 
$table.  '
            SET' 
"\n\t\t\t\t" 
                
$valuestofields 
            
$whereclause
        
);
            
    }
    

}


?>

Does anyone know of a good SQL syntax highlighter (that doesn't require installing additional libraries)?

[edit]Usage being:
PHP Code:

$db = new Database('server''user''pass''db'); 

just replace the error handling method ... mine uses another class for displaying things.

AN-net 12-07-2005 02:57 AM

well yes all variables are delcared such as var $foobar; but setting them to anything other than an empty array is not allowed when not in the constructor function.


All times are GMT. The time now is 03:37 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01256 seconds
  • Memory Usage 1,914KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (13)bbcode_php_printable
  • (6)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (17)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete