$ result only one line and then convert to session

I am trying to query one row from the table 'account'. I kind of messed up MYSQLI, so I need some advice. How can i do this?

$link = mysqli_connect("localhost","root","","database") or die("Error " . mysqli_error($link));

$query = "SELECT * FROM account WHERE username='".$user."' AND password='".$passcode."' LIMIT 1";
$result = $link->query($query) or die("Error " . mysqli_error($link));
$numrow = $result->num_rows;
$res = $result->fetch_assoc();

After the request, I want to copy the data to the session, I do like this:

    session_start();  
    $tableau = array($res['cod_acc'],$res['username'],$res['password']); 
    $_SESSION['tableau'] = $tableau;

And after that, how can I print the data?

    $tableau = $_SESSION['tableau']; 
    echo "$tableau['username']";
+4
source share
2 answers

From your question:

How can I print the data?

First of all, you need to add error_reporting()to your code:

error_reporting(E_ALL);

You store the values ​​in an array for $_SESSION:

$tableau = array($res['cod_acc'],$res['username'],$res['password']); 
$_SESSION['tableau'] = $tableau;

If you look at your session array, this is not an associative array .

, , :

$tableau = $_SESSION['tableau']; 
echo $tableau['username'];

:

:

echo $tableau[1]; // username on second index.

2:

, :

$tableau = array(
"cod_acc"=>$res['cod_acc'],
"username"=>$res['username']); 
$_SESSION['tableau'] = $tableau;

. , , , .

:

, - .

+2

, . , .

0

Source: https://habr.com/ru/post/1627563/


All Articles