I need some help with my code as I have got a problem with storing the data in the $_SESSION
array. I want to connect to two different database tables at the same time while I am fetching the data of username
, id
, firstname
and current_campaign
from the user table and also fetching the data of campaign_name
from the campaign table so I can store them in the $_SESSION
array.
However, when I tried this:
$link = mysqli_connect('localhost', 'mydbusername', 'mydbpassword', 'mydbname');
$sql = "SELECT id, username, password, firstname, current_campaign FROM users WHERE username = ?";
$campaign_db = "SELECT campaign_name FROM campaign WHERE username = 'myusername'";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_username);
// Set parameters
$param_username = $username;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Store result
mysqli_stmt_store_result($stmt);
// Check if username exists, if yes then verify password
if(mysqli_stmt_num_rows($stmt) == 1){
// Bind result variables
mysqli_stmt_bind_result($stmt, $id, $username, $hashed_password, $firstname, $lastname, $email, $gender, $current_campaign, $country);
if(mysqli_stmt_fetch($stmt)) {
if(password_verify($password, $hashed_password)) {
// Password is correct, so start a new session
session_start();
// Store data in session variables
$_SESSION["loggedin"] = true;
$_SESSION["id"] = $id;
$_SESSION["username"] = $username;
$_SESSION["firstname"] = $firstname;
$_SESSION["current_campaign"] = $current_campaign;
if($stmt_1 = mysqli_prepare($link, $campaign_db))
{
if(mysqli_stmt_execute($stmt_1))
{
mysqli_stmt_store_result($stmt_1);
if(mysqli_stmt_num_rows($stmt_1) >= 1)
{
mysqli_stmt_bind_result($stmt_1, $campaign);
/* execute query */
$stmt_1->execute();
/* Get the result */
$result = $stmt_1->get_result();
$num_of_rows = $result->num_rows;
while ($campaign = $result->fetch_assoc())
{
$_SESSION["campaign"] = $campaign['campaign_name'];
}
}
}
}
}
}
}
It will give me the outpit which it show like this:
Array ( [campaign] => facebook )
It should be:
Array ( [campaign] => somename [campaign] => youtube [campaign] => google [campaign] => linkedlin [campaign] => bing [campaign] => facebook )
I think the problem lies somewhere in this code:
if($stmt_1 = mysqli_prepare($link, $campaign_db))
{
if(mysqli_stmt_execute($stmt_1))
{
mysqli_stmt_store_result($stmt_1);
if(mysqli_stmt_num_rows($stmt_1) >= 1)
{
mysqli_stmt_bind_result($stmt_1, $campaign);
/* execute query */
$stmt_1->execute();
/* Get the result */
$result = $stmt_1->get_result();
$num_of_rows = $result->num_rows;
while ($campaign = $result->fetch_assoc())
{
$_SESSION["campaign"] = $campaign['campaign_name'];
}
}
}
}
What I can see in the array for the $_SESSION
as there is only one data that are stored in the array. I can be able to store the username, id, firstname and current_campaign in the array with no problem.
Can you please show me an example how I can be able to connect to two different database tables at the same time while I am fetching the data to store in the $_SESSION
array?
Thank you.