Page 1 of 1

need some help with cookies

Posted: Wed Feb 17, 2010 11:45 am
by mattman098
I have a bit of an issue here creating cookies...

I have an SQL query which returns values to an array (which works, and prints into a table etc..) but i want to also store the results of the query into cookies so that i can use them on another page later.

I tried doing it like this with an index variable dynamically creating cookies for each result produced and assigning a value to it.

When I try echoing the cookie, nothing happens...

Code: Select all

while($row = mysql_fetch_array($result))
{
  setcookie("Username".$i, $row['Username'], time()+3600);
  setcookie("FirstName".$i, $row['FirstName'], time()+3600);
  setcookie("Surname".$i, $row['Surname'], time()+3600);
  setcookie("School".$i, $row['School'], time()+3600);
  setcookie("Framework".$i, $row['Framework'], time()+3600);
  setcookie("Account".$i, $row['Account'], time()+3600);
  setcookie("Banned".$i, $row['Banned'], time()+3600);
  $i++;
}
 
echo "<p>";
echo $_COOKIE["Username0"];
echo "</p>";
any advice would be appreciated!

Re: need some help with cookies

Posted: Wed Feb 17, 2010 2:59 pm
by chopsmith
You are using mysql_fetch_array (fetches an indexed array), then treating $row as an associative array. Try this in place of your first line:

Code: Select all

 
while ($row = mysql_fetch_assoc($result))
 
mattman098 wrote:I have a bit of an issue here creating cookies...

I have an SQL query which returns values to an array (which works, and prints into a table etc..) but i want to also store the results of the query into cookies so that i can use them on another page later.

I tried doing it like this with an index variable dynamically creating cookies for each result produced and assigning a value to it.

When I try echoing the cookie, nothing happens...

Code: Select all

while($row = mysql_fetch_array($result))
{
  setcookie("Username".$i, $row['Username'], time()+3600);
  setcookie("FirstName".$i, $row['FirstName'], time()+3600);
  setcookie("Surname".$i, $row['Surname'], time()+3600);
  setcookie("School".$i, $row['School'], time()+3600);
  setcookie("Framework".$i, $row['Framework'], time()+3600);
  setcookie("Account".$i, $row['Account'], time()+3600);
  setcookie("Banned".$i, $row['Banned'], time()+3600);
  $i++;
}
 
echo "<p>";
echo $_COOKIE["Username0"];
echo "</p>";
any advice would be appreciated!

Re: need some help with cookies

Posted: Wed Feb 17, 2010 3:38 pm
by flying_circus
chopsmith wrote:You are using mysql_fetch_array (fetches an indexed array), then treating $row as an associative array.
The manual says the default is MYSQL_BOTH, which returns the array as both associative and by numerical indice

http://us.php.net/manual/en/function.my ... -array.php

Re: need some help with cookies

Posted: Wed Feb 17, 2010 3:47 pm
by chopsmith
Hmm, I did not know that. Anyway, it was the first thing that struck me, and I could've sworn I've had a problem using mysql_fetch_array() and treating it as an assoc before, but I could be wrong. Thanks for the heads up.
flying_circus wrote:
chopsmith wrote:You are using mysql_fetch_array (fetches an indexed array), then treating $row as an associative array.
The manual says the default is MYSQL_BOTH, which returns the array as both associative and by numerical indice

http://us.php.net/manual/en/function.my ... -array.php

Re: need some help with cookies

Posted: Wed Feb 17, 2010 4:12 pm
by AbraCadaver
What is the value of $i the very first time through your loop?

Re: need some help with cookies

Posted: Wed Feb 17, 2010 4:18 pm
by flying_circus
AbraCadaver wrote:What is the value of $i the very first time through your loop?
$i=0 :wink:

viewtopic.php?f=1&t=113008

Re: need some help with cookies

Posted: Wed Feb 17, 2010 4:19 pm
by AbraCadaver
flying_circus wrote:
AbraCadaver wrote:What is the value of $i the very first time through your loop?
$i=0 :wink:
How so?

But I do agree with using sessions. Just answering the question first ;-) Also, you're correct in the other thread about cookies being available on the next page load as well.

Re: need some help with cookies

Posted: Wed Feb 17, 2010 4:23 pm
by flying_circus
This was a duplicate thread, he posted all of the code in the thread I linked to about 4 or 5 posts down.

Re: need some help with cookies

Posted: Wed Feb 17, 2010 4:29 pm
by AbraCadaver
flying_circus wrote:This was a duplicate thread, he posted all of the code in the thread I linked to about 4 or 5 posts down.
I see you got him straight on the other thread. My point was that $i is not set the first time through, which is not 0 and will not give Username0, but just Username.

But I see it in the other one now.

Re: need some help with cookies

Posted: Thu Feb 18, 2010 7:42 am
by mattman098
haha broswer crashed halfway through posting this, so i guess the duplicate thing is because of that, really sorry guys

thanks for all the feedback though =]

I'm going to be trying the ob_start() thing when i get back to where i can access my server later on

Although i did also have the idea, for incase i want to be a smartass for my project and talk about security, of using sessions, and then maybe getting in my quota of cookies by using a cookie to store a session ID, does this work?

if you want to see the other post it is here... viewtopic.php?f=1&t=113008

But let's stick with this one now, has more replies i guess =D

thanks again!

Re: need some help with cookies

Posted: Thu Feb 18, 2010 3:18 pm
by mattman098
I solved it!

basically all i had to do was either change the name of the '$row' variable/array that the cookie values are called from, or recall the entire query...

I'm not sure which yet as I changed both at the same time and I'm needing to wait an hour before these cookies expire haha, i could delete them then test but I have plenty of other work to do in this hour so I'll wait

Thanks for all your help guys, much appreciated