1

I have a strange question. I need to send some code to a client without having access to the server to test my code. In addition, it's using postgreSQL which I've never used, and I've not done PHP for a while!

In order to save some time, I'd really appreciate if someone could tell me if this code will do what I want?

example feed

<?
$sql = "SELECT * FROM V_SIDE_MENU_E";

include 'db.inc.php';
?>

db.inc.php

$connectString = 'host=localhost dbname=myDatabase user=foo password=bar';
$link = pg_connect($connectString);

if (!$link) {
    echo "error";
} else {

$result = pg_query($link, $sql);
$rows = array();
while($r = pg_fetch_assoc($result)) {
    $rows[] = $r;
}
print json_encode($rows);
}

2 Answers 2

6

I would change

$rows = array();
while($r = pg_fetch_assoc($result)) {
    $rows[] = $r;
}
print json_encode($rows);

into

print json_encode(array_values(pg_fetch_all($result)));

But that's just a style thing -- your code should work.

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

2 Comments

Many thanks Billy, that's good to know. Unfortunately I've just found out that the server the code is gonna run on is 5.1.6, which I understand doesn't have json_encode. But I found a __json_encode function in the comments here which sounds like it should work for me: php.net/manual/en/function.json-encode.php
@Marky: Tell whoever's running the server to update their copy of PHP. 5.1.6 is riddled with holes. You should be able to get json_encode by installing a PECL extension on PHP < 5.2.
2

Tested on your mysql ( it looks like it will work ). Your SELECT will work same in PostgreSQL like mySQL

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.