Hello! Help, please, have such a calculation.

<?php $queryps=$bd->query("SELECT COUNT(id_ms) FROM messages WHERE from_id!=$myrow2[id] AND is_read='0'"); $pos = $queryps->fetch(PDO::FETCH_NUM); ?> 

Where the variable $myrow2[id] equal to my id. The problem is that here there are 2 users in the first one correctly, for example 3, and in the second one, for example, also 3 not read, but for some reason, that place 3, 13. Please help me, where is my mistake?

  • Because from the table select all records, except belonging to the current user. Cap - etki
  • @Fike from_id! = $ Myrow2 [id] here a problem please tell me. - andreykartavtsev
  • @Andrey Kartavtsev, you would have come up with a table chart - etki '
  • @Fike The table schema is CREATE TABLE IF NOT EXISTS messages ( id_ms int (11) NOT NULL AUTO_INCREMENT, group_hash int (11) NOT NULL, from_id int (11) NOT NULL, message text COLLATE utf8_unicode_ci, timemsg datetime NOT NULL, is_read enum ( '0', '1') COLLATE utf8_unicode_ci NOT NULL DEFAULT '0', from_user_delete int (1) NOT NULL DEFAULT '0', to_user_delete int (1) NOT NULL DEFAULT '0', PRIMARY KEY ( id_ms ), KEY from_id ( from_id ), KEY is_read ( is_read )) ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci AUTO_INCREMENT = 1; - andreykartavtsev
  • @Fike INSERT INTO messages ( id_ms , group_hash , from_id , message , timemsg , is_read , from_user_delete , to_user_delete ) VALUES (63, 29542, 6, 'What are you doing?', '2014-05-03 09:00:24', '1', 0, 0), - andreykartavtsev

1 answer 1

 $queryps=$bd->query("SELECT COUNT(id_ms) FROM messages WHERE from_id=".$myrow2['id']." AND is_read='0'"); 

Try never write PHP variables in quotes! This is my advice for the future.

  • @ Nik555 Thank you very much, and why not write? load server or just not right? or longer code is executed? - andreykartavtsev
  • When I started learning PHP, I immediately tried to remember how to write code in a better and more optimized way. Variables enclosed in double quotes slow down the PHP interpreter greatly, especially in large loops. It is even recommended to use single quotes even more often if the special characters \ t, \ n are not found in them. <? php echo 'Variable str ='. $ str; // this code is faster. echo "Variable str = $ str"; // the result is the same, but slower. ?> Of course, if you need to display only one line, then you won't notice much of a difference, but it's better to get used to writing code optimized right away. - Nik555
  • @ Nik555, I'm a fan of single quotes myself, but how much do you think is spent on parsing strings in double quotes? ideone.com/MOENnx - this example says about 125-130% of the parsing time for single. The same script on my laptop says the difference is about 4% not in favor of singles, I suspect that the creators of php jumped up on this. But it’s not about this, but the fact that any large cycle, access to the database or a not very efficient hashing algorithm almost surely consumes more CPU time than all the work with lines outside cycles in an application. - etki
  • Well, separately, I note that the cycle itself in terms of execution lies in similar values ​​(again, on my laptop, for some reason, it eats more than just operations with strings). - etki
  • It also depends on the server and on the version of PHP. Here is the result of this script on my personal server, I have PHP 5.4.23 version :) float (0.041002035140991) float (0.20201182365417) float (0.18901014328003) float (0.16100978851318) float (0.14800810813904) - Nik555