0

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'];

?> 
Ehsan Khodarahmi
  • 4,772
  • 10
  • 60
  • 87

2 Answers2

2

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

Marc
  • 310
  • 2
  • 7
2

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]; // elver@gmail.com
echo $_SESSION['resultado']['correo']; // elver@gmail.com

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.

Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37