如何在MacOs Sierra上将pdo_pgsql扩展添加到php7.1

时间:2019-07-05 15:00:42

标签: php pdo php-pgsql

我正在运行macOS Sierra(10.12.6)和php7.1,很长一段时间以来,我一直在试图弄清楚如何读取包含PDO :: FETCH_ASSOC的php文件。我收到一个错误,我(认为)与没有使用扩展名pdo_pgsql正确配置php有关。 php文件如下所示:

<?php 
error_reporting(E_ALL);
ini_set('display_errors', '1');
$db = new PDO('pgsql:host=localhost;port=5432;dbname=some_db;', 'username', 'password');
$sql = $db->query("SELECT id, name, image, web, category, ST_AsGeoJSON(geom, 5) as geom FROM some_file ORDER BY name");
$features=[];
while ($row=$sql->fetch(PDO::FETCH_ASSOC)){
    $feature=['type'=>'Feature'];
    $feature['geometry']=json_decode($row['geom']);
    unset($row['geom']);
    $feature['properties']=$row;
    array_push($features, $feature);
}
$featureCollection=['type'=>'FeatureCollection', 'features'=>$features];
echo json_encode($featureCollection);
?>

和html文件中引用它的部分是:

        <!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>WebMap 101</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.1/dist/leaflet.css">
    <script src="https://unpkg.com/leaflet@1.0.1/dist/leaflet.js"></script>
    <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
    <script src="resources/leaflet.ajax.js"></script>
    <script src="resources/turf.min.js"></script>
    <style>
        #header {
            height: 75px;
            background-color: darkgoldenrod;
        }

        #mapdiv {
            height: 650px;
            background-color: salmon;
        }

        #side_panel {
            height: 650px;
            background-color: beige;
        }

        #footer {
            height: 75px;
            background-color: darkgray
        }

        .attraction {
            margin-bottom: 5px;
        }

    </style>
</head>



<body>
    <div id="header" class="col-md-12">
        <h1 class="text-center">Mexico City</h1>
    </div>
    <div id="side_panel" class="col-md-3">
        <h1 class="text-center">Attractions</h1>
        <button id="zoomToZocalo" class="form-control btn-primary">Zocalo</button>
    </div>
    <div id="mapdiv" class="col-md-9"></div>
    <div id="footer" class="col-md-12">
        <h4 id="map_coords" class="text-center">Latitude: 19.4 Longitude: -99.1 Zoom Level: 11</h4>
        <h4 class="text-center">&copy;2016 <a href="http:page_name.com">Page name</a></h4>
    </div>
    <script>
        var mymap = L.map('mapdiv')
        mymap.setView([19.4, -99.1], 11);

        var backgroundLayer = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png');
        mymap.addLayer(backgroundLayer);




        var zocaloMarker = L.marker([19.43278, -99.13333]).addTo(mymap).bindPopup("<h3 class='text-center'>Zocalo</h3><a href='https://en.wikipedia.org/wiki/Z%C3%B3calo' target='blank'><img src='img/zocalo.jpg' width='200px'></a>");

        $("#zoomToZocalo").click(function() {
            mymap.setView([19.43278, -99.13333], 17);
        });


        var lyrAttractions;
        $.ajax({
            url: 'load_attractions.php',
            success: function(response) {
                if (lyrAttractions) {
                    mymap.removeLayer(lyrAttractions)
                };
                lyrAttractions = L.geoJSON(JSON.parse(response), {
                    pointToLayer: function(feature, latlng) {
                        var str = "<button id='zoomTo" + feature.properties.name.replace(/ /g, '');
                        str += "' class='form-control btn btn-primary attraction'>";
                        str += feature.properties.name + "</button>";
                        $("#side_panel").append(str);
                        $("#zoomTo" + feature.properties.name.replace(/ /g, '')).click(function() {
                            mymap.setView([latlng.lat, latlng.lng], 17);
                        });


                        var str = "<h4 class='text-center'>" + feature.properties.name + "</h4><hr>";
                        str += "<a href='" + feature.properties.web + "' target='blank'>";
                        str += "<img src='img/" + feature.properties.image + "' width='200px'>";
                        str += "</a>";
                        return L.marker(latlng).bindPopup(str);

                }});
                lyrAttractions.addTo(mymap);
                mymap.fitBounds(lyrAttractions.getBounds());
            }});

        mymap.on('mousemove', function(e) {
            var str = "Latitude: " + e.latlng.lat.toFixed(5) + " Longitude: " + e.latlng.lng.toFixed(5) + " Zoom Level: " + mymap.getZoom();
            $("#map_coords").html(str);
        });
    </script>
</body>
</html>

在Google开发人员工具中,我收到此错误:

VM102:1 Uncaught SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at Object.success ((index):80)
at i (jquery-2.2.4.min.js:2)
at Object.fireWith [as resolveWith] (jquery-2.2.4.min.js:2)
at z (jquery-2.2.4.min.js:4)
at XMLHttpRequest.<anonymous> (jquery-2.2.4.min.js:4)

第80行对应于以上"lyrAttractions = L.geoJSON(JSON.parse(response), {"

网络标签显示:

Image: Network tab in Google Developer

与网络-> load_attraction.php的响应选项卡显示:

<br />
<font size='1'><table class='xdebug-error xe-uncaught-exception' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Fatal error: Uncaught Error: Call to a member function fetch() on boolean in /Users/agh/Sites/webmaps101/load_attractions.php on line <i>7</i></th></tr>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Error: Call to a member function fetch() on boolean in /Users/agh/Sites/webmaps101/load_attractions.php on line <i>7</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0245</td><td bgcolor='#eeeeec' align='right'>362600</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='/Users/agh/Sites/webmaps101/load_attractions.php' bgcolor='#eeeeec'>.../load_attractions.php<b>:</b>0</td></tr>
</table></font>

现在,超过60小时后,我仍然没有找到解决方法,开始感到沮丧。我的假设是php扩展名pdo_pgsql无法正常工作。我使用brew install php@7.1

用自制软件安装了php@7.1

以下内容可能会有所帮助: $ php -v返回:

Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
    with Xdebug v2.7.2, Copyright (c) 2002-2019, by Derick Rethans
    with Zend OPcache v7.1.30, Copyright (c) 1999-2018, by Zend Technologies

$ which php返回:

/usr/local/opt/php@7.1/bin/php

$ php -m | grep pdo_pgsql返回: pdo_pgsql

$ php --ini返回:

Configuration File (php.ini) Path: /usr/local/etc/php/7.1
Loaded Configuration File:         /usr/local/etc/php/7.1/php.ini
Scan for additional .ini files in: /usr/local/etc/php/7.1/conf.d
Additional .ini files parsed:      /usr/local/etc/php/7.1/conf.d/ext-opcache.ini

$ brew doctor返回:

Warning: "config" scripts exist outside your system or Homebrew directories.
`./configure` scripts often look for *-config scripts to determine if
software packages are installed, and which additional flags to use when
compiling and linking.

Having additional scripts in your path can confuse software installed via
Homebrew if the config script overrides a system or Homebrew-provided
script of the same name. We found the following "config" scripts:
  /usr/local/php5/bin/curl-config
  /usr/local/php5/bin/freetype-config
  /usr/local/php5/bin/icu-config
  /usr/local/php5/bin/iodbc-config
  /usr/local/php5/bin/libmcrypt-config
  /usr/local/php5/bin/libpng-config
  /usr/local/php5/bin/libpng12-config
  /usr/local/php5/bin/php-config
  /usr/local/php5/bin/xslt-config
  /Library/Frameworks/Python.framework/Versions/3.6/bin/python3-config
  /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6-config
  /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6m-config
  /Library/Frameworks/Python.framework/Versions/3.7/bin/python3-config
  /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7-config
  /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7m-config
  /Applications/Postgres.app/Contents/Versions/latest/bin/gdal-config

Warning: You have unlinked kegs in your Cellar.
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
  git
  python

运行phpinfo文件可以给我以下信息:

Server API  Apache 2.0 Handler
Virtual Directory Support   disabled
Configuration File (php.ini) Path   /usr/local/etc/php/7.1
Loaded Configuration File   /usr/local/etc/php/7.1/php.ini
Scan this dir for additional .ini files /usr/local/etc/php/7.1/conf.d
Additional .ini files parsed    /usr/local/etc/php/7.1/conf.d/ext-opcache.ini
PHP API 20160303
PHP Extension   20160303
Zend Extension  320160303
Zend Extension Build    API320160303,NTS
PHP Extension Build API20160303,NTS

并在同一文件中进一步查找:

pdo_pgsql
PDO Driver for PostgreSQL   enabled
PostgreSQL(libpq) Version   11.3
Module version  7.1.30
Revision    $Id: 9c5f356c77143981d2e905e276e439501fe0f419 $

运行pear config-show返回:

Auto-discover new Channels     auto_discover    <not set>
Default Channel                default_channel  pear.php.net
HTTP Proxy Server Address      http_proxy       <not set>
PEAR server [DEPRECATED]       master_server    pear.php.net
Default Channel Mirror         preferred_mirror pear.php.net
Remote Configuration File      remote_config    <not set>
PEAR executables directory     bin_dir          /usr/local/opt/php@7.1/bin
PEAR documentation directory   doc_dir          /usr/local/share/pear@7.1/doc
PHP extension directory        ext_dir          /usr/local/lib/php/pecl/20160303/
PEAR directory                 php_dir          /usr/local/share/pear@7.1
PEAR Installer cache directory cache_dir        /private/tmp/pear/cache
PEAR configuration file        cfg_dir          /usr/local/share/pear@7.1/cfg
directory
PEAR data directory            data_dir         /usr/local/share/pear@7.1/data
PEAR Installer download        download_dir     /private/tmp/pear/download
directory
Systems manpage files          man_dir          /usr/local/share/man
directory
PEAR metadata directory        metadata_dir     <not set>
PHP CLI/CGI binary             php_bin          /usr/local/opt/php@7.1/bin/php
php.ini location               php_ini          /usr/local/etc/php/7.1/php.ini
--program-prefix passed to     php_prefix       <not set>
PHP's ./configure
--program-suffix passed to     php_suffix       <not set>
PHP's ./configure
PEAR Installer temp directory  temp_dir         /private/tmp/pear/temp
PEAR test directory            test_dir         /usr/local/share/pear@7.1/test
PEAR www files directory       www_dir          /usr/local/share/pear@7.1/htdocs
Cache TimeToLive               cache_ttl        3600
Preferred Package State        preferred_state  stable
Unix file mask                 umask            22
Debug Log Level                verbose          1
PEAR password (for             password         <not set>
maintainers)
Signature Handling Program     sig_bin          /usr/local/bin/gpg
Signature Key Directory        sig_keydir       /usr/local/etc/php/7.1/pearkeys
Signature Key Id               sig_keyid        <not set>
Package Signature Type         sig_type         gpg
PEAR username (for             username         <not set>
maintainers)
User Configuration File        Filename         /Users/agh/.pearrc
System Configuration File      Filename         /usr/local/etc/php/7.1/pear.conf

我也尝试了php7.3和php7.2,但是遇到模块API(= 20160303)和PHP API(= 20170718)不匹配的问题,因此无法加载扩展名pso_pgsql。当我安装php7.1(使用与模块相同的API)时,至少这个问题消失了。

但是,当我在php.ini文件中添加extension=pdo_pgsql.so并运行php --ini时,我收到一条错误消息,说PHP Warning: Module 'pdo_pgsql' already loaded in Unknown on line 0

我已经读到可能还有另一个文件加载了pdo_pgsql.so,并且有人建议在conf.d文件夹中查找。我以前在conf.d中有几个文件(想想是在以前的php安装(7.2和7.3)期间,例如“ ext-pdo_pgsql.ini”。现在我只有“ exe-opcache.ini”,其中仅包含以下信息:

在哪里可以加载pdo_pgsql.so?

我注意到文件pdo_pgsql.so的图标与最近与pecl安装的扩展程序相关的图标不同。 pdo_pgsql的黑色终端带有文本“ exec”,其他扩展名只是白皮书。我不记得最初是什么时候得到pdo_pgsql.so文件的(因为我已经尝试了很多事情),因为尝试pecl install pdo_pgsql时,我最终遇到一个错误: >

Installing shared extensions:     /private/tmp/pear/temp/pear-build-aghlTd4GE/install-PDO-1.0.3/usr/local/Cellar/php@7.1/7.1.30_1/pecl/20160303/
cp: modules/*: No such file or directory
make: *** [install-modules] Error 1
ERROR: `make INSTALL_ROOT="/private/tmp/pear/temp/pear-build-aghlTd4GE/install-PDO-1.0.3" install' failed

我想我可能通过运行在https://gist.github.com/doole/8651341处找到的脚本来得到pdo_pgsql.so,它似乎可以进行一些修改。最后,它返回了:

extension=/usr/local/lib/php/extensions/pdo_pgsql.so

我手动将文件移动到pear config-show给定的ext_dir文件夹中(但也尝试将ext_dir更改为/usr/local/lib/php/extensions/,但是没有运气。)

但是,由于运行$ php -m时会显示pdo_pgsql,所以我认为它已经包含在php软件包中了吗?但是到底如何才能使它能够加载到php.ini文件中,而不会出现已被加载的错误呢?另一方面,它在phpinfo文件中说启用了PostgreSQL的PDO驱动程序。默认情况下是否加载,而没有在php.ini中指示加载?这就是为什么它“已加载”?

S ...我看错了方向吗?我的原始错误PDO :: FETCH_ASSOC是否是由其他原因引起的?如果是这样:那到底是什么?

如果有人可以提供一些建议,我将非常高兴。我没主意了。

0 个答案:

没有答案