0

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
    )
10
  • your "It should be:" is not a valid array, keys must be unique Commented Dec 4, 2018 at 21:18
  • @IdontDownVote what i should use then? Commented Dec 4, 2018 at 21:25
  • numeric keys like like nick shows below Commented Dec 4, 2018 at 21:27
  • I have tried it but it give me this [0] => facebook when i tried this $_SESSION['campaign'] = array($campaign); Commented Dec 4, 2018 at 21:30
  • 1
    This code is open to sql injection. Using prepare() doesn't constitute as a "prepared statement" per se; you should use one. Commented Dec 4, 2018 at 21:32

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you for this, but when I try this in the loop: $_SESSION['campaign'] = array($campaign);, it give me this [0] => facebook. Any idea?
@RobertJones you need to use $_SESSION['campaign'] = array(); outside the loop, then inside the loop you would use $_SESSION['campaign'][] = $campaign;
Oh I didn't realised that I need to use $_SESSION['campaign'] = array(); outside the loop so when I insert the $_SESSION['campaign'][] = $campaign; inside the loop, it works great. Thank you very much for your help so problem are now resolved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.