Migrate to Postgresql

时间:2018-10-02 09:06:31

标签: php postgresql

I've been trying for days but I can not solve my problem. I state that I started to create my own web page on Mysql and it works perfectly. I had to migrate to Postgres and the problems started here.

For example there:

I have this error:

Fatal error: Uncaught Error: Call to a member function query() on string in C:\xampp\input.php:39 Stack trace: #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];

this is the config.php file that i used:

<?php

$conn_string = "host=localhost port=5432 dbname=postgres user=postgres password=123456789";
$dbconn = pg_connect($conn_string);

?>

EDIT.

I follow your suggests:

config.php

<?php 
$dbname = "postgres";
$host = "localhost";
$username = "postgres";
$dbh = new PDO("pgsql:dbname=$dbname;host=$host", $username, 123456789 ); 
?> 

For example i have another problem about a similar error:

Warning: pg_query() expects parameter 1 to be resource, object given in C:\xampp\htdocs\PhpProject1\select.php on line 5

Warning: pg_num_rows() expects parameter 1 to be resource, null given in C:\xampp\htdocs\PhpProject1\select.php on line 18

<?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)  
 {  

How can i solve this problem?? Thanks you

2 个答案:

答案 0 :(得分:2)

$conn_string is a string, not an object, so you can't call any methods on it! Even the variable name tells you so!

Are you sure you didn't mean to create a new PDO connection using that string?

Your string should be like this example:

$postgresDsn = 'pgsql:host=localhost;port=5432;dbname=testdb;user=someone;password=mypass'

$db = new PDO($postgresDsn, $user, $password);

Check the docs for PDO here:

http://php.net/manual/en/pdo.construct.php

Also, specific instructions for postgres:

https://secure.php.net/manual/en/ref.pdo-pgsql.connection.php

UPDATE

I just noticed, you aren't using $dbconn. Try changing:

$prenotato = $conn_string->query($checkdata)->pg_fetch_row()[0];

to

$prenotato = $dbconn->query($checkdata)->pg_fetch_row()[0];

Regardless, check PDO out, it's more secure, and easier to work with.

答案 1 :(得分:0)

由于您的第一个问题已得到回答,并且您切换到PDO,所以我会回答您的后续问题。

由于您现在正在使用PDO,因此无法使用pg_*方法。您现在需要使用PDO方法。有关如何使用它们的更多信息,请参见PDO::query的文档。

这应该与您的查询配合使用

$rows = $dbh->query($sql);