Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2010
    Posts
    16

    Question Unanswered: Problem displaying row content from table

    Hi all, not sure if this is a PHP question or more of a PostgreSQL question, but here goes.

    I am using PHP with a PostgreSQL database. To display the row content of one of my tables I have been using the following method:
    Code:
    $row_to_display_query = 'SELECT idx, image_pic, item_title, quantity FROM tbl01';
    $row_to_display_result = pg_query($row_to_display_query) or die("Query failed: " . pg_last_error());
    
    	while ($line = pg_fetch_array($row_to_display_result, null, PGSQL_ASSOC))		 {
    			
    
    	foreach ($line as $col_value) {
    	echo "\t\t<td>$col_value";
            }
    However, I have run into situations where it would be better if I had better control over each database field being displayed on the web page via PHP. I have tried this format below, but so far have not had any luck with it properly displaying:

    Code:
    $rowcount = count(idx);
    $index = 1;
    
    while ($index <= $rowcount){
    $idx = pg_query('SELECT idx FROM inventory WHERE idx = '. $index. ';');
    $image_pic = pg_query('SELECT image_pic FROM inventory WHERE image_pic = '. $index. ';');
    $item_title = pg_query('SELECT item_title FROM inventory WHERE item_title = '. $index. ';');
    $quantity = pg_query('SELECT quantity FROM inventory WHERE quantity = '. $index. ';');
    
    echo "<td>"+ $idx +"</td><td><A HREF=\"http://www.dbforums.com/images/"+ $image_pic + "\"></td><td>"+ $item_title 
    
    +"</td> + $quantity +"</td>";
    $index = $index + 1;
    	}
    
    echo "\t</tr>\n";
    echo "</table>\n";
    Any suggestions on how to get the second format to work would be appreciated.
    Last edited by RRT; 12-01-13 at 23:49. Reason: typos and clarification of question I'm asking

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Your first bit of code takes your query results and loops over each row.
    For each row it loops through the fields and displays them on the page.

    If I've read your question right then you want to cherry pick which columns to display and where...

    Looping over the rows is the same:
    Code:
    while ($line = pg_fetch_array($row_to_display_result, null, PGSQL_ASSOC)) {
       ...
    }
    In this next example I am going to only echo the "image_pic" column values:
    Code:
    while ($line = pg_fetch_array($row_to_display_result, null, PGSQL_ASSOC)) {
        echo $line["image_pic"];
    }
    HTH
    George
    Home | Blog

  3. #3
    Join Date
    Jan 2010
    Posts
    16
    Your assumption is correct, I do want to cherry pick which columns to display and where, however that coding change didn't seem to do it 100%. When I used:

    while ($line = pg_fetch_array($row_to_display_result, null, PGSQL_ASSOC)) {
    echo $line["image_pic"];
    echo "<A HREF=\"/images/"+ $line["image_pic"] + "</A>";
    }

    instead of giving me the "image_pic" filename, I get back numbers, such as:

    00000000

  4. #4
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Can you get anything back from other columns?
    What is the data type of "image_pic"?
    George
    Home | Blog

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •