There is a php script that outputs articles from a database (in theory). But in practice, he displays only the first entry.

<?php @mysql_connect('localhost','admin','123') or die('WRONG CONNECTION('.mysql_error()); mysql_select_db(iskstore); $sql=mysql_query("SELECT * FROM `articles_db`"); $result=mysql_fetch_array($sql); $htmlformat = '<div class ="article-%s"><div class="a_title"><h2>%s</h2></div><div class="a_text">%s</div></div>'; echo sprintf($htmlformat, $result['a_id'], $result['a_title'], $result['a_text']); @mysql_close() ?> 

the table itself

I do not specifically understand PHP and there is a suspicion that it was necessary through fetch_assoc () but with it I generally get an error. How to make the script unload all the records?

  • one
    so as not to be soared with the array fetch_assoc really simpler ..... secondly, it is necessary to process it in a loop. and in the documentation there are quite examples of this associated with it ...... and thirdly: do not use the mysql extension - it is outdated ..... use mysqli or pdo ....... fourthly: do not use stubs in the form of @ otherwise the chief and the senior programmer will rape you - Alexey Shimansky

1 answer 1

Insert mysql_fetch_array into the loop

 @mysql_connect('localhost','admin','123') or die('WRONG CONNECTION('.mysql_error()); mysql_select_db(iskstore); $sql=mysql_query("SELECT * FROM `articles_db`"); while ($result=mysql_fetch_array($sql)) { $htmlformat = '<div class ="article-%s"><div class="a_title"><h2>%s</h2></div><div class="a_text">%s</div></div>'; echo sprintf($htmlformat, $result['a_id'], $result['a_title'], $result['a_text']); } @mysql_close()