简体   繁体   中英

fetch first column from table

First of all thanks for reading this.
I'm having this function setup

function SelectHydross($conn){
    $sql = "SELECT * from HydrossTheUnstable"; 
    $data = $conn->query($sql);

    $title = $data->fetchColumn(1);
    echo '<div><table>';
    echo '<tr>';
    echo "<th>" . $title['BossName'] . "</th>";

Which gives me Warning: Illegal string offset 'BossName'
Line 70 is: echo "<th>" . $title['BossName'] . "</th>"; echo "<th>" . $title['BossName'] . "</th>";

If i'm using this code

function SelectHydross($conn){
    $sql = "SELECT * from HydrossTheUnstable"; 
    $data = $conn->query($sql);
    $title = $data->fetch(PDO::FETCH_ASSOC); 
    echo '<div><table>';
    echo '<tr>';
    echo "<th>" . $title['BossName'] . "</th>";

It does skip the first column and starts with the 2nd.
I don't know any way out at the moment and i'd like to ask for some help!

This is how my table looks like

在此处输入图片说明

As I outlined in comments which I won't repeat (you can read those), you can use LIMIT with an offset that can skip a row.

Ie: LIMIT 1,10 in a SELECT statement.

The manual:

Example:

[LIMIT {[offset,] row_count | row_count OFFSET offset}]

As stated in the comment, fetchColumn targets one row and returns a value, not an array.

Replace this line :

$title = $data->fetchColumn(2);

with

$title = $data->fetch(PDO::FETCH_ASSOC);

and your first solution will work.

Edit : If not, maybe you are fetching the results before this line. PDOStatement::fetch returns the first line of a rowset, it does not begin on the second one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM