Search Mysql data list

Asked

Viewed 91 times

2

Currently on my system, I am 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 so:

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

It works perfectly for only one table data, but in case 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 several results each, as I do to call this result in PHP?

  • How hard it is to do the same thing?

  • In case I was calling so: $_SESSION['group_id']=$exibe2['group_id']; now I would call it so? $_SESSION['verdados']=$exibe4; because it’s not just one more column I want to pick up, it’s several. And if I use it the way I posted it, it returns the following error: Notice: Array to string conversion in

  • But you’re wanting to do something different, if you want multiple columns, use multiple columns, just play what you did with one another. Not to invent a new way.

2 answers

1


I don’t like this way of working with results but if that’s what you chose we’ll go for it. For the other columns 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’s possible that it can be played straight into $_SESSION unused $exibe4 but since I can’t test I won’t guarantee.

I also think I could save the whole $exibe4 in $_SESSION and then read each element. But again I will not guarantee for not being able to test. It would be more or less this:

$_SESSION['verdados'] = $exibe4;

and the use would be:

echo ' '.$_SESSION['verdados']['account_id'].' ';
// ... continua aqui para todas as colunas

I put in the Github for future reference.

  • I don’t have very wide knowledge so it was the way I found :/ I tried to do the 1 way you explained it, it even returned the columns, but only the first row. I took WHERE userid = '$userid' to see if it returned the rest but without success.

  • The example you used in the question only returns one line as well. The solution I gave is in accordance with what you posted in the question. If you have another problem you want to solve you can open another question.

  • Ta but in case SELECT account_id, userid, sex, email, group_id, last_ip, unban_time, diasvip, cash2 FROM login ORDER BY account_id it should not return ALL lines of account_id, userid, sex... and sort by account_id ??

  • Yes, but this one query is different from the one you used. If you will actually return several lines and play in a $SESSION, I’m going to tell you not to do this. You have to find another solution, so the problem and the solution is completely different. Already warning to open another question asking for a solution and do not talk about using $_SESSION, leave open.

1

If you want to collect all the columns and pass their values to the session, you can use the 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 in Ideone.

  • In case I would call in the file only $v or $_SESSION[$k] ?

  • If you look at the demo, you can check that the $k is the name of the key that comes from the database and the $v its value... your session array will match the array coming from the database. If you have any questions, leave a comment that I will improve the answer later.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.