missparx Posted April 17, 2008 Share Posted April 17, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/101550-is-there-a-function-for-this/ Share on other sites More sharing options...
rhodesa Posted April 17, 2008 Share Posted April 17, 2008 Here is a detailed example on how to use PgSQL with PHP: http://us3.php.net/manual/en/pgsql.examples.php Quote Link to comment https://forums.phpfreaks.com/topic/101550-is-there-a-function-for-this/#findComment-519491 Share on other sites More sharing options...
btherl Posted April 18, 2008 Share Posted April 18, 2008 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'] Quote Link to comment https://forums.phpfreaks.com/topic/101550-is-there-a-function-for-this/#findComment-520014 Share on other sites More sharing options...
missparx Posted April 18, 2008 Author Share Posted April 18, 2008 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..... Quote Link to comment https://forums.phpfreaks.com/topic/101550-is-there-a-function-for-this/#findComment-520534 Share on other sites More sharing options...
rhodesa Posted April 18, 2008 Share Posted April 18, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/101550-is-there-a-function-for-this/#findComment-520624 Share on other sites More sharing options...
missparx Posted April 19, 2008 Author Share Posted April 19, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/101550-is-there-a-function-for-this/#findComment-520935 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.