سؤال

I have a drupal website with a mysql database with some data that i would like to access from another website that is not drupal. Anyone who knows how to do that? Any help is much appreciated. I'm new to drupal and not into all the specific drupal terms yet.

EDIT: I have tried this form of connection on the non-drupal website:

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

but with no result. The domains are different, but both located on the same server.

هل كانت مفيدة؟

المحلول

Step 1 : Make the connections :

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

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

Step 2 : Select the database to work with,

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

Step 3 : Now execute the query and get the result,

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

Step 4 : Don't forget to close the database connection,

//close the connection
mysql_close($dbhandle);

Here is a code in full:

<?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);
?>

Source :http://webcheatsheet.com/php/connect_mysql_database.php

Note : If you have mysql running on different port then do this,

mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top