I try to set cookie values from mysql DB, I have the code:

$result = mysql_query("select ID from table");
while ($thisrow=mysql_fetch_row($result))  
       {
	$gname="name_".$i;
	setcookie ("$gname", $thisrow[0]);
	$i++;
        }

Now the strange thing is, if the number of ID is less than 16, script runs without any issue, I can set cookie and retrieve those cookie values, however if the number of ID from DB is more than 16, script cannot set cookies, I don't know what causes this problem, anyone can give me a clue? thanks in advance.

It probably has nothing to do with the number 16, it may just so happen that the browser has run out of space to store cookies from your domain.
There is a limited amount of space allocated for each domain.

usually, you dont want to be setting all the rows in your mysql query result to cookies, since space is limited. However, you would rather just save meta data on the clients browser.
Then use this meta data to access settings for the client that is saved on your server database, or txt files etc.

Using your example:

// check if there is a cookie named saved_db_queries for this user
// The value of this cookie is the unique id for the file for which the users mysql queries are cached
$user_query_id = $_COOKIE['saved_db_queries'];

if (!$user_query_id) {
	// user has not been here, create a new file id to save mysql queries to
	$user_query_id = setcookie('saved_db_queries', $_SERVER['REMOTE_ADDR'].rand(0, 1000);
}

// this file is used to save the users query results
// its created out of the id we have saved as a cookie on the users computer
$file = 'cache/query_'.$user_query_id.'.txt';
$fp = fopen($file, 'w'); // create a pointer to a file resource/stream

$result = mysql_query("select ID from table");
while ($thisrow=mysql_fetch_row($result))  
{
	$gname="name_".$i;
    saveQueryToFile($fp, "$gname", $thisrow[0]):
	$i++;
}

fclose($fp); // close our file resource pointer

/**
* Writes a single line of string data to a txt file
*
*/
function saveQueryToFile($fp, $line_id, $line_data) 
{
	if (is_resource($fp)) 
	{
		$line = $line_id.' '.$line_data."\r\n";
		fwrite($fp, $line, strlen($line);	
	}
}

Here, we only set a cookie named "saved_db_queries" on the users computer, with the value being the a unique identifyer for the file on the server where we will save data for this user.

This way we dont need to save data on the client, jus the meta data needed to retrieve user based data for this user.

There are other ways you can save data client side, on the users browser, other than cookies. There however involve javascript.

Its usually called JavaScript Object Persistance, which you can look up on google.
An example is using Frames, such as Gmail.
All these however, only last while the user is viewing your website, once they close the browser, pfft.. gone.

usually, theres no need for saving lots of permanent data on the browser however, other methods will do.

Note: Saving data on the browser (cookies) can be misleading.
Every time a http request is made, cookie headers have to sent back to the server in order for your php to read the cookies on the browser.
So saving the data on your server in the first place, and using only an id to link that data to the user, actually saves bandwidth.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.