In the previous post, I have written an article about Parse signUp and signIn. However, Parse will stop service in Jan 27th, 2017. Instead of using parse, I will introduce how to use mysql to create signUp and signIn, to some extent, how to connect the swift app to mySQL.
There are some basic knowledge for the connection, make sure you have worked with php before 🙂 The basic four steps are below:
You have a server which has already installed apache, php and mysql(LAMP is also allowed)
We write some php code to execute the mysql query
Using our app to request that url and to run step 2
Receive the data back from that URL
You have a server which has already installed apache, php and mysql
I usually use amazon AWS as my server, to install apache, php, mysql, you can find some tutorial on the internet. If you have any question feel free to comment below, I will write one as well. Official aws document Try this link, if you are not sure now.
We write some php code to execute the mysql query
The basic idea is to execute these mysql query. For here, I will use two basic files, one called Conn.php, it will include some mysql connection settings:
1
2
3
4
5
6
7
8
9
10
<?php
class Conn {
public static $dbhost = "localhost";
public static $dbuser = "root";
public static $dbpass = "yourpassword";
public static $dbname = "mydb";
}
?>
Filling that string with your own settings. Then let’s have a quick look for another file, MySQLDao.php: I may include some simple function for signUp and signin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
class MySQLDao {
var $dbhost = null;
var $dbuser = null;
var $dbpass = null;
var $conn = null;
var $dbname = null;
var $result = null;
function __construct() {
$this->dbhost = Conn::$dbhost;
$this->dbuser = Conn::$dbuser;
$this->dbpass = Conn::$dbpass;
$this->dbname = Conn::$dbname;
}
public function openConnection() {
$this->conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
if (mysqli_connect_errno())
echo new Exception("Could not establish connection with database");
}
public function getConnection() {
return $this->conn;
}
public function closeConnection() {
if ($this->conn != null)
$this->conn->close();
}
public function getUserDetails($email)
{
$returnValue = array();
$sql = "select * from users where email='" . $email . "'";
$result = $this->conn->query($sql);
if ($result != null && (mysqli_num_rows($result) >= 1)) {
$row = $result->fetch_array(MYSQLI_ASSOC);
if (!empty($row)) {
$returnValue = $row;
}
}
return $returnValue;
}
public function getUserDetailsWithPassword($email, $userPassword)
{
$returnValue = array();
$sql = "select id,email,password from users where email='" . $email . "' and password='" .$userPassword . "'";
$result = $this->conn->query($sql);
if ($result != null && (mysqli_num_rows($result) >= 1)) {
$row = $result->fetch_array(MYSQLI_ASSOC);
if (!empty($row)) {
$returnValue = $row;
}
}
return $returnValue;
}
public function registerUser($email, $password)
{
$sql = "insert into users set email=?, password=?";