Hello!

How can PHP output some information from the database to a page?

For example:

<div class="main"> <h1>Статьи</h1><br> <?php $connect = mysql_connect('localhost','root','12345'); mysql_select_db('algoritm_db',$connect); $sql="select * from articles"; $result=mysql_query($sql)or die("Query failed: " . mysql_error()); $i=0; while($row = mysql_fetch_array($result, MYSQL_NUM)) { print "<div class='zaglav'>".$row[1]."</div>"; $i=$i+1; } ?> </div> 

Ie I wanted to create several div blocks of the same class, but with different info .. but it does not work, displays the PHP code and everything .... Displays after print ...

What am I doing wrong?

Thank.

  • File with what extension? Hosting with php? - Sh4dow

4 answers 4

first, where ";" at the end of similar lines?

 print "<div class='zaglav'>$row[1]</div>" 

Yes, and it is desirable to allocate variables, here's how to do it:

 print "<div class='zaglav'>".$row[1]."</div>"; 

this also applies to the $ i variable that you output in a loop

in the rules of good tone, the request to the database is made to form in this form, so it is more easily perceived visually:

  $sql="SELECT * FROM `articles`"; 

as well, if there are no records in the database, this code will generate an SQL error (if the display of errors is enabled), so check the number of records before outputting from the database.

  • And also - if the interpreter worked , the code would not be output ... - Zowie
  • hmm, re-read his question, exactly, I thought he had an error code, although they obviously are there, I indicated them: D - Elime
  • about; I forgot my floor ... but it still displays the Php code ... it's a matter of the loop ... but something with print ... - Leshij_2005

In my opinion it is even easier!

 while($row = mysql_fetch_array($result, MYSQL_NUM)){ printf("<div class="zaglav">%s</div> <div class="article" id="article">%s<br>",$row['caption'],$row['stroka2']); } 

printf - it seems more convenient to me with such conclusions!

    As far as I remember the HTML markup, the built-in PHP script was usually output in echo:

     echo "<title>".$title."</title>"; 

    Or is it my habit from frameworks? :)

      I also try this:

       mysql_select_db('algoritm_db',$connect); $sql="SELECT * FROM articles"; $result=mysql_query($sql)or die("Query failed: " . mysql_error());; $i=0; if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; } else { while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { ?> <div class="zaglav"><?php print $row['caption']?></div> <div class="article" id="article<?php print $i?>"><br> <?php print $row['value']?> <br><br> <a href="#" class="btnAr" id="butAr<?php print $i?>">Скрыть</a> </div> <div class="preview" id="preview<?php print $i?>"> <br><?php print $row['value_preview']?> <br><br> <a href="#" class="btnAr" id="butAr<?php print $i?>">Подробнее...</a> <div> <?php $i=$i+1; } } mysql_free_result($result); ?> 

      Now it worked)) Hooray !!)

      Judging by the content of the site. The rules for the design of the program code should be thrown away to the first version at all !!! Learning from mistakes)

      Thanks to all!