从另一个域访问drupal mysql

时间:2014-05-07 13:36:01

标签: mysql drupal-7

我有一个带有mysql数据库的drupal网站,其中包含一些我希望从另一个不是drupal的网站访问的数据。谁知道怎么做? 任何帮助深表感谢。我是drupal的新手,而不是所有特定的drupal术语。

编辑:我在非drupal网站上尝试过这种形式的连接:

$connection = new mysql('localhost', '$username', '$password', '$database_name', 3306);

但没有结果。域名不同,但都位于同一台服务器上。

1 个答案:

答案 0 :(得分:1)

第1步:建立连接:

$username = "your_name"; $password = "your_password"; $hostname = "localhost"; 

//connection to the database 
$dbhandle = mysql_connect($hostname, $username, $password)

步骤2:选择要使用的数据库,

$selected = mysql_select_db("examples",$dbhandle) 
  or die("Could not select examples");

步骤3:现在执行查询并获得结果

$result = mysql_query("SELECT id, model, year FROM cars");

步骤4:不要忘记关闭数据库连接,

//close the connection
mysql_close($dbhandle);

以下是完整的代码:

<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
 or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

//select a database to work with
$selected = mysql_select_db("examples",$dbhandle) 
  or die("Could not select examples");

//execute the SQL query and return records
$result = mysql_query("SELECT id, model,year FROM cars");

//fetch tha data from the database 
while ($row = mysql_fetch_array($result)) {
   echo "ID:".$row{'id'}." Name:".$row{'model'}."Year: ". //display the results
   $row{'year'}."<br>";
}
//close the connection
mysql_close($dbhandle);
?>

来源:http://webcheatsheet.com/php/connect_mysql_database.php

注意:如果你在不同的端口上运行mysql,那么就这样做,

mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');