i am just learning how to code for oop and i have an error that i have not seen

Fatal error: Class 'queryUpdate' not found in testingfile.php on line 28


line 28

$queryUpdate = new queryUpdate($table, $fields, $condition);

what i am trying to run. which is in another file that is brought in with an include

public function queryUpdate($table, $fields, $condition){
	
	$values = "";
		if($fields != ''){
		foreach ($fields as &$value) {
			${$value} = $_POST[$value];
			$values .= $value."='$".$value."', ";
			}
			}
			
			// remove our trailing space and , 
        $fields = substr($values, 0, -2);
		
		$query = "UPDATE $table SET $fields WHERE $condition ";


		$result = mysql_query($query);

}

thanks in advance

queryUpdate is a function, not a class. Read more here.

here yo go, chew on this for a while, I didn't really test any of it but it should give you kind of an idea of the purpose of a class and maybe something to start with.

<?php
class MyDBClass
{
    private $arrDBConns = array();
    public $arrErrors = array();

    function __construct()
    {

    }

    //define the database connection info, the purpose is to support multiple databases and users
    //if a connection is created it is kept alive for the duration of the script
    //something can be added later to close if necessary but it may be faster just to leave it open
    function getDBInfo($strDbType)
    {
        $arrDBInfo = array();
        $arrDBInfo["dbname1"] = array("username"=>"my_db_user1", "pass"=>"my_db_pass1", "server"=>"servername");
        $arrDBInfo["dbname2"] = array("username"=>"my_db_user2", "pass"=>"my_db_pass2", "server"=>"servername");
        $arrDBInfo["dbname3"] = array("username"=>"my_db_user3", "pass"=>"my_db_pass3", "server"=>"servername");
        $arrDBInfo["dbname4"] = array("username"=>"my_db_user4", "pass"=>"my_db_pass4", "server"=>"servername");

        if(in_array($strDbType, $arrDBInfo))
        {
            return $arrDBInfo[$strDbType];
        }
        else
        {
            $arrErrors[] = "db type not available";
            return false;
        }
    }

    //connect to the requested database
    function dbConn($strDbType)
    {
        if(isset($this->arrDBConns[$strDbType]) && $this->arrDBConns[$strDbType] != false) return true;

        $this->arrDBConns[$strDbType] = false;

        if(!$this->arrDBConns[$strDbType] = mysql_connect($this->arrDBInfo[$strDbType]["server"], $this->arrDBInfo[$strDbType]["username"], $this->arrDBInfo[$strDbType]["pass"])) $arrErrors[] = "could not connect";
        if(!mysql_select_db($strDbType, $this->arrDBConns[$strDbType])) $arrErrors[] = "could not select db";

        return !$this->arrDBConns[$strDbType]?false:true;
    }

    //run update query: params - database type, table name, fields array, condition
    function queryUpdate($strDbType, $table, $fields, $condition)
    {
        if(!$this->dbConn($strDbType)) return false;

        $values = "";
        if($fields != '')
        {
            foreach ($fields as &$value)
            {
                ${$value} = $_POST[$value];
                $values .= $value."='$".$value."', ";
            }
        }

        // remove our trailing space and , 
        $fields = substr($values, 0, -2);

        $this->strQuery = "UPDATE $table SET $fields WHERE $condition";
        return mysql_query($query, $this->arrDBConns[$strDbType]);
    }
}

$strDbType = "dbname3";

$objDB = new myDBClass();
$objDB->queryUpdate($strDbType, $table, $fields, $condition);

if(count($objDB->arrErrors) == 0)
{
    echo "we can hope that everything went well";
}
else
{
    echo "<pre>" . print_r($objDB->arrErrors, true) . "</pre>";
}
?>

im chewing but it is taking me longer cause i am learning this at the same time.

the dbConn is pretty complex. i wouldnt be able to actually write something like that yet but i understand it.

i think i understand this.

return mysql_query($query, $this->arrDBConns[$strDbType]);

it just means run the mysql_query with these terms and connect to the db with this

but what i am not understanding is how you actually get the database info. i dont see you actually calling getDBInfo anywhere. imo i would think that it would be put in the __construct() then just stores the correct info

so would i always call the class in a new instance, then from the instance i call the specific function along with the passing the data.

You are right, that I would put into the constructor and I would also remove the parameter from line 15. I will do that sometimes, I will start a class thinking that it will work one way and then while I am writing I am constantly thinking that it would be nice if it had this functionality and that functionality so throughout the process I always end up with one or two disconnects that have to be ironed out. This is what happens when I don't take the time to plan out the class and determine requirements prior to writing it and just go at it half cocked.

The reason I wrote it this way is because I always set up a different user for each database, that way it helps to isolate security holes. So when you call your update function, the object will check to see if it has a live connection to that database and if not it will create at that time.

[edit]: I also added an array definition for $this->arrDBInfo on line 6 and changed the dbinfo function to use the class property $this->arrDBInfo instead of $arrDBInfo because $arrDBInfo would not be accessible outside of that method where as $this->arrDBInfo is accessible throughout the class.

I have applied the changes here:

<?php
class MyDBClass
{
    private $arrDBConns = array();
    public $arrErrors = array();
    private arrDBInfo = array();

    function __construct()
    {
        $this->getDBInfo();
    }

    //define the database connection info, the purpose is to support multiple databases and users
    //if a connection is created it is kept alive for the duration of the script
    //something can be added later to close if necessary but it may be faster just to leave it open
    function getDBInfo()
    {
        $this->arrDBInfo["dbname1"] = array("username"=>"my_db_user1", "pass"=>"my_db_pass1", "server"=>"servername");
        $this->arrDBInfo["dbname2"] = array("username"=>"my_db_user2", "pass"=>"my_db_pass2", "server"=>"servername");
        $this->arrDBInfo["dbname3"] = array("username"=>"my_db_user3", "pass"=>"my_db_pass3", "server"=>"servername");
        $this->arrDBInfo["dbname4"] = array("username"=>"my_db_user4", "pass"=>"my_db_pass4", "server"=>"servername");
    }

    //connect to the requested database
    function dbConn($strDbType)
    {
        if(isset($this->arrDBConns[$strDbType]) && $this->arrDBConns[$strDbType] != false) return true;

        $this->arrDBConns[$strDbType] = false;

        if(!$this->arrDBConns[$strDbType] = mysql_connect($this->arrDBInfo[$strDbType]["server"], $this->arrDBInfo[$strDbType]["username"], $this->arrDBInfo[$strDbType]["pass"])) $arrErrors[] = "could not connect";
        if(!mysql_select_db($strDbType, $this->arrDBConns[$strDbType])) $arrErrors[] = "could not select db";

        return !$this->arrDBConns[$strDbType]?false:true;
    }

    //run update query: params - database type, table name, fields array, condition
    function queryUpdate($strDbType, $table, $fields, $condition)
    {
        if(!$this->dbConn($strDbType)) return false;

        $values = "";
        if($fields != '')
        {
            foreach ($fields as &$value)
            {
                ${$value} = $_POST[$value];
                $values .= $value."='$".$value."', ";
            }
        }

        // remove our trailing space and , 
        $fields = substr($values, 0, -2);

        $this->strQuery = "UPDATE $table SET $fields WHERE $condition";
        return mysql_query($query, $this->arrDBConns[$strDbType]);
    }
}

$strDbType = "dbname3";

$objDB = new myDBClass();
$objDB->queryUpdate($strDbType, $table, $fields, $condition);

if(count($objDB->arrErrors) == 0)
{
    echo "we can hope that everything went well";
}
else
{
    echo "<pre>" . print_r($objDB->arrErrors, true) . "</pre>";
}
?>

sweet. not bad for learning on my own in 2 days. eh?

thanks alot you have helped me learn. if i have any more questions or query's...haha... i will post them here.

thanks again

just fixing another syntax error:

<?php
class MyDBClass
{
    private $arrDBConns = array();
    public $arrErrors = array();
    private $arrDBInfo = array();

    function __construct()
    {
        $this->getDBInfo();
    }

    //define the database connection info, the purpose is to support multiple databases and users
    //if a connection is created it is kept alive for the duration of the script
    //something can be added later to close if necessary but it may be faster just to leave it open
    function getDBInfo()
    {
        $this->arrDBInfo["dbname1"] = array("username"=>"my_db_user1", "pass"=>"my_db_pass1", "server"=>"servername");
        $this->arrDBInfo["dbname2"] = array("username"=>"my_db_user2", "pass"=>"my_db_pass2", "server"=>"servername");
        $this->arrDBInfo["dbname3"] = array("username"=>"my_db_user3", "pass"=>"my_db_pass3", "server"=>"servername");
        $this->arrDBInfo["dbname4"] = array("username"=>"my_db_user4", "pass"=>"my_db_pass4", "server"=>"servername");
    }

    //connect to the requested database
    function dbConn($strDbType)
    {
        if(isset($this->arrDBConns[$strDbType]) && $this->arrDBConns[$strDbType] != false) return true;

        $this->arrDBConns[$strDbType] = false;

        if(!$this->arrDBConns[$strDbType] = mysql_connect($this->arrDBInfo[$strDbType]["server"], $this->arrDBInfo[$strDbType]["username"], $this->arrDBInfo[$strDbType]["pass"])) $arrErrors[] = "could not connect";
        if(!mysql_select_db($strDbType, $this->arrDBConns[$strDbType])) $arrErrors[] = "could not select db";

        return !$this->arrDBConns[$strDbType]?false:true;
    }

    //run update query: params - database type, table name, fields array, condition
    function queryUpdate($strDbType, $table, $fields, $condition)
    {
        if(!$this->dbConn($strDbType)) return false;

        $values = "";
        if($fields != '')
        {
            foreach ($fields as &$value)
            {
                ${$value} = $_POST[$value];
                $values .= $value."='$".$value."', ";
            }
        }

        // remove our trailing space and , 
        $fields = substr($values, 0, -2);

        $this->strQuery = "UPDATE $table SET $fields WHERE $condition";
        return mysql_query($query, $this->arrDBConns[$strDbType]);
    }
}

$strDbType = "dbname3";

$objDB = new myDBClass();
$objDB->queryUpdate($strDbType, $table, $fields, $condition);

if(count($objDB->arrErrors) == 0)
{
    echo "we can hope that everything went well";
}
else
{
    echo "<pre>" . print_r($objDB->arrErrors, true) . "</pre>";
}
?>

dont know what you changed but cool. i have never actually ran this script. im just reading it and learning.

hi again,

i grabbed your code for the getDBinfo and dbConn as i cant write me own as of now. and changed the DB info as needed but i am running into these 2 errors.

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'crashpix'@'localhost' (using password: NO) in /query.class.php on line 31

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in query.class.php on line 32

at least its a progression.

you could try hard coding the user, pass, server and database name, just to see if the logic in the class is a little off. Also, make sure that you are calling the method "queryUpdate" as $objDbObject->queryUpdate("database_name", $table, $fields, $condition);

I am pretty sure that my logic is correct though but give it a shot.

well i did hard code it in and found where my problem was. not i am really getting to understand that. but when i get not errors or anything my DB entry does not update. i have put the if statement to tell me that it went well.

this is what i have.

$table = "users";
$name = "bob";
$fields = array("name");
$condition = "WHERE company_id='999'";
$strDbType = "*omitted*";

$query = new Query();

$query->queryUpdate($strDbType, $table, $fields, $condition);

from my understanding it that the class will run in the place where i call it and can grab the previous variables that i set. is this true or do i have to send everything to the class?

it is designed so that all parameters ($strDbType, $table, $fields, $condition) are required every time you call that method but it is really just a duplication of what you submitted to the forum in the beginning and can be modified however you see fit.

damn me. instead of $this->strQuery in the mysql_query. i had my original $query variable there.

so it wasnt actually executing the query that i created. but i was able to debug this so i know i got the hang of it.

Seriously i thank you lots.. haha. you helped me better improve my way of coding in less than a week. that is a new record for me.

ok i got another question that im trying to look for the answer but coming up short.

what im thinking is to be able to write a function within the class that outputs all the posted $_POST like this

function getPostData(){
	
            	foreach ($_POST as $key => $value)
            	{
					${$key}."='{$value}', 555";
            	}
	}

but is it possible to return those values outside of the function right below where it was called without having to write them all out?

i hope i make sense

ok i got another question that im trying to look for the answer but coming up short.

what im thinking is to be able to write a function within the class that outputs all the posted $_POST like this

function getPostData(){
	
            	foreach ($_POST as $key => $value)
            	{
					${$key}."='{$value}', 555";
            	}
	}

but is it possible to return those values outside of the function right below where it was called without having to write them all out?

i hope i make sense

Tell me if I am off but it looks like you are just trying to display the post array in a readable manor. If that is the case I will usually just do this:

echo "<pre>" . print_r($_POST, true) . "</pre>";

However if you insist on sticking this in a class, in order to keep things organized I would stick this in a whole new class that has to do with header(get, post, querystring and url) manipulation.

for ex:

class headerManager
{
    function __construct()
    {   
            
    }   

    function getHeaderVarString($arrType = false)
    {   
        if(!is_array($arrType)) $arrType = array("POST");

        $strOutPut = ""; 

        foreach($arrType as $key=>&$value) //using & before $value to loop by reference, prevents php from creating a duplicate array
        {   
            $strOutPut .= "_" . $value . "<br />";

            global ${"_" . $value}; //don't ask because I don't know, but in order to access a super global array through a variable array in a function it must be defined as global even though you can reference it by ${"_POST"} and it would be just fine.  Some funky scope thing and you can add that to the list of things that I just don't get

            foreach(${"_" . $value} as $innerKey=>&$innerValue)
            {   
                $strOutPut .= "&nbsp;&nbsp;&nbsp;&nbsp;" . $innerKey . "=" . $innerValue . "<br />";
            }   
        }   

        return $strOutPut;
    }   
}

$headerMan = new headerManager();
echo $headerMan->getHeaderVarString(array("POST", "GET", "COOKIE", "SESSION"));

yes that is what i am looking for.

i really dont like the scope of things. if scopes were more broad then things would be a little easier.

so if i understand this correctly this takes any recently post items and then returns them like $name = $_POST[name]; to the place that you called it from. and from there i could take it and have it go right into another function.

yep, you can request any combination of post, get, session or cookie and it will show the variable names and values as:
_POST
key1=value1
key2=value2
_GET
key1=value1
key2=value2
key3=value3

ok cool i will dive into it more.

one thing though. does making it a global var end after the script is run. or is it like a session?

Yes, even though you declare a variable global, it is still destroyed at the end of the script.

Declaring a variable global doesn't actually change the variable at all. It merely allows that one single function to have access to that variable, otherwise anything(with the exception of super globals and constants) declared outside of that function is inaccessible to that function. This is why it doesn't make sense to me to have to use it in this context because $_POST is a super global. In order to use a variable super global array inside of a function it must be declared global in that function. Which is the reason for my comment, you can access the $_POST array inside of the function but in order to use $varname = "_POST"; ${$varname} you must declare ${$varname} as global. It doesn't make much sense. You will find strange stuff like this with PHP as your PHP knowledge increases, I actually have a list of odd things that don't make much sense in PHP.

first off why do we declare that $arrType = false when it starts.

also couldnt we just write it this way?

function getHeaderVarString($arrType = false)
    {   
        if(!is_array($arrType)) $arrType = array("POST");

        foreach($arrType as $key=>&$value) 
        {   

            global ${$key} = $value;  
        }   
    }

imo, if we are declaring it a global so we could just cut the bloat. or is global only for variables and then we declare them later.

better yet couldnt we just do

static ${$key} = $value;

$arrType is not set to false from the beginning, but this is a way in php to provide a default. so if nothing is passed to the method then $arrType is false, otherwise $arrType is whatever is passed in. Then this line:

if(!is_array($arrType)) $arrType = array("POST");

sets it to array("POST") if it is not already an array. So if nothing is passed into the function, false is not an array so $arrType becomes array("POST"). I would much rather do this, which would be essentially the same thing:

function getHeaderVarString($arrType = array("POST"))

but you are not allowed to call functions inside the perens of a function/method definition.

function getHeaderVarString($arrType = false)
    {   
        if(!is_array($arrType)) $arrType = array("POST");

        foreach($arrType as $key=>&$value) 
        {   

            global ${$key} = $value;  
        }   
    }

Here it looks like you are trying to overwrite the post array except you are missing the "_" in global ${"_" . $key} = $value. I don't know what would happen if you defined the variable as static. I have worked with static methods which are defined as such inside of classes but I have not defined variables as static so I don't know what would happen.

Also, I am judging by this line "global ${$key} = $value;" that you may still be misunderstanding what the word global actually does when called in a function or method. Defining a variable global is equivalent to passing the variable as a parameter by reference. So for example, passing a variable by reference:

$foo = "0";
modFoo($foo);
echo $foo; //would output "1" because the variable is passed by reference

function modFoo(&$bar) //& before the parameter is the syntax for passing by reference
{
    $bar = "1";
}

The following is essentially the same thing

$foo = "0";
modFoo();
echo $foo; //would output "1" because of the global definition in the function

function modFoo()
{
    global $foo; //if not defined global, $foo would be inaccessible to this function
    $foo = "1";
}

And in order to output this:
_POST
key1=value1
key2=value2
_GET
key1=value1
key2=value2
key3=value3
from a class method, the class that I wrote is the way that I would do it.

so is _ to define a global variable like you are supposed to do with private and protected variables?

cause i dont see much sense in make in the variable be called $_name instead of just $name

Ok instead of defining a variable as a global. How would i go about getting a variable out of a function.

for example i have

$content = $_POST['content'];
			
			require_once("query.class.php");
			
			$query = new Query;
			
			$query->cleanHTML($content);

and within that function there is a return $html. how do i grab that. do i just do this? $html = $query->cleanHTML($content);

Correct.

cool just checking. It worked for me that way but i didnt know if that was the proper way of calling it.

thanks

Ok so im trying to test and implement that global post Grabber and im running into problems. It is not grabbing any of the posted data

how im calling it and what i am doing

require_once("headers.class.php");
			
			$headerMan = new headerManager();
			$headerMan->getHeaderVarString(array("POST"));
			
			require_once("query.class.php");
			
			$query = new Query;
			$html = $query->cleanHTML($content);
			$content = array("content"=>$html, "title"=>$title, "company_id"=>$company_id);
			$query->queryInsert("dbName", "forms", $content);
						
			echo'Success...Please Wait';
			echo '<code><script language=javascript>setTimeout(\'parent.$.fn.colorbox.close();\', 1500);</script></code>';

any thoughts?

Am not entirely sure but I think you should replace:

array("POST")

with:

$_POST

hmm. still doesn't seem to be working either. also im getting errors with the script


Warning: Invalid argument supplied for foreach() in headers.class.php on line 21

As i didnt write it i dont know what is wrong with it

hmm. still doesn't seem to be working either. also im getting errors with the script


Warning: Invalid argument supplied for foreach() in headers.class.php on line 21

As i didnt write it i dont know what is wrong with it

Post the code that you have in headers.class.php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.