Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.