Jump to content

Recommended Posts

Hi

 

I've been trying to connect and query pgsql through PDO but can't seem to get very far.

 

I have a table consisting of the following:

id, title, owner, post, editdate, createdate, deleted

 

When I try to run a simple query I don't get any output whatsoever

 

<?php

try
{

$db = new PDO("pgsql:host=localhost;dbname=slukedb",blank,blank);

$sql = "SELECT title, owner, post, createdate FROM news_posts";
Foreach ($db->query($sql) as $row)
{
echo $row['title'] . ' - ' . $row['owner'] . ' - ' . $row['post'] . ' - ' . $row['createdate'] . '<br />';
}

$db = null;
} catch (PDOException $e)
{
echo $e->getMessage();
}
?>

 

This is given as an example of querying using PDO by almost all sites I've been to.

 

If I try the following I can get a bit further:

<?php
try {
$db = new PDO("pgsql:host=localhost;dbname=slukedb",blank,blank);

$sql = "SELECT title, owner, post, createdate FROM news_posts";

$stmt = $db->query($sql);

$result = $stmt->fetch(PDO::FETCH_ASSOC);

foreach ($result as $row) {
echo echo $row['title'] . ' - ' . $row['owner'] . ' - ' . $row['post'] . ' - ' . $row['createdate'] . '<br />';
}

$db = null;
} catch(PDOException $e) {
echo $e->getMessage();
}
?>

 

But it'll only display the first row in the table.

 

Thank you for your help

Link to comment
https://forums.phpfreaks.com/topic/65940-pgsql-via-pdo/
Share on other sites

The examples I see here are different:

 

<?php
$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name = ?");
if ($stmt->execute(array($_GET['name']))) {
  while ($row = $stmt->fetch()) {
    print_r($row);
  }
}
?>

 

This is using a prepared transaction, but fetching the result data should be the same.  $stmt->fetch() must be called to fetch each row individually.

Link to comment
https://forums.phpfreaks.com/topic/65940-pgsql-via-pdo/#findComment-330498
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.