There are two codes, the first is not working, the data is taken from the database, the second is an analog, I take data from the array, does it work? There are options for the first?

Code with mysql:

session_start(); $db->conn_open(); $ses = session_id(); // где $db->q - mysql_query $ses_db = $db->q("SELECT * FROM `table` WHERE `ses` = '".$ses."'"); $ses_d = mysql_fetch_array($ses_db); $ses_data = $ses_d; $cookie_dn = mb_strtolower($ses_data["q_name"], "utf-8"); $cookie_dv = $ses_data['q_answ']; $retr = setcookie("question[".$cookie_dn."]",$cookie_dv,time()+1000000); var_dump($retr); // false var_dump($cookie_dn); // validated var_dump($cookie_dv); // validated var_dump($_COOKIE["question"]); // NULL die; 

DB class code:

 $db = new database; class database { var $dblink; function conn_open() { global $wdb; $this->dblink = mysql_connect($wdb['host'], $wdb['username'], $wdb['password']); mysql_select_db($wdb['name']); @mysql_query("SET NAMES utf8"); } function q($query) { return mysql_query($query); } } 

Code without mysql:

 session_start(); $db->conn_open(); $ses = session_id(); $ses_d = array("val"=>"1", "val2"=>"2"); $ses_data = $ses_d; $cookie_dn = mb_strtolower($ses_data["q_name"], "utf-8"); $cookie_dv = $ses_data['q_answ']; $retr = setcookie("question[".$cookie_dn."]",$cookie_dv,time()+1000000); var_dump($retr); // true var_dump($cookie_dn); // validated var_dump($cookie_dv); // validated var_dump($_COOKIE["question"]); // validated die; 
  • Are you in the first code just trying to output to the browser the data that you receive with a query from the database? They are coming? - n.osennij
  • @ n.osennij, but where there is "// validated", the data comes in correct, but the first version does not put the cookie, the second - easily. - John Smit Conor
  • "There are two codes, the first is not working, the data is taken from the database, the second is analog, I take data from the array, it works? Are there options for the first?" So what are you asking us? Have you correctly placed all the question marks and others? - n.osennij
  • @ n.osennij, I can not understand, the database request is adequate, the answer is adequate, no cookies are set, what could be the problem. I read similar forums, the cookie before displaying data in the browser, everything is correct. I will repeat the question, why is it not working, who has come across such an absurdity? - John Smit Conor
  • If any output (tags, empty lines, spaces, text, etc.) has already been transferred to the client before calling the function, setcookie () will cause a failure and return FALSE. If setcookie () succeeds, it returns TRUE. This, however, does not mean that the client application (browser) correctly received and processed the cookie. - Maxim Stepanov

1 answer 1

And remove from the name of the cookie []. Try it.

 <?php session_start(); //устанавливаем соедиение c БД тут в $connection $query = "SELECT * FROM table WHERE ses = '$ses'"; $result = $connection->query($query); if (!$result) {die($connection->error);}; $myrow = $result->fetch_array(MYSQLI_ASSOC); $cookie_dn = mb_strtolower($myrow['q_name'], 'utf-8'); $cookie_dv = $myrow['q_answ']; $cookie_name="question_${cookie_dn}"; $retr=setcookie($cookie_name, $cookie_dv, time()+1000000, '/'); var_dump($retr); echo $_COOKIE[$cookie_name]; ?> 
  • Thanks for the help, but I have php 5.3 and the code on mysql, your option is not missed. And the mistake was that in ['q_name'] the text was with spaces, I missed it through str_replace (), it all worked. - John Smit Conor