dongma7725 2012-07-03 19:37
浏览 61
已采纳

postgres php不给结果集中的每一列

i am having a product master table and various other tables containing properties of product,the query :

select p.description, category.value, colour.value, wood.value, brand.value, type.value, fabric.value, model.value from product_master p, category, colour, wood, brand, type, fabric, model where p.category_code=category.category_code and p.colour_code = colour.colour_code and p.wood_code = wood.wood_code and p.brand_code = brand.brand_code and p.type_code = type.type_code and p.fabric_code = fabric.fabric_code and p.model_code = model.model_code

is working fine in pgAdmin but in php it is giving only 2 columns, i am getting result back via AJAX

my php code is

<?php
// Connecting, selecting database
$dbconn = pg_connect("host=***** dbname=*** user=*** password=***")
    or die('Could not connect: ' . pg_last_error());

// Performing SQL query
$query = ' select p.description, category.value, colour.value, wood.value, brand.value, type.value, fabric.value, model.value from product_master p, category, colour, wood, brand, type, fabric, model where p.category_code=category.category_code and p.colour_code = colour.colour_code and p.wood_code = wood.wood_code and p.brand_code = brand.brand_code and p.type_code = type.type_code and p.fabric_code = fabric.fabric_code and p.model_code = model.model_code ';
$result = pg_query($query) or die('Query failed: ' . pg_last_error());



echo pg_affected_rows($result) ;

echo "
";

echo pg_num_fields($result);


// Printing results in HTML
echo "<table>
";
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
    echo "\t<tr>
";
    foreach ($line as $col_value) {
        echo "\t\t<td>$col_value</td>
";
    }
    echo "\t</tr>
";
}
echo "</table>
";

// Free resultset
pg_free_result($result);

// Closing connection
pg_close($dbconn);
?>
  • 写回答

1条回答 默认 最新

  • doutangkao2789 2012-07-03 19:50
    关注

    You are using pg_fetch_array() incorrectly. You can't pass the second parameter as NULL because it indicates which line is being read.

    Try this:

    while ($line = pg_fetch_array($result)) {
        echo "\t<tr>
    ";
    
        foreach ($line as $col_value) 
            echo "\t\t<td>$col_value</td>
    ";
    
        echo "\t</tr>
    ";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Semantic Kernel + OllamaSharp 集成本地大语言模型
  • ¥15 channels运行加载异常
  • ¥15 用C语言实现语音的读取及播放
  • ¥15 poi怎么在指定位置之后生成多个表格
  • ¥15 nginx在location设置了client_max_body_size依旧报错413
  • ¥15 C#添加WCF服务引用客户端调用方法没有反应
  • ¥15 stm32f103c8t6与esp8266
  • ¥15 使用Hadoop的MapReduce program
  • ¥15 Python发生IndexError错误如何解决?
  • ¥15 如何用matlab搭建激光器