2

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 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

And i don't need to do a connection? Like i have done?
This is the better way of creating a connection, using PDO. Give it a try!
@delboy1987uk ok man, thank you. Can you help me another time?? I edit my first post!
0

As your first problem is already answered and you switched to PDO I'll answer your follow-up question.

Since you are now using PDO you cant use the pg_* methods. You need to use the PDO methods now. See the documentation for PDO::query for more information about how to use them.

This should work with your query:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.