我正在运行 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">©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), {"
网络选项卡显示:
load_attraction.php 的 Network -> Respons-tab 显示:
<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 不能正常工作。我使用自制软件安装了 php@7.1 brew install 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,但是遇到了Module API(=20160303)和PHP API(=20170718)不匹配的问题,因此无法加载扩展pso_pgsql。当我安装 php7.1(它使用与模块相同的 API)时,至少这个问题已经消失了。
但是,当我添加extension=pdo_pgsql.so
php.ini 文件并运行时,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”,它只包含信息opcache.so 文件所在的位置。但是还有其他可以加载 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
我想我可能已经通过运行此处找到的脚本获得了 pdo_pgsql.so:https://gist.github.com/doole/8651341 ,它似乎可以进行一些修改。它最终以返回结束:
extension=/usr/local/lib/php/extensions/pdo_pgsql.so
我手动将文件移动到相同的 ext_dir 文件夹中pear config-show
(但也尝试将 ext_dir 更改为/usr/local/lib/php/extensions/
没有运气。)
但是,由于 pdo_pgsql 在运行时出现,$ php -m
我假设它已经(?)包含在 php 包中?但是我到底如何才能让它加载到 php.ini 文件中,而不会出现它已经被加载的错误呢?另一方面,它在 phpinfo-file 中表示已启用 PostgreSQL 的 PDO 驱动程序。它是否默认加载,而不告诉它在 php.ini 中这样做?这就是它“已经加载”的原因吗?
苏……我看错方向了吗?我的原始错误 PDO::FETCH_ASSOC 是由其他原因引起的吗?如果是这样:它到底是什么?
如果有人可以提供一些建议,我会很高兴。我没主意了。