need some help with cookies

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mattman098
Forum Newbie
Posts: 16
Joined: Wed Feb 10, 2010 1:45 pm

need some help with cookies

Post 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!
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: need some help with cookies

Post 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!
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: need some help with cookies

Post 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
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: need some help with cookies

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: need some help with cookies

Post by AbraCadaver »

What is the value of $i the very first time through your loop?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: need some help with cookies

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: need some help with cookies

Post 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.
Last edited by AbraCadaver on Wed Feb 17, 2010 4:27 pm, edited 1 time in total.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: need some help with cookies

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: need some help with cookies

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
mattman098
Forum Newbie
Posts: 16
Joined: Wed Feb 10, 2010 1:45 pm

Re: need some help with cookies

Post 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!
mattman098
Forum Newbie
Posts: 16
Joined: Wed Feb 10, 2010 1:45 pm

Re: need some help with cookies

Post 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
Post Reply