首页 > 解决方案 > 迁移到 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)  
 {  

我怎么解决这个问题??谢谢

标签: phppostgresql

解决方案


$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,它更安全,更易于使用。


推荐阅读