Reputation: 408
I need some help with storing the data in the arrays. I have got a problem with the arrays variable $_SESSION
, because it will only allow me to store one data at a time which it have overwritten the data.
When I try this:
$link = mysqli_connect('localhost', 'mydbusername', 'mydbpassword', 'mydbname');
$campaign_db = "SELECT campaign_name FROM campaign WHERE username = 'myusername'";
if($stmt = mysqli_prepare($link, $campaign_db))
{
// Set parameters
$param_username = $_SESSION['username'];
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt))
{
// Bind variables to the prepared statement as parameters
mysqli_stmt_execute($stmt);
$results = mysqli_stmt_get_result($stmt);
while ($rows = mysqli_fetch_array($results, MYSQLI_NUM))
{
foreach ($rows as $campaign)
{
$_SESSION['campaign'] = $campaign;
}
}
}
print_r($_SESSION);
// Close statement
mysqli_stmt_close($stmt);
}
I will only get this:
Array
(
[campaign] => facebook
)
It should be:
Array
(
[campaign] => somename [campaign] => youtube [campaign] => google [campaign] => linkedlin [campaign] => bing [campaign] => facebook
)
I have tried this:
$_SESSION['campaign'][] = $campaign;
It give me an error: Fatal error: Uncaught Error: [] operator not supported for strings. Stack trace: #0 {main} thrown.
Can you please show me an example how I can insert and store more than 1 data in the array for the variable $_SESSION
?
EDIT: When I try this:
while ($rows = mysqli_fetch_array($results, MYSQLI_NUM))
{
$_SESSION['campaign'] = array($rows);
)
And I have also try this:
while ($rows = mysqli_fetch_array($results, MYSQLI_NUM))
{
foreach ($rows as $campaign)
{
$_SESSION['campaign'] = array($campaign);
}
)
It still give me this:
[campaign] => Array
(
[0] => facebook
)
Upvotes: 0
Views: 29
Reputation: 147146
You must have initialised $_SESSION['campaign']
to a string somewhere in your code. Wherever you did that you need to change it to
$_SESSION['campaign'] = array('string value')
or if it's an empty string then just use array()
and then you will be able to use
$_SESSION['campaign'][] = $campaign;
in your loop.
Upvotes: 1