Jump to content

[SOLVED] php and postgres help, real easy for vets


Diceman

Recommended Posts

so im trying to query a postgres database that will return multiple rows. this is just an example of what im wanting to do, not real. say i have a database called employees. i have 10 employee records and each record contains the first name, last name, and age. the data will be returned to me from the database as 10 rows with the first name, last name, and age if i were to select * from *;.

 

now for the tricky part. i want php to assign a variable name to the results, first name = $first_name or whatever, but i want each row's first name to be a different variable. IE, i want to be able to print $first_name1 or something like that to print the first name of the first row and $first_name2 to print for the first name of the second row and so forth for all of the variables, so 10 first names, 10 last names, 10 ages.

 

basically, i need to take all the data that has been returned and do some math with them to get some totals and whatnot, it is long and drawn out. the script im using on single row databases "works" but i get one rows values for all rows since the variables are getting overwritten each time it sees a new row.

 

how do i do that? im thinking an array will do it but i tried reading about them and just got lost. im not that great with php and just need the basic code so i can morph it into a script that can handle this database. if anything in this post isnt clear or doesnt make sense, let me know and i'll try to explain further.

Link to comment
Share on other sites

You sould be able to do this all within your query. We need more details of EXACTLY what it is your trying to do.

 

meh....

 

i graph poker statistics via cacti, via a php script, via a postgres database that has the stats in it. in order to get the proper values, i have to sql query for the stats i need and then do some math to get the proper values through averaging, division, and multiplication. at that point, it outputs to a single line and my cacti install reads and injects it into rrdtool.

 

all the databases i have done thus far have one limit of play in them. that outputs one line so every variable i get is written to from the query. the new database i have has 15-20 different limits. each limit is outputed on its own line from the sql query but the php script that i currently use gets the variables overwritten for each new line that comes out of the sql query. that means i get 15-20 lines of the same values when it is all said and done from the last line that wrote the variables.

 

i need to have each line tag a number or letter or something on the end to differentiate from the other lines so that each line is able to have math functions executed on it. im not interested in changing the sql query as it is about 50 lines long. doing the math in php is fine with me, i just need a way to keep the variables from overwriting themselves with each new line. 

Link to comment
Share on other sites

no, i asked a simple question and all i've gotten as response was "you dont know what you're talking about". if you're willing to answer the question i posed in the first post, great. i got it from there. if not, i'll just hit up another forum for the information.

Link to comment
Share on other sites

The question wasn't simple.  But I think I understand what you are asking.  Do you want something like this:

 

$first_names = array();
$last_names = array();
$ages = array();
$sql = "SELECT blah blah blah";
$res = pg_query($pg_conn, $sql) or die("Query $sql failed");
while ($row = pg_fetch_assoc($res)) {
  $first_names[] = $row['first_name'];
  $last_names[] = $row['last_name'];
  $ages[] = $row['ages'];
}
print "Row 4 is {$first_name[4]} {$last_name[4]}, age {$ages[4]}\n";

 

Edit: Note that the first row will be numbered 0, not 1

Link to comment
Share on other sites

The question wasn't simple.  But I think I understand what you are asking.  Do you want something like this:

 

$first_names = array();
$last_names = array();
$ages = array();
$sql = "SELECT blah blah blah";
$res = pg_query($pg_conn, $sql) or die("Query $sql failed");
while ($row = pg_fetch_assoc($res)) {
  $first_names[] = $row['first_name'];
  $last_names[] = $row['last_name'];
  $ages[] = $row['ages'];
}
print "Row 4 is {$first_name[4]} {$last_name[4]}, age {$ages[4]}\n";

 

Edit: Note that the first row will be numbered 0, not 1

 

that seems like it should do what i want. i will need to retrofit it a little but i'll try it out and see what i get. thanks for the information.

 

no disrespect intended towards any parties but i have joined a couple programming forums on an unrelated issue in the past few weeks and every single one of them have done the same thing to me. seems like i cant get a straight answer out of anyone these days.

Link to comment
Share on other sites

You have "you dont know what you're talking about". in quotes. Diceman, who are you quoting?

 

it isnt a direct quote more or less reading between the lines. refer to the first reply to this thread for further insight. if that was not the intent, so be it. just stating how it came across to me.

Link to comment
Share on other sites

"Your whole concept sounds a bit dodgy to me. can you explain WHY you think you need to do things this way?

 

Maybe some sample code." is what Thorpe wrote.

 

 

Sounded to me like he was trying to help. Lay down the defenses. Everyone is cool here and we all like helping one another.

Link to comment
Share on other sites

Especially if you consider more than half the people who post here want something different and have not looked at php.net for the answer and than they expect other users to write out their code for them.

 

Basically you were showing a huge lack of effort to display that data by not even giving us a starting point that you were trying.

 

Next time try first and if that does not work post that code with the description of what you are trying to do. People are more willing to help if someone has put forth the effort and it is visably seen with code.

 

--FrosT

Link to comment
Share on other sites

regardless, i asked a specific question and it was answered with multiple questions instead of a small block of code. that is the point being made. all i wanted was an example so that i could adapt it into my script. after i got the example i requested, i was able to adapt the script and it is now working as desired.

Link to comment
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.