Search for MySQL data list

2

Currently on my system, I'm calling each column at once:

$sql2 = mysqli_query($db, "SELECT group_id FROM login WHERE userid = '$userid'");
$exibe2 = mysqli_fetch_assoc($sql2);
$_SESSION['group_id']=$exibe2['group_id'];

And in the file I'm calling it like this:

echo ' '.$_SESSION['group_id'].' ';

It works perfectly for just one data in a table, but if I wanted to do something like:

$sql4 = mysqli_query($db, "SELECT account_id,userid,sex,email,group_id,last_ip,unban_time,diasvip,cash2 FROM login ORDER BY account_id WHERE userid = '$userid'");
$exibe4 = mysqli_fetch_assoc($sql4);

In case I want to fetch several columns that have multiple results each, how do I call this result in PHP?

    
asked by anonymous 21.03.2015 / 18:07

2 answers

1

I do not like this way of working with results but if that's what you chose let's go for it. For the other coulnas just follow the same technique used for a column:

$sql4 = mysqli_query($db, "SELECT account_id, userid, sex, email, group_id, last_ip, unban_time, diasvip, cash2 FROM login ORDER BY account_id WHERE userid = '$userid'");
$exibe4 = mysqli_fetch_assoc($sql4);
$_SESSION['account_id'] = $exibe4['account_id'];
$_SESSION['userid'] = $exibe4['userid'];
$_SESSION['sex'] = $exibe4['sex'];
$_SESSION['email'] = $exibe4['email'];
$_SESSION['group_id'] = $exibe4['group_id'];
$_SESSION['last_ip'] = $exibe4['last_ip'];
$_SESSION['unban_time'] = $exibe4['unban_time'];
$_SESSION['diasvip'] = $exibe4['diasvip'];
$_SESSION['cash2'] = $exibe4['cash2'];

I think it is possible that it can be played directly in $_SESSION without using $exibe4 but as I can not test I will not guarantee.

I also think I could save all $exibe4 to $_SESSION and then read each element. But again I will not guarantee because I can not test. It would look something like this:

$_SESSION['verdados'] = $exibe4;

and the use would be:

echo ' '.$_SESSION['verdados']['account_id'].' ';
// ... continua aqui para todas as colunas
    
21.03.2015 / 18:35
1

If you want to collapse all columns and pass their values to the session, you can use foreach() :

$sql4 = mysqli_query($db, "SELECT account_id,userid,sex,email,group_id,last_ip,unban_time,diasvip,cash2 FROM login ORDER BY account_id WHERE userid = '$userid'");
$exibe4 = mysqli_fetch_assoc($sql4);

foreach ($exibe4 as $k => $v) {
    $_SESSION[$k] = $v;
}

See example on Ideone .

    
21.03.2015 / 18:42