Jump to content

Mysqli query $result into a $_SESSION. Can I?


jomla

Recommended Posts

Hi at all

I tryed to insert full $result of a mysqli query into a session variable to can use it anyware.

It work fine when I work into index.php but not into other pages that have always session_start called.

Is there some thing to know about?
thank you very much

Link to comment
Share on other sites

You can't store that into a session because the object only works while the connection to the MySQL server is open. Once the connection is closed the object stops working.

 

If you want to store all the results, store it in an array:

$results = $mysqli->query($query);
$data = array();
while($row = $mysqli->fetchAssoc()) {
  $data[] = $row;
}
$_SESSION['iml'] = $data;
Link to comment
Share on other sites

Hello Jomla.

 

I'm a newbie teaching myself php and MySQL.

I've been looking at session since I started in June 2016.

 

The way I got to read a database MySQL was:-

 

$db_host = "localhost"; // $db_host
$db_user = "<database username>"; // $db_user
$db_pass = "<database password>"; // $db_pass
$db_databasename = "<database name>"; // $db_databasename

$conn = new mysqli($db_host, $db_user, $db_pass, $db_databasename);

if(! $conn)
{
die('Connection Failed'.mysql_error());
}

$sql = "SELECT Member_id, Fullname, Username, Email, Password, Location, Country, DOB, Gender, Relationship, Photo, Tzone, WebsiteURL, MembershipType, AccountStatus, BusinessAddress, SignupDate, LastVisitDate, is_online, AccountPrivacy, UserContactPrivacy, AutoRenewPass, AutoRenewDate, IP_Address, AccountClosedDate, AccountDeleteDate, Remarks FROM phpbear_registration";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if ( $row["Email"] == $user_email) {
if ( $row["Password"] == $user_password) {
switch ($row["AccountStatus"] ) {
case "Suspended":
echo "This account is Suspended.";
header("Refresh:5; url=/index.php");
$conn->close();
exit();
case "Closed":
echo "This account was closed on:- ". $row["AccountClosedDate"];
header("Refresh:5; url=/index.php");
$conn->close();
exit();
}

if ( $row["AccountStatus"] == "Active" or $row["AccountStatus"] == "New") {
// echo "id: " . $row["Member_id"]. " - Name: " . $row["Fullname"]. " " . $row["Email"]. "<br>";
// Get member's data from database
$_SESSION['member_Member_id'] = $row["Member_id"];
$_SESSION['member_Accountname'] = $row["Fullname"];
$_SESSION['member_Username'] = $row["Username"];
$_SESSION['member_Email'] = $row["Email"];
$_SESSION['member_Password'] = $row["Password"];
$_SESSION['member_Location'] = $row["Location"];
$_SESSION['member_Country'] = $row["Country"];
$_SESSION['member_DOB'] = $row["DOB"];
$_SESSION['member_Gender'] = $row["Gender"];
$_SESSION['member_Relationship'] = $row["Relationship"];
$_SESSION['member_Photo'] = $row["Photo"];
$_SESSION['member_Tzone'] = $row["Tzone"];
$_SESSION['member_WebsiteURL'] = $row["WebsiteURL"];
$_SESSION['member_MembershipType'] = $row["MembershipType"];
$_SESSION['member_AccountStatus'] = $row["AccountStatus"];
$_SESSION['member_BusinessAddress'] = $row["BusinessAddress"];
$_SESSION['member_SignupDate'] = $row["SignupDate"];
$_SESSION['member_LastVisitDate'] = $row["LastVisitDate"];
$_SESSION['member_isonline'] = $row["is_online"];
$_SESSION['member_Privacy'] = $row["AccountPrivacy"];
$_SESSION['member_Contact_Privacy'] = $row["UserContactPrivacy"];
$_SESSION['member_AutoRenewPassword'] = $row["AutoRenewPass"];
$_SESSION['member_AutoRenewPasswordDate'] = $row["AutoRenewDate"];
//$_SESSION['member_IPAddress'] = $row["IPAddress"];
/* $_SESSION['member_AccountClosedDate'] = $row["AccountClosedDate"];
$_SESSION['member_AccountDeleteDate'] = $row["AccountDeleteDate"];
$_SESSION['member_Remarks'] = $row["Remarks"]; */
/* $sql = "UPDATE phpbear_registration SET IPAddress='' WHERE Member_id='$userID'";
mysqli_query($conn,$sql); */
/* ------------------------------------------------------------
if ($row["AccountStatus"] = "New") {
$sql = "UPDATE phpbear_registration SET AccountStatus='Active' WHERE Member_id='$_SESSION['member_Member_id']'";
$sql = "UPDATE phpbear_registration SET AccountDeleteDate='--/--/----' WHERE Member_id='$_SESSION['member_Member_id']'";
mysqli_query($conn,$sql);
}
---------------------------------------------------------------------------------------- */
$conn->close();
require($_SERVER['DOCUMENT_ROOT'].'/Core/user/user.php');
exit();
}
}
}
}
}
$conn->close();

 

I know it's a long winded but at that time I didn't know how else to store the results.

Good Luck.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...