简体   繁体   中英

PDO get first element of a SQL query result

How do I get 7 from the table below with a query like:

SELECT name from table WHERE id = 3

That is, the first item of the array.

id name
1   [2,3]
2   [3,5,6]
3   [7,8]

This is how the PDO is looking like. I can get the

$db = new PDO('...');
$sth = $db->prepare('SELECT `name` AS grupo
FROM `table`
WHERE `id` =3');

$sth->execute();
$resultado= $sth->fetch();
echo ($resultado)[0];

Which is returning:

[7,8]

Have a look at PDO::fetchColumn .

Returns a single column from the next row of a result set

EDIT:

echo json_decode($sth->fetchColumn(), true)[0];

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