简体   繁体   中英

How to get single element from $row[id]

I'm working on a project for PHPBB. I have a problem that I get some $row types. And they have some "id"s from MySQL, But I want theese "id"s by one by.

It's SQL query ;

$sql = $db->sql_build_query('SELECT', array(
    'SELECT'    => 'u.*, z.friend, z.foe, p.*',

    'FROM'      => array(
        USERS_TABLE     => 'u',
        POSTS_TABLE     => 'p',
    ),

    'LEFT_JOIN' => array(
        array(
            'FROM'  => array(ZEBRA_TABLE => 'z'),
            'ON'    => 'z.user_id = ' . $user->data['user_id'] . ' AND z.zebra_id = p.poster_id'
        )
    ),

    'WHERE'     => $db->sql_in_set('p.post_id', $post_list) . '
        AND u.user_id = p.poster_id'
));

$result = $db->sql_query($sql);

And then for user_id's

$rowset[$row['post_id']] = array(
        'hide_post'         => ($row['foe'] && ($view != 'show' || $post_id != $row['post_id'])) ? true : false,

        'user_id'           => $row['user_id'],
.
.

I logged $row[user_id] for in a topic and it return ;

  • 97777
  • 97778
  • 97779
  • 97783

But I want just first id i mean -> "97777" . So how can i pass just "97777" to a variable?

Dont suggest about sql level because I can't change any SQL command.

Dont suggest about sql level because I can't change any SQL command.

This is the problem though, your PHP is clearly selecting all 9 rows;

Using WHERE user_id = '97777' you will only select the record you want.

Why can you not edit the SQL? aren't you familiar with SQL or is the code your are using your own?

Without any code it is hard to say what you want to do:

reset($row);
$first_key = key($row);

or

reset($rows);
$first_key = key($rows);

Hard to guess without seeing any code.

You can limit your records using LIMIT clause.

SELECT column_name FROM table_name LIMIT 1;

It will return the first record of that table.

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