i am a bit confused to as why a custom php field requires me to connect to the database ... hasn't that already happened?

is there a shortcut or do i have to do something like this: (obviously with variables defined before)

//connect to server
$ms = mysql_connect(HOST, DB_USERNAME, DB_PASSWORD);
if ( !$ms )
{
	echo "Error connecting to server.\n";
}

//connect to database
$dbconnect = mysql_select_db(DB_NAME);
if ( !$dbconnect )
{
	echo "Error connecting to database.\n";
}

i would hate to have to include this on every view that i use a custom php field

Comments

aburrows’s picture

What are you trying to do?
Don't use PHP inside of Drupal, its a security risk, either hook into it or add relevant php into the view tpl files

dalearyous’s picture

I am trying to do certain calculations and outputs in fields. I have tried several alternatives with no luck.

like:
SELECT SUM(something) WHERE these items are true

But the view will have several fields with several different calculations.

nevets’s picture

You should not need to connect to the database. It would help to see your code, but I suspect you are trying to use PHP/mysql functions when you want to be using the Drupal DB API.

dalearyous’s picture

thanks for your help!

this is what i was doing:

 mysql_query("SELECT SUM(field_cost_budget_value) FROM content_type_financials_summary")
 or die(mysql_error()); 
nevets’s picture

Instead of mysql_query() you want to use db_query(). And the "or die(...)" is not a very user friendly approach to handling errors.

dalearyous’s picture

ah ok, is there a list of ... i guess syntax for stuff like this in drupal?

how would i display the db_query?

... and the or die is just a dirty way to test things out

nevets’s picture

See the example #1 on the db_query() page. The API documentation is at http://api.drupal.org/api/drupal/6