views:

132

answers:

3

I am using php and postgresql.

I need a function that:

connects to a db if not already connected run the query safely put the results into an object

For example: I do a query that is 'select * from test'. I get back 2 rows...There are three columns in 'test' (id,fname,lname).

I have an object named $ep. I want to be able to put the column name results into the object and also put the values into the object. So I could do a var_dump of $ep->fname and it would show me an array with two results.

I don't want to hardcode the column names into my function. I want it to be dynamic. So regardless of what table or columns I request it will put everything into the object for me.

A: 

An Object Relational Mapper (ORM) can give you a lot of the functionality you described (and more). Take a look at:

Here's what Propel code looks like:

$book = BookQuery::create()->findPK(123); // retrieve a record from a database
$book->setName('Don\'t be Hax0red!'); // modify. Don't worry about escaping
$book->save(); // persist the modification to the database

$books = BookQuery::create()  // retrieve all books...
  ->filterByPublishYear(2009) // ... published in 2009
  ->orderByTitle()            // ... ordered by title
  ->joinWith('Book.Author')   // ... with their author
  ->find();

foreach($books as $book) {
  echo  $book->getAuthor()->getFullName();
}
NullUserException
A: 

Try

pg_fetch_object

read the documentation

example

<?php 
$database = "test";
$db_conn = pg_connect ("host=localhost port=5432 dbname=$database");
if (!$db_conn): ?>
    <H1>Failed connecting to postgres database <?php echo $database ?></H1> <?php
    exit;
endif;

$ep = pg_query ($db_conn, "SELECT * FROM verlag ORDER BY autor");
$row = 0; // postgres needs a row counter other dbs might not 

while ($data = pg_fetch_object ($ep, $row)) {
    echo $data->fname;
    $row++;
}

?>
JapanPro
-1 its crazy, what wrong with it, please highlight so i can also learn.
JapanPro
This seems to work. I had to remove the $row from the while loop because I was getting an error message.
Keith
Yes please explain why someone voted this down?
Keith
i wonder some one is desperately doing on my ID. i am here to help not enmity!::)
JapanPro
I put what you did in a function. Now how do I make it so I can access the object outside the function?
Keith
two ways , either you pass $data from $data = pg_fetch_object ($ep, $row) to other function you are calling or make $data as local variable meaning , you can define $data at the top of portion, once its executed , you can access this data from any where. does this make sense if yes please voteup and accept as answer.
JapanPro
@JapanPro - I have tried global $data but it is not working. I don't want to pass it to another function.
Keith
add your code to jsfiddle.net please
JapanPro
http://jsfiddle.net/rAHKa/
Keith
updated here http://jsfiddle.net/rAHKa/3/
JapanPro
check final one http://jsfiddle.net/rAHKa/4/
JapanPro
@kaith did that worked?
JapanPro
@kaith are u there? hello any update!
JapanPro
@kaith, dont stop responding after getting answered. just update whats going on.
JapanPro
A: 

You would need to construct your own array or object, and then loop through the results, adding each of them to the array/object as you went.

There are some packages which include this functionality, like ezSQL.

Lucanos