在PHP中使用pg_fetch_row引用列名

Léo*_* 준영 5 php postgresql

如何用PHP引用列名pg_fetch_row

关于我们用Cha调试的代码的示例.

$dbconn = pg_connect("host=localhost port=5432 dbname=noa user=noa password=123");
$result_titles_tags = pg_prepare( $dbconn, "query777",
    "SELECT question_id, title
    FROM questions
    WHERE question_id IN
    (    
        SELECT question_id
        FROM questions
        ORDER BY was_sent_at_time
        DESC LIMIT 50
    )
    ORDER BY was_sent_at_time
    DESC LIMIT 50;"
);
$result_titles = pg_execute( $dbconn, "query777", array());


while($row = pg_fetch_row( $result_titles )) {
    $question_id = $row[0];                   // This works but following does not
       // We cannot use here `$question_d = $row['question_id']      
                // Problem here: 
                // What is the reason that you cannot use $row['question_id']?
       // for some unknown reason
       //  
Run Code Online (Sandbox Code Playgroud)

Eri*_*ric 6

你正在寻找pg_fetch_assoc,它返回一个关联数组 - 即,你可以按名称调用字段.

pg_fetch_array允许您通过名称或索引调用它们.pg_fetch_object允许你将它们称为$row->question_id.

选择你最喜欢的一个.没有真正可观的速度差异.