Jump to content

Recommended Posts

I'm new to php(and to SQL), at work I am working on a web page(html/php)  that is dealing w/ a Postgres SQL database(obviously why I'm in this section)

and I was wondering if there was a function (or a query) that allows me to assign variables to values within a table....

 

I started some code but got a little lost...

I started with this... (not sure if it is usable at all)

 

$select_variable = pg_query($conn, "SELECT blah blah blah FROM tablename WHERE id="$id_request");

 

but all of the php functions i can find are booleans.... is there any function that I can assign a variable to what I selected???

because I need to display those variables to the page.

I hope this isnt a completely ridiculous question lol  :)

 

 

Link to comment
https://forums.phpfreaks.com/topic/101550-is-there-a-function-for-this/
Share on other sites

I'm a little confused by your question, but I think you are looking for something like this:

 

$pg_res = pg_query($conn, "SELECT blah blah blah FROM tablename WHERE id="$id_request");
while ($row = pg_fetch_assoc($pg_res)) {
  var_dump($row);
}

 

var_dump() gives you a view of what's inside that $row variable.  Instead you can fetch individual columns by accessing $row['column_name']

ok so the link helped a lot....

 

although i keep running into problems (the story of my life)....

 

im trying to select the values from a specific id value in a psql table....

the table is as such

 

id serial

service varchar(10)

description varchar(200)

starttime timestamp(0)

and such.....

 

 

i keep getting errors when i try to display only one row containing a specific id number

 

right now i have....

 

$id_request = pg_fetch_assoc(pg_query($conn, "SELECT nextval('servicestatus_id_seq')"));

 

$result_service = pg_query("SELECT * FROM servicestatus  WHERE id='$id_request'");

 

while($line = pg_fetch_array($result_service, null))

{

        foreach($line as $col_value)

        {

                echo "$col_value \n";                <------ i'm aware i'm displaying too much kinda trying to fix that

        }

}

 

i'm getting errors about the $id_request variable, right now $id_request echo'd returns "Array" and won't output $id_request[index]

so when i delete the pg_fetch_assoc....  and echo $id_request i get Request id #4 or whatever number

 

I just want the 4 part but i cannot get it......

 

 

ahhh i'm confused.....

Let's try this:

 

<?php
  list($id_request) = pg_fetch_array(pg_query($conn, "SELECT nextval('servicestatus_id_seq')"));
  $result_service = pg_query("SELECT * FROM servicestatus  WHERE id='$id_request'");
  while($row = pg_fetch_assoc($result_service, null))
  {
    echo $row['id']."\n";
    echo $row['service']."\n";
    echo $row['description']."\n";
    echo $row['starttime']."\n";
  }
?>

 

With id_request. pg_fetch_array and pg_fetch_assoc will both return an array of values (even though there is only one value in there). We don't want to use assoc because we don't really have a column name (the column is a calculation). We also don't really care about the column name. So, we use fetch_array which returns a numerically indexed array. By wrapping $id_request with list(), it will put the first value in the array into $id_request.

 

For the second query, we will use assoc as it will return an associative array with the column names as the keys. Then, you can just reference the values with those keys.

 

Make more sense? More confused?

thanks for all the help guys...

 

i got everything figured out (especially a better understanding of what exactly is going on)...

took me a minute to figure out that nextval function was incrementing the id number everytime it was called

i hate problems that....

 

thanks again... :)

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.