Just started learning php (right in practice). I ask for your help. I need to display the name of the news, which is equal to the news id. News text, which is equal to news id. And you need to display the date of the news, which is equal to the news id. But, unfortunately, I can not figure out how to do it. Help, please, I do not have enough knowledge.

<?php // Новости $get_rows_news = $mysqli->query("SELECT id FROM news"); $rows_news = mysqli_num_rows($get_rows_news); $get_date_news = $mysqli->query("SELECT 'date' FROM news"); $get_text_news = $mysqli->query("SELECT 'text' FROM news"); $get_name_news = $mysqli->query("SELECT 'name' FROM news"); for($i=0; $i < $rows_news ; ++$i){ ?> <div class="smart-container"> <div class="titlenews"> <?php echo "Название новости" ?> <span class="newsdate"> <?php echo "Дата новости"; ?> </span> </div> <div class="textnews"> <p>Текст новости</p> </div> </div> <?php } ?> 
  • Then start by learning sql. it can do much more than get one column from the entire table. try to do everything for a minimum of queries to the database (SQL generally allows you to get any data, no matter how complex or in how many tables are located, in one query). In your case, you need something like select id, date, text, name from news where id=? , bind using php.net/manual/ru/mysqli-stmt.bind-param.php the desired id value and execute the query. He will immediately return all the required fields from one single record with the specified id - Mike

1 answer 1

 <?php $result = mysql_query("SELECT * FROM news WHERE 1"); //если нужна новость с идентификатором 10, то раскомментировать следующую строку, а предыдущую убрать // $result = mysql_query("SELECT * FROM news WHERE id='10'"); if (mysql_num_rows($result) != 0) { while ($data = mysql_fetch_assoc($result)) { $name = $data["name"]; $date = $data["date"]; $text = $data["text"]; ?> <div class="smart-container"> <div class="titlenews"> <?php echo $name; ?> <span class="newsdate"> <?php echo $date; ?> </span> </div> <div class="textnews"> <p><?php echo $text; ?></p> </div> </div> <?php } } ?> 
  • Bd hooked up, the output is empty (Even the loop was not executed - flappypappy
  • Thank you, you helped. Only I do not have mysql, but mysqli Yesterday 4 hours because of this I sat = \ - flappypappy
  • Yes, did not pay attention that mysqli. I do not think that something will be very different. - Alien Guest