【发布时间】:2019-05-11 18:40:35
【问题描述】:
我为客户构建了一个 Angular 6 应用程序。客户端当前在 Dreamhost 上托管一个 mysql 数据库。有一个现有数据库,我需要确定如何将我的应用程序连接到现有数据库以查询数据。我知道直接查询数据库会打开客户端进行 sql 注入,所以我需要实现一个服务。但是,我主要是前端开发人员,并且对连接数据库所需的后端服务感到迷茫。
该应用程序是用 Angular 6 编写的,将部署到 Andriod 和 iOS。请建议最好的服务或教程来实现可以由应用程序使用并提供 json 响应的 Web 服务。该应用程序当前从嵌入式 json 文件中获取数据,该文件由数据库中的查询生成并与应用程序一起部署。我正在尝试在数据库和应用程序之间建立“实时”更新
我创建了一个 php rest 服务,我正在尝试将它安装在 Dreamhost mysql vps 上:
让服务器管理员在 VPS 上创建一个文件夹 api/。里面将有三个文件夹,每个文件夹中有一个文件: api/配置 api/对象 api/访客
/api/config/database.php
<?php
class Database{
// specify your own database credentials
private $host = "localhost";
private $db_name = "**************";
private $username = "**************";
private $password = "**************";
public $conn;
// get the database connection
public function getConnection(){
$this->conn = null;
try{
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname="
. $this->db_name, $this->username, $this->password);
$this->conn->exec("set names utf8");
}catch(PDOException $exception){
echo "Connection error: " . $exception->getMessage();
}
return $this->conn;
}
}
?>
/api/objects/guest.php
<?php
class Guest{
// database connection and table name
private $conn;
private $table_name = "guests";
// object properties
public $id;
public $name;
public $bio;
public $image;
public $type;
// constructor with $db as database connection
public function __construct($db){
$this->conn = $db;
}
}
?>
/api/guest/read.php
<?php
// required header
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
// include database and object files
include_once '../config/database.php';
include_once '../objects/category.php';
// instantiate database and category object
$database = new Database();
$db = $database->getConnection();
// initialize object
$guest = new Guest($db);
// query categorys
$stmt = $guest->read();
$num = $stmt->rowCount();
// check if more than 0 record found
if($num>0){
// guest array
$guest_arr=array();
$guest_arr["records"]=array();
// retrieve our table contents
// fetch() is faster than fetchAll()
// http://stackoverflow.com/questions/2770630/pdofetchall-vs-pdofetch-in-a-loop
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
// extract row
// this will make $row['name'] to
// just $name only
extract($row);
$guest_item=array(
"id" => $id,
"name" => $name,
"bio" => html_entity_decode($bio),
"image" => $image,
"type" => $type
);
array_push($guest_arr["records"], $guest_item);
}
// set response code - 200 OK
http_response_code(200);
// show guests data in json format
echo json_encode($guest_arr);
} else {
// set response code - 404 Not found
http_response_code(404);
// tell the user no guest was found
echo json_encode(
array("message" => "No guests found.")
);
}
// no guests found will be here
// read guests
function read(){
// select all query
$query = "SELECT
c.name as name, p.id, p.name, p.bio, p.image, p.type
FROM
" . $this->table_name . " p
LEFT JOIN
categories c
ON p.id = c.id
ORDER BY
p.name DESC";
// prepare query statement
$stmt = $this->conn->prepare($query);
// execute query
$stmt->execute();
return $stmt;
}
?>
出于隐私考虑,我从代码中删除了实际的用户名/密码/dbname,但用户 已为应用/服务创建具有选择和索引访问权限的帐户
当前服务器是一个 VPS mysql.ServerName.org,所以我假设一旦我将这个脚本安装在一个名为 api/ 的文件夹中,我就可以在我的浏览器中输入以下地址并点击这个“服务”:
我将调整 sql,因为我不确定它是否正确,但这是将基本脚本转换为 PHP Restful 服务的第一步
对于第一遍,我真的只需要查询来宾表以获取包含的信息。
“https://mysql.ServerName.org/api/guest/read.php”
好的,安装了服务,这是我当前的服务器响应:请注意:
Uncaught Error: Using $this when not in object context in /home/libertycon/libertycon.org/api/guest/read.php:74 Line 74: $stmt = $this->conn->prepare($query);
我的PHP比较糟糕,但是我在read.php文件中看不到任何问题所以我不知道为什么它找不到明显存在的方法......
【问题讨论】:
-
你需要学习如何用php制作一个REST api。这是你的开始:codeofaninja.com/2017/02/create-simple-rest-api-in-php.html
-
@MaxSvid 我正在学习您提供的其余教程,并将相应地更新代码,谢谢。
-
如果我正确地遵循了该教程,那么我应该能够做到这一点并获得我相信的客人的 json 数组......
-
另外,@MaxSvid 如果您将其作为完整答案而不是评论,我会将其作为已接受的答案进行投票
标签: php mysql rest service angular6