首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将分析与外部数据库连接?

将分析与外部数据库连接?
EN

Stack Overflow用户
提问于 2015-07-23 09:13:12
回答 1查看 2.5K关注 0票数 1

我正在使用PHP、mysql等传统技术构建它的项目,它是一个web应用程序。现在我们想在Andoid,iOS等平台上为移动用户开发一个应用程序。因此,我们正在考虑将MySql与Parse.com数据库连接起来。我知道解析使用NoSql类型的数据库来存储对象。

所以我的问题是,我们能把解析数据库连接到任何其他SQL数据库吗?如果是,那我们怎么能做到呢?

编辑

@Luca我刚刚创建了一个像你一样的新云功能。在下面。

代码语言:javascript
复制
Parse.Cloud.define("get_parse4j_object", 

function(request,response){

    // Parameters from client (iOS/Android app)
    //var requestedObjectId = request.params.objectId;

    // Calling beckend service for getting user information
    Parse.Cloud.httpRequest({
        method: "GET",
        headers: {
            'Content-Type': 'application/json'
        },        
        url: "https://api.parse.com/1/parse4j/MLiOgxncUM", /* This could be your url for the proper php module */
        //body: { "objectId":requestedObjectId }, /* Here you compose the body request for the http call, passing the php parameters and their values */
        success: function(httpResponse) {

        /* We expect that the php response is a Json string, using the header('application/json'), so: */
        var jsonResponse = JSON.parse(httpResponse.text);

        /* sample structure in jsonResponse: { "name":"Joe", "surname":"Banana", "birth_date":"01-02-1999" } */

        /* Do any additional stuff you need... */

        /* return the result to your iOS/Android client */
        return response.success( { "myRequestedUserInfo" : jsonResponse } );
                alert(jsonResponse);
        },
        error: function(httpResponse) {            
                return response.error({ "msg":"Unable to fetch this user", "code":123456 }); // sample error response            
        }
    });

});

我遵循卢卡·拉科解释我的方式。但是,当我从客户端JS调用函数时,会出现错误。这是我的客户JS

代码语言:javascript
复制
<script type="text/javascript">
    Parse.initialize("APP_ID", "JAVASCRIPT_KEY");


    Parse.Cloud.run('get_parse4j_object', {}, {
            success: function(result) {
            alert(result);
        },
            error: function(error) {
            alert(JSON.stringify(error));
        }
    });
  </script>

在“网络”选项卡中我可以看到

代码语言:javascript
复制
POST https://api.parse.com/1/functions/get_parse4j_object 400 (Bad Request)

而错误是:{“代码”:141,“消息”:“函数找不到”}我在哪里丢失并做错了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-23 14:06:26

如果您的意思是像一个普通的mysql连接器,那么响应是否定的,您不能。目前,进行解析和其他相关操作的唯一方法是查询“从和解析”。要明确:

  • 如果您想从存储在mysql中的Parse中获得一个值,您必须使用一个http请求,该请求存储在您的php网站(由您实现)上的一个特定的php模块上,期望得到一些paramenter,并以一种特定的方式返回结果,通常以json格式返回,还可以使用http header应用程序/json。
  • 如果您想从php获得存储在解析db上的值,您可以在解析网站( https://parse.com/docs/rest/guide/ )上的规范之后从php运行REST调用,或者只使用php ( https://github.com/ParsePlatform/parse-php-sdk )。也请看一看Webhooks的分析。

据我所知,您已经有了一个正常工作的web服务,因此您只需通过Parse将存储在mysql上的服务器上的资源代理到客户端。换句话说,您应该使用Parse (针对iOS或Android)为客户端上要检索的每种类型的信息创建一个Parse函数,并为您在设备上执行的每个操作创建另一个Parse函数,并且您希望保存在mysql上,始终通过分析系统。

我个人的观点是继续使用Mysql,特别是因为在Parse上,我们仍然对查询有很大的限制(没有组,没有不同,查询超时等等),而对于推送通知来说,这似乎是一个很好的服务。无论如何,所有这些都取决于软件的复杂性,正如我所说的,这只是我的观点。

编辑

这里有一个例子:

在分析云代码中,让我们创建一个名为'get_user_info'的云函数

代码语言:javascript
复制
Parse.Cloud.define("get_user_info", 

function(request,response){

    // Parameters from client (iOS/Android app)
    var requestedUserId = request.params.user_id;

    // Calling beckend service for getting user information
    Parse.Cloud.httpRequest({
        method: "POST",        
        url: "https://www.yourPhpWebsite.com/getUser.php", /* This could be your url for the proper php module */
        body: { "php_param_user_id":requestedUserId }, /* Here you compose the body request for the http call, passing the php parameters and their values */
        success: function(httpResponse) {

        /* We expect that the php response is a Json string, using the header('application/json'), so: */
        var jsonResponse = JSON.parse(httpResponse.text);

        /* sample structure in jsonResponse: { "name":"Joe", "surname":"Banana", "birth_date":"01-02-1999" } */

        /* Do any additional stuff you need... */

        /* return the result to your iOS/Android client */
        return response.success( { "myRequestedUserInfo" : jsonResponse } );

        },
        error: function(httpResponse) {            
                return response.error({ "msg":"Unable to fetch this user", "code":123456 }); // sample error response            
        }
    });

});

示例'getUser.php‘模块可以是

代码语言:javascript
复制
<?php

$expectedUserId = $_POST['php_param_user_id'];

// query your MySql db using passed user id
$query = "SELECT name,surname,birth_date FROM MyUserTable Where id = ".$expectedUserId;

// perform your query (the above one is just an example, would be better to use PDO and any other check, just to avoid SQL Injection)
// ...
// ..
// .

$resultQuery = row[0];

// sample json structure
$jsonResponseToParse = '{ "name":'.resultQuery["name"].', "surname":'.resultQuery["surname"].', "birth_date":'.resultQuery["birth_date"].' }';

header('application/json');

echo jsonResponseToParse;

exit();

?>

希望它能帮上忙

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31583013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档