我正在尝试使用Flutter和MySQL构建一个聊天移动应用程序。但是到目前为止,大多数资源和教程都在使用firebase。我使用MySQL的原因是因为我有一个连接到MySQL的Web应用程序,并且当前在移动应用程序部分上工作。
是否可以使用MySQL而不是Firebase构建聊天移动应用?
答案 0 :(得分:2)
<?php $dns = ‘mysql:host=localhost;dbname=enter-database-name’; $user = ‘enter-username’; $password = ‘enter_the_password_of_your_database’; try{ $db = new PDO ($dns, $user, $pass); }catch( PDOException $e){ $error = $e->getMessage(); echo $error; }
<?php require_once('db.php'); $query = 'SELECT * FROM posts'; $stm = $db->prepare($query); $stm->execute(); $row = $stm->fetch(PDO::FETCH_ASSOC); echo json_encode($row);
Future getData() async{ var url = 'https://disgusted-vapors.000webhostapp.com/get.php'; http.Response response = await http.get(url); var data = jsonDecode(response.body); print(data.toString()); }
并调用:
@override void initState() { getData(); }