Posted By

wza on 05/04/09


Tagged

mysql php textmate drupal


Versions (?)

mysql connect from drupal settings.php


 / Published in: PHP
 

  1. function query($sql){
  2. include("settings.php");
  3. $connect = explode("@",$db_url);
  4. $login = explode("//",$connect[0]);
  5. $login = explode(":",$login[1]);
  6. $hostinfo = explode("/",$connect[1]);
  7. $conxion = mysql_connect($hostinfo[0], $login[0], $login[1]);
  8. $result=mysql_db_query($hostinfo[1],$sql,$conxion);
  9. return $result;
  10. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: troynt on May 26, 2009

Alternatively you could use Pear::DB pear install db

Posted By: troynt on May 26, 2009

Comment got cut off.

requireonce('DB.php'); $db = &DB::Connect($dburl, array()); if( PEAR::isError($db) ) { die( $db->getMessage() ); }

$res = $db->query($sql); $res->fetchInto( $row, DBFETCHMODEASSOC ); while( $row ) { $rows[] = $row; $res->fetchInto( $row, DBFETCHMODEASSOC ); }

Posted By: kevinherrington on December 8, 2009

I have a few nicer ways to do this.

function query($sql) { includeonce('./includes/bootstrap.inc'); drupalbootstrap(DRUPALBOOTSTRAPDATABASE); return db_query($sql); }

(Don't worry, Drupal is intelligent enough to not bootstrap itself over and over there.)

Or if you really don't want to use Drupal for your queries:

function query($sql) { static $connection; includeonce('./sites/default/settings.php'); includeonce('./includes/database.mysql.inc'); if (empty($connection)) { $connection = dbconnect($dburl); } return mysql_query($sql, $connection); }

You need to login to post a comment.