help to make a request which can determine where a certain news is, for example, there are for example 20 news (not in order), how to determine where news 17 is located?

Closed due to the fact that the essence of the question is not clear to the participants Visman , Alexey Shimansky , insolor , Kromster , Qwertiy 21 Aug '17 at 12:20 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • four
    At the 17th place you have it :) - Visman
  • What do you mean by "where is a certain news"? Do you want to know the number of the record in the database table or the rating of this news on the site? Plus it would not be bad to see the structure of the table where the news is stored. - Dmitriy Kondratiuk

1 answer 1

/* Выполнение SQL query */ $query = "SELECT * FROM my_table"; $result = mysql_query($query) or die("Query failed"); 

then we get the result of the query in the array containing the numeric index of the fields

 $line = mysql_fetch_array($result, MYSQL_NUM); 

in the mysql_fetch_array() function, the second parameter is the MYSQL_NUM MYSQL_NUM at which the columns are returned in the array containing the numeric field index. This index starts at 0, the first result field

we find in the $line array your news 17, we take the key, this will be its ordinal number in the database.

PS Do not forget that the counting starts from scratch.

  • and how to do the same procedure in the case of PDO? - privetsh
  • @privetsh needs to call the function PDOStatement :: fetch () in a loop (because it returns one result from the selection set), and the second parameter is to pass the FETCH_NUM flag to the function description here php.net/manual/ru/pdostatement.fetch.php - Andriy Poplavskiy
  • thank you very much - privetsh