Cooper94 Posted January 31, 2009 Share Posted January 31, 2009 Would this be the best way to post or pull the data from the database? <? include 'db.php' ?> <? $result = mysql_query("select * from news ORDER BY id DESC LIMIT 4"); $num = mysql_num_rows($result); $i =0; ?> <? while($i < $num) { $date = mysql_result($result,$i,"date"); $headline = mysql_result($result,$i,"headline"); $sum = mysql_result($result,$i,"sum"); $id = mysql_result($result,$i,"id"); ?> <p align="justify"><?echo $date;?> : Notam : <?echo $headline; ?><br> <?echo $sum; ?><br> <a href="news.php?id=<?echo $id;?>" style="text-decoration: none; border-bottom: 1px dotted; color: #006699;">Read More</a> <? $i++;} ?> Quote Link to comment https://forums.phpfreaks.com/topic/143257-best-way/ Share on other sites More sharing options...
wildteen88 Posted January 31, 2009 Share Posted January 31, 2009 You should try to avoid short tags altogether. It is always best to use the full PHP tags (<?php ?>) as short tags can be disabled. This way you you know your PHP scripts will run on any PHP enabled server. You should also try to avoid going in and out of PHP. This just slows the processing of the script down. Also I prefer to use mysql_fetch_assoc when retrieving data from a mysql query over mysql_result. <?php include 'db.php'; $result = mysql_query("select * from news ORDER BY id DESC LIMIT 4"); // check to see if any results where returned from the query if(mysql_num_rows($result) > 0) { // loop through the results while($row = mysql_fetch_assoc($result)) { $date = $row['date']; $headline = $row['headline']; $sum = $row['sum']; $id = $row['id']; echo <<<HTML <p align="justify"> $date : Notam : $headline<br> $sum<br> <a href="news.php?id=$id" style="text-decoration: none; border-bottom: 1px dotted; color: #006699;">Read More</a> </p> HTML; } } // no results returned from the query. else { echo '<p>No news available at this time</p>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/143257-best-way/#findComment-751281 Share on other sites More sharing options...
.josh Posted January 31, 2009 Share Posted January 31, 2009 Also I prefer to use mysql_fetch_assoc when retrieving data from a mysql query over mysql_result. To expand on that, unless you are pulling like 1 specific column of one specific row from your database, it is a lot faster to use a mysql_fetch_xxx function rather than mysql_result, because those functions will pull all of the columns of the row in one call, instead of doing one for each column. Quote Link to comment https://forums.phpfreaks.com/topic/143257-best-way/#findComment-751290 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.