【问题标题】:How do I Connect an android application to a running MySQL Database, on an external hosted server如何在外部托管服务器上将 android 应用程序连接到正在运行的 MySQL 数据库
【发布时间】:2013-05-22 12:43:11
【问题描述】:

基本上,我已经做了很多搜索,但仍然没有找到任何解决问题的方法。

我正在尝试将我的应用程序连接到外部服务器上由公司运行和托管的数据库(我们已经将此数据库用于我们在用户计算机上运行的应用程序)

我想查询数据库的登录信息,这样当用户在我的应用程序上输入他的用户名和密码时,它会将这些详细信息与数据库中的详细信息进行比较,然后回复,说明这些凭据是否正确,或者不,并允许用户查看不同的页面,该页面将从数据库中为该用户提取特定详细信息(但这是我将来需要解决的问题)

我目前的主要问题是,我似乎无法让应用程序能够连接到数据库,目前我正在使用 WAMP 服务器来托管我制作的 php 文件...

注意:我将使用“user”和“pass”而不是我拥有的实际用户名和密码

<?php

//connecting
//server, username, password, database

$dbhost = 'external ip entered here';
$dbuser = 'user';
$dbpass = 'pass';
$dbdb   = 'datbasename';

//connect to mySQL 
$connect = mysql_connect ($dbhost,$dbuser,$dbpass) or die("Connection error");

//select the database
mysql_select_db($dbdb) or die ("database selection error");

//Retrieve the login details via POST
$username=$_POST['username'];
$password=$_POST['password'];

//Query the table
$query="SELECT llogint,lpasint FROM TCHAUFF (nolock) 
WHERE llogint='$username' AND lpasint='$password'";

//check if there any results returned
$num = mysql_num_rows($query);

//if a record was found matching the details entered in the query
if($num == 1){
//create a while loop that places the returned data into an array
while($list=mysql_fetch_assoc($query)){

//store the returned data into variable
$output = $list;

//encode the returned data in JSON format
echo json_encode( $output );
//close the connection
mysql_close();
}}
?>

老实说,我不认为他们的 php 文件有任何问题,但如果有,请纠正我,哈哈!

我遇到的问题是,如果我尝试通过将文件放入 www 文件夹并转到 localhost/androidtest.php 来从 WAMP 运行这个 php 文件

我收到一条错误消息警告:mysql_connect():无法建立连接,因为目标机器主动拒绝了它

我已经尝试了所有我能找到的解决方案,

将此信息添加到 config.inc.php 文件中

/* Server: UserSeaCogi[1] */
$i++;
$cfg['Servers'][$i]['verbose'] = 'external database';
$cfg['Servers'][$i]['host'] = 'external ip entered here';
$cfg['Servers'][$i]['port'] = '';
$cfg['Servers'][$i]['socket'] = '';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['auth_type'] = 'cookie';
$cfg['Servers'][$i]['user'] = 'user';
$cfg['Servers'][$i]['password'] = 'pass';
$cfg['Servers'][$i]['AllowNoPassword'] = true;

我迷失了想法!

我添加了 绑定地址 = 0.0.0.0 到 my.ini 文件

但我认为这与我设置 WAMP 的方式有关,但我似乎找不到要更改的内容。

我也知道我绝对可以访问数据库,因为如果我将它添加到 Visual Studio,我可以从中运行查询......需要注意的一点......它是 2003 年的服务器......我知道有一定的Visual Studio 的限制不确定是否适用于 android?

提前致谢

【问题讨论】:

标签: php android mysql apache wamp


【解决方案1】:

在你的 php 代码中

mysql_select_db($dbdb) 

应该是

mysql_select_db($dbdb,$connect) ;

然后添加这些行

$result = mysql_query($query, $connect);
$num = mysql_num_rows($result);

【讨论】:

  • 网络IP是指我的IP吗?我尝试连接的数据库的 IP 还是我的外部 IP?
  • 您好,感谢您的回复/编辑我添加了行并进行了更改,但仍然出现相同的错误,这令人沮丧!哈哈!
猜你喜欢
  • 1970-01-01
  • 2014-03-17
  • 1970-01-01
  • 2016-07-15
  • 2012-04-03
  • 1970-01-01
  • 1970-01-01
  • 2017-09-14
  • 1970-01-01
相关资源
最近更新 更多