php - 迁移到 Postgresql
问题描述
我已经尝试了好几天,但我无法解决我的问题。我声明我开始在 Mysql 上创建自己的网页,并且效果很好。我不得不迁移到 Postgres,问题从这里开始。
例如那里:
我有这个错误:
致命错误:未捕获错误:调用 C:\xampp\input.php:39 中字符串上的成员函数 query() 堆栈跟踪:#0 {main}
$checkdata = "SELECT count(*) as prenotato
FROM Prenotazione
WHERE data='$data'
AND NOT ('$newTimeEnd' < orario_inizio OR orario_fine < '$orario_inizio')";
$prenotato = $conn_string->query($checkdata)->pg_fetch_row()[0];
这是我使用的 config.php 文件:
<?php
$conn_string = "host=localhost port=5432 dbname=postgres user=postgres password=123456789";
$dbconn = pg_connect($conn_string);
?>
编辑。
我听从你的建议:
配置文件
<?php
$dbname = "postgres";
$host = "localhost";
$username = "postgres";
$dbh = new PDO("pgsql:dbname=$dbname;host=$host", $username, 123456789 );
?>
例如,我有另一个关于类似错误的问题:
警告:pg_query() 期望参数 1 是资源,第 5 行 C:\xampp\htdocs\PhpProject1\select.php 中给出的对象
警告:pg_num_rows() 期望参数 1 是资源,在第 18 行的 C:\xampp\htdocs\PhpProject1\select.php 中给出 null
<?php
require ('config.php');
$output = '';
$sql = "SELECT * FROM Prenotazione where data = CURRENT_DATE()";
$result = pg_query($dbh, $sql);
$output .= '
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th width="10%">Nominativo</th>
<th width="20%">Data</th>
<th width="5%">Orario Inizio</th>
<th width="5%">Orario Fine</th>
<th width="5%">Email</th>
<th width="50%">Oggetto</th>
</tr>';
$rows = pg_num_rows($result);
if($rows > 0)
{
我怎么解决这个问题??谢谢
解决方案
$conn_string
是一个字符串,而不是一个对象,所以你不能在它上面调用任何方法!甚至变量名也会告诉你!
您确定您不是要使用该字符串创建新的 PDO 连接吗?
你的字符串应该像这个例子:
$postgresDsn = 'pgsql:host=localhost;port=5432;dbname=testdb;user=someone;password=mypass'
$db = new PDO($postgresDsn, $user, $password);
在此处查看 PDO 的文档:
http://php.net/manual/en/pdo.construct.php
此外,postgres 的具体说明:
https://secure.php.net/manual/en/ref.pdo-pgsql.connection.php
更新
我刚刚注意到,您没有使用$dbconn
. 尝试改变:
$prenotato = $conn_string->query($checkdata)->pg_fetch_row()[0];
到
$prenotato = $dbconn->query($checkdata)->pg_fetch_row()[0];
无论如何,请检查 PDO,它更安全,更易于使用。
推荐阅读
- es6-promise - 实现一个承诺以返回两个承诺中较慢的一个
- asp.net-core - Blazor 不构建 dist 包
- c++ - 基于函数而不是集合的二进制搜索或迭代器?
- mongodb - 如何构建聚合管道以根据其他文档中的字段值过滤文档?
- python - Python 传统知识。使用 Tab 键时设置小部件焦点
- php - Laravel:$stg 未定义
- apache-spark - Pyspark - 如何在单个数据框中获取数值列的基本统计数据(平均值、最小值、最大值)以及分位数(25%、50%)
- git - 为什么“在 Github 上共享项目”(来自 IntelliJ)有效,但“git push remote origin master”(来自终端)没有?
- python - 函数知道传递给它的值的索引
- c++ - macos 是内置于 clang 编译器还是内置于 xcode ide?