Pokouším se připojit přes android k MySQL databázi. Dočetl jsem, že se nelze připojit přímo. Tak dělám PHP serv, který mi do android pošle dané data z MySQL. MySQL server mám na Synology Diskstation. Při pokusu o připojení přes test.php se ohlásí error.
ERROR: Warning: mysql_connect(): php_network_getaddresses: gethostbyname failed. errno=0 in /volume1/web/servr/db_connect.php on line 28 Warning: mysql_connect(): php_network_getaddresses: gethostbyname failed. errno=0 in /volume1/web/servr/db_connect.php on line 28 php_network_getaddresses: gethostbyname failed. errno=0 Warning: mysql_close(): no MySQL-Link resource supplied in /volume1/web/servr/db_connect.php on line 42
db_config.php
<?php
/*
* All database connection variables
*/
define('DB_USER', "root"); // db user
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', "test"); // database name
define('DB_SERVER', "http://192.168.1.6/phpMyAdmin/index.php"); // db server
?>
db_connect.php
<?php
/**
* A class file to connect to database
*/
class DB_CONNECT {
// constructor
function __construct() {
// connecting to database
$this->connect();
}
// destructor
function __destruct() {
// closing db connection
$this->close();
}
/**
* Function to connect with database
*/
function connect() {
// import database connection variables
require_once __DIR__ . '/db_config.php';
// Connecting to mysql database
$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
// Selecing database
$db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
// returing connection cursor
return $con;
}
/**
* Function to close db connection
*/
function close() {
// closing db connection
mysql_close();
}
}
?>
test.php
<?php
//Create Database connection
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
if (!$db) {
die('Could not connect to db: ' . mysql_error());
}
mysql_select_db("Data")or die("cannot select DB");
//Replace * in the query with the column names.
$result = mysql_query("SELECT * FROM customer", $db);
?>