使用PHP MySQL进行Android登录和注册

本教程介绍如何在Android应用中实现登录和注册功能。首先,通过XAMPP在Mac OS X上设置本地Web服务器和MySQL数据库。接着,使用PHP连接到数据库。在Android端,开发登录注册应用,通过PHP脚本处理服务器端的数据交互。
摘要由CSDN通过智能技术生成

Android Login and Registration are very common scenarios. You will find registration and login operation in all the apps where we want user information. In this tutorial, we’ll set up a local web server and MySQL database. We will develop android login and registration application. We will use PHP script to connect to the MySQL database.

Android登录和注册是非常常见的情况。 您会在所有需要用户信息的应用程序中找到注册和登录操作。 在本教程中,我们将设置本地Web服务器和MySQL数据库。 我们将开发android登录和注册应用程序。 我们将使用PHP脚本连接到MySQL数据库。

Android登录注册 (Android Login Registration)

The first step is to create the backend web server. I am working on Mac OS X and XAMPP can be used to set up a local Apache web server and MySQL database quickly.

第一步是创建后端Web服务器。 我在Mac OS X上工作,并且XAMPP可用于快速设置本地Apache Web服务器和MySQL数据库。

设置XAMPP服务器 (Setting Up XAMPP Server)

XAMPP(or WAMP) is a one-click installer software that creates an environment for developing a PHP, MySQL web application (that we’ll be connecting with our android application).

XAMPP(或WAMP)是一键式安装程序软件,可为开发PHP,MySQL Web应用程序(我们将与android应用程序连接)创建环境。

Download and install XAMPP from here.

此处下载并安装XAMPP。

Launch the XAMPP app after installation and you will be greeted with below screen.

安装后启动XAMPP应用程序,将会看到下面的屏幕。

You can test your server by opening https://localhost. The following screen should appear.

您可以通过打开https://localhost来测试服务器。 应出现以下屏幕。

Also, you can check phpMyAdmin by opening https://localhost/phpmyadmin. Let’s see what it shows!

另外,您可以通过打开https://localhost/phpmyadmin来检查phpMyAdmin。 让我们看看它显示了什么!

OOPS! You might end up with a screen like this. Seems like the MySQL server isn’t properly running. Go To the Manage Servers tab in the XAMPP application and click restart all. The servers should be running properly as seen in the image below.

糟糕! 您可能最终会看到这样的屏幕。 似乎MySQL服务器无法正常运行。 转到XAMPP应用程序中的“管理服务器”选项卡,然后单击“全部重新启动”。 如下图所示,服务器应正常运行。

Now test phpMyAdmin in the localhost and you’ll end up with a screen similar to this.

android phpmyadmin

现在在本地主机中测试phpMyAdmin,您将得到一个类似于此的屏幕。

Now let’s test a sample php script. Create a new test.php file and add the following lines into it.

现在,让我们测试一个示例php脚本。 创建一个新的test.php文件,并在其中添加以下几行。

<?php
echo "Hello, World";
?>

In the above code:

在上面的代码中:

  • ?php starts opening tag for any PHP script.

    php开始为任何PHP脚本打开标签。
  • ?> means closing tag like closing bracket in Java.

    ?>表示结束标记,例如Java中的结束括号。

Note: Knowing PHP is not mandatory for this tutorial.

注意:本教程不是必须了解PHP。

if you’re using a MAC then goto Applications->Xampp->htdocs. Create a new folder here lets say test_android and copy paste the test.php that was created before. Now open the url https://localhost/test_android/test.php

如果您使用的是MAC,请转到“应用程序”->“ Xampp”->“ htdocs”。 在这里创建一个新文件夹,假设为test_android并复制粘贴之前创建的test.php。 现在打开URL https://localhost/test_android/test.php

You’ll end up with a screen like this:

android php hello world

您最终将得到一个这样的屏幕:

设置MySQL数据库 (Setting Up MySQL Database)

Open the phpMyAdmin by visiting https://localhost/phpmyadmin.
Now select the Databases Tab that’s present in the top left of the headers row. Give a random name and create it. The newly created empty database would be visible in the left sidebar.

通过访问https://localhost/phpmyadmin打开phpMyAdmin。
现在,选择标题行左上方的“数据库”选项卡。 给出一个随机名称并创建它。 新创建的空数据库将在左侧边栏中可见。

Let’s create a users table in the newly created Database. Run the following query in the console

让我们在新创建的数据库中创建一个用户表。 在控制台中运行以下查询

CREATE TABLE  `firstDB`.`users` (
    `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `username` VARCHAR( 20 ) NOT NULL ,
    `password` VARCHAR( 20 ) NOT NULL
)

If the table is successfully created, you’ll end up with a screen similar to this:

如果成功创建了表,您将看到类似以下的屏幕:

将PHP连接到MySQL数据库 (Connecting PHP to MySQL Database)

To connect a PHP script to MySQL database three input values are required.
Following are the inputs and there default values for a XAMPP server

要将PHP脚本连接到MySQL数据库,需要三个输入值。
以下是输入以及XAMPP服务器的默认值

  • Host name: localhost

    主机名:localhost
  • MySQL user name : root

    MySQL用户名:root
  • MySQL password : It is blank. “”

    MySQL密码:为空。 “”

Let’s create a test-connect.php script and add it in the htdocs->test-android folder.

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值