<? mysql_connect('localhost', 'root', ''); mysql_select_db('blog'); $count = mysql_query("SELECT * FROM `question_categories`"); $query = mysql_query("SELECT * FROM `question_categories`"); $categorie = mysql_fetch_assoc($query); $count = mysql_num_rows($count); ?> <form action="<? $_SERVER['PHP_SELF'] ?>" method="POST"> <input type="text" placeholder="Как выучить php быстрее?" name="question_title"> <textarea type="text" placeholder="Когда я создавал сайт я столкнулся с такой проблемой..."></textarea> <select> <? for($i = 0; $i <= $count; $i++) { echo("<section>" . $categorie['title'] . "</section>"); } ?> </select> </form> 

Category output does not work

  • What do the logs say? - Kosta B.
  • Nothing, just no categories - MrAndy

1 answer 1

Make sure that the result of the SELECT * FROM question_categories query is not empty. To count the number of rows by the mysql_num_rows () function, you do not need to send another query to the database. This code

 $count = mysql_query("SELECT * FROM `question_categories`"); $query = mysql_query("SELECT * FROM `question_categories`"); $categorie = mysql_fetch_assoc($query); $count = mysql_num_rows($count); 

can be replaced by:

 $query = mysql_query("SELECT * FROM `question_categories`"); $count = mysql_num_rows($query); 

Output the query result in a while loop using mysql_fetch_assoc () :

 while ($categorie = mysql_fetch_assoc($query)) { echo "<section>" . $categorie['title'] . "</section>"; } 

These functions for working with mysql are already aging and are not recommended for use. Use the mysqli or PDO functions instead.