Problems printing a session in php

I have a problem while passing information with $_SESSION in php. Every time that I try it, it sends me a message saying "Array to string conversion".
Here is the code:

<?php

session_start(); 
ob_start();
include("conexion3.php");
$con = mysql_connect($host,$user,$pw) or die("Error");
mysql_select_db($db,$con) or die("Error DB");

$sel2 = mysql_query("SELECT usuario_n,correo FROM usuarios WHERE usuario_n='machan'",$con);
$sesion = mysql_fetch_array($sel2);

$_SESSION['resultado'] = $sesion;

echo $_SESSION['resultado'];

?> 

Upvotes: 0

Views: 59

Answers (2)

Marc
Marc

Reputation: 310

You're trying to echo out an array. $session stores the array you fetched from your mysql db. Try the following instead of that echo statement:

print_r($_SESSION['resultado']);

http://php.net/manual/en/function.print-r.php

Upvotes: 2

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

The answer lies in your last comment. The problem is, $_SESSION['resultado'] is an array and with echo $_SESSION['resultado']; you're trying to convert array to string, hence the message.

You can do something like this to get the values:

echo $_SESSION['resultado'][0]; // machan
echo $_SESSION['resultado']['usuario_n']; // machan
echo $_SESSION['resultado'][1]; // [email protected]
echo $_SESSION['resultado']['correo']; // [email protected]

Sidenote: Please don't use the mysql_ database extensions, they were deprecated in PHP 5.5.0 and were removed in PHP 7.0.0. Use mysqli or PDO extensions instead. And this is why you shouldn't use mysql_ functions.

Upvotes: 2

Related Questions