简体   繁体   English

Slim PHP REST API无法在生产服务器中获取数据,可以在本地主机上工作

[英]Slim PHP REST API not fetching data in production server, works in localhost

I have a simple Slim PHP REST api which works great on my local server, but when I pushed it to a shared hosting server it does not fetch the data. 我有一个简单的Slim PHP REST api,它在本地服务器上很好用,但是当我将其推送到共享托管服务器时,它不会获取数据。 My limited plan does not give me SSH access to the database so it is difficult to run tests, but this is what I have tracked so far... 我有限的计划没有给我SSH访问数据库的权限,因此很难运行测试,但这是我到目前为止跟踪的内容...

Here is a test api endpoint: 这是一个测试API端点:

$app->get('/test', function() use($app) {
    $app->response->setStatus(200);
    if($db = getDB()){
      echo "connected<br>";
    } else{
      echo "not connected<br>";
    }
    $sql = "SELECT * FROM artists";
    $stmt = $db->prepare($sql);
    if($stmt){
      echo "statement true<br>";
    } else{
    echo "statement not true<br>";
    }
    $stmt->execute();
    $stmt->setFetchMode(PDO::FETCH_OBJ);
    if($stmt->fetch()){
      echo "executed";
    }else{
      echo "not executed";
    }
});

This gives me an output of: 'connected' 'statement true' 'not executed'. 这给了我一个输出:'connected''statement true''not execute'。

I am able to run queries from the phpMyAdmin dashboard successfully. 我能够从phpMyAdmin仪表板成功运行查询。 In the webpage, the rest api is working fine and returning a 'No match found' message implying that the functions are working, but the data set is empty. 在网页中,其余api正常运行,并返回“未找到匹配项”消息,表明函数正在运行,但数据集为空。 I do not know which direction to look to find why no records are being returned? 我不知道寻找哪个方向来查找为什么未返回任何记录?

In the complete data functions: 在完整的数据功能中:

    $error = array('error' => 'No match found');

    // fetch data into array
    $i = 0;
    while($row = $stmt->fetch()) {
        $data[$i] = $row;
        $i++;
    }

    // return data
    if($data) {
        $app->response->setStatus(200);
        $app->response()->headers->set('Content-Type', 'application/json');
        echo json_encode($data, JSON_PRETTY_PRINT);
    } else {
        $app->response->setStatus(200);
        $app->response()->headers->set('Content-Type', 'application/json');
        echo json_encode($error);
    }

I am new to PHP so any advice on how to trouble shoot, log, or otherwise trace the issue would be appreciated. 我是PHP的新手,所以对于如何排除问题,记录日志或以其他方式跟踪问题的任何建议将不胜感激。

Ahhhh... I found it! 啊...我找到了! The problem was not with the php, but with the sql. 问题不在于PHP,而在于SQL。 Apparently, sql statements are case insensitive on my local machine (mac), but case sensitive on Linux servers. 显然,sql语句在本地计算机(mac)上不区分大小写,但在Linux服务器上则区分大小写。 In the DB the table name is Artists where in the code above I had artists . 在DB中,表名是Artists ,在上面的代码中我有artists

So the correction is: 因此,更正为:

$sql = "SELECT * FROM artists";

Becomes: 成为:

$sql = "SELECT * FROM Artists";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM