Jump to content

Best Way?


Cooper94

Recommended Posts

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++;} 
?>

Link to comment
https://forums.phpfreaks.com/topic/143257-best-way/
Share on other sites

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>';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/143257-best-way/#findComment-751281
Share on other sites

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. 

Link to comment
https://forums.phpfreaks.com/topic/143257-best-way/#findComment-751290
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.