By dalearyous on
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
What are you trying to
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
this is what i am trying to do
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.
You should not need to
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.
thanks for your help! this is
thanks for your help!
this is what i was doing:
Instead of mysql_query() you
Instead of mysql_query() you want to use db_query(). And the "or die(...)" is not a very user friendly approach to handling errors.
ah i see!
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
See the example #1 on the
See the example #1 on the db_query() page. The API documentation is at http://api.drupal.org/api/drupal/6