comment pls on how i take data from the database..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shotokan99

    comment pls on how i take data from the database..

    as of now im using this approach to take data:
    =============== =============== =======
    require('conn.p hp');
    $q='select * from '.$dbf_vid.' order by xrank';
    $res=mysql_quer y($q) or die('error: cannot perform query on new');
    $num=mysql_numr ows($res);
    $i=0;

    while($i<$num){
    $id=mysql_resul t($res,$i,'xid' );
    $rank=mysql_res ult($res,$i,'xr ank');
    $title=mysql_re sult($res,$i,'x title');
    $artist=mysql_r esult($res,$i,' xartist');
    $taglink=mysql_ result($res,$i, 'xtaglink');
    $img=mysql_resu lt($res,$i,'xim g');
    $ref=mysql_resu lt($res,$i,'xre f');
    $swf=mysql_resu lt($res,$i,'xsw f');

    if($i%2==0){
    $color='#BCBCDE ';
    $i++;
    }else{
    $color='#FFFFFF ';
    $i++;
    }
    echo'<tr>';
    echo' <td bgcolor="'.$col or.'" width="33"><fon t face="Verdana"
    size="1">'.$id. '</font></td>';
    echo' <td bgcolor="'.$col or.'" width="28"><fon t face="Verdana"
    size="1">'.$ran k.'</font></td>';
    echo' <td bgcolor="'.$col or.'" width="183"><fo nt face="Verdana"
    size="1">'.$tit le.'</font></td>';
    echo' <td bgcolor="'.$col or.'" width="213"><fo nt face="Verdana"
    size="1">'.$art ist.'</font></td>';
    echo' <td bgcolor="'.$col or.'" width="110"><fo nt face="Verdana"
    size="1">'.$ref .'</font></td>';
    echo' <td bgcolor="'.$col or.'" width="54"><fon t face="Verdana"
    size="1">
    <a href="mtvdel.ph p?id='.$id.'&db f=mtv_topvid_ph ">delete</
    a></font></td>';
    echo' <td bgcolor="'.$col or.'" width="53"><fon t face="Verdana"
    size="1"><a href="mtvedit.p hp?
    id='.$id.'
    &rank='.$ran k.'
    &title='.$title .'
    &by='.$artis t.'
    &taglink='.$tag link.'
    &ref='.$ref. '
    &img='.$img. '
    &swf='.$swf. '
    ">edit</a></font></td>';
    echo' </tr>';
    }//end while

    =============== =============== ====
    this works fine with me, but is there much better approach compared to
    what im doing?

    tnx

  • Schraalhans Keukenmeester

    #2
    Re: comment pls on how i take data from the database..

    At Mon, 21 May 2007 19:32:41 -0700, shotokan99 let his monkeys type:
    as of now im using this approach to take data:
    =============== =============== =======
    require('conn.p hp');
    $q='select * from '.$dbf_vid.' order by xrank';
    $res=mysql_quer y($q) or die('error: cannot perform query on new');
    $num=mysql_numr ows($res);
    $i=0;
    >
    while($i<$num){
    $id=mysql_resul t($res,$i,'xid' );
    $rank=mysql_res ult($res,$i,'xr ank');
    $title=mysql_re sult($res,$i,'x title');
    $artist=mysql_r esult($res,$i,' xartist');
    $taglink=mysql_ result($res,$i, 'xtaglink');
    $img=mysql_resu lt($res,$i,'xim g');
    $ref=mysql_resu lt($res,$i,'xre f');
    $swf=mysql_resu lt($res,$i,'xsw f');
    >
    if($i%2==0){
    $color='#BCBCDE ';
    $i++;
    }else{
    $color='#FFFFFF ';
    $i++;
    }
    echo'<tr>';
    echo' <td bgcolor="'.$col or.'" width="33"><fon t face="Verdana"
    size="1">'.$id. '</font></td>';
    echo' <td bgcolor="'.$col or.'" width="28"><fon t face="Verdana"
    size="1">'.$ran k.'</font></td>';
    echo' <td bgcolor="'.$col or.'" width="183"><fo nt face="Verdana"
    size="1">'.$tit le.'</font></td>';
    echo' <td bgcolor="'.$col or.'" width="213"><fo nt face="Verdana"
    size="1">'.$art ist.'</font></td>';
    echo' <td bgcolor="'.$col or.'" width="110"><fo nt face="Verdana"
    size="1">'.$ref .'</font></td>';
    echo' <td bgcolor="'.$col or.'" width="54"><fon t face="Verdana"
    size="1">
    <a href="mtvdel.ph p?id='.$id.'&db f=mtv_topvid_ph ">delete</
    a></font></td>';
    echo' <td bgcolor="'.$col or.'" width="53"><fon t face="Verdana"
    size="1"><a href="mtvedit.p hp?
    id='.$id.'
    &rank='.$ran k.'
    &title='.$title .'
    &by='.$artis t.'
    &taglink='.$tag link.'
    &ref='.$ref. '
    &img='.$img. '
    &swf='.$swf. '
    ">edit</a></font></td>';
    echo' </tr>';
    }//end while
    >
    =============== =============== ====
    this works fine with me, but is there much better approach compared to
    what im doing?
    >
    tnx
    [...]
    if (mysql_numrows( $res) === 0) {
    // do_something when no rows found
    // and exit
    }
    while ($row = mysql_fetch_ass oc($res)) {
    // your html here, replace your var names with
    // $row['fieldname'] (e.g. $row['title'])
    }

    All the quotes can make things confusing. Consider using heredoc syntax
    instead, then you only have to worry about quotes you actually need in the
    html.

    echo <<<HTML_END
    <td bgcolor="$row['color']" width="33">
    <font face="Verdana" size="1">$row['id']</font>
    </td>';
    (etc etc)
    HTML_END; // no spaces before this line, just the heredoc endtag and a ;


    HTH
    Sh.


    Comment

    • Toby A Inkster

      #3
      Re: comment pls on how i take data from the database..

      shotokan99 wrote:
      this works fine with me, but is there much better approach compared to
      what im doing?
      Yes.

      <?php
      require('conn.p hp');
      $q="SELECT xid,xrank,xtitl e,xartist,xref, xtaglink,ximg,x swf
      FROM $dbf_vid
      ORDER BY xrank;";
      $res=mysql_quer y($q) or die('error: cannot perform query on new');
      $i=0;

      $shown_fields = array('id', 'rank', 'title', 'artist', 'ref');

      while($data = mysql_fetch_ass oc($res))
      {
      $class = (++$i%2==0) ? 'even' : 'odd';

      printf('<tr class="%s">', htmlentities($c lass));

      foreach ($shown_fields as $f)
      printf('<td class="%s">%s</td>', $f, htmlentities($d ata['x'.$f]));

      printf('<td class="delete"> <a href="mtvdel.ph p?id=%s&amp;dbf =mtv_topvid_ph" >delete</a></td>',
      htmlentities(ur lencode($data['xid'])));

      $editurl = sprintf('id=%s& rank=%s&title=% s&by=%s&taglink =%s&ref=%s&img= %s&swf=%s',
      urlencode($data['xid']),
      urlencode($data['xrank']),
      urlencode($data['xtitle']),
      urlencode($data['xartist']),
      urlencode($data['xtaglink']),
      urlencode($data['xref']),
      urlencode($data['ximg']),
      urlencode($data['xswf']));
      printf('<td class="edit"><a href="%s">edit</a></td>', htmlentities($e diturl));

      echo "</tr>\n";
      }
      ?>

      Advantages:

      * You don't specify which columns exist in your database, so you
      may be fetching other columns that you don't need. Hence I've
      specified the exact columns to select.

      * I've removed all the colours, fonts and so on from the output,
      replacing them with class="odd" and class="even" attributes on the
      table cells, which can be styled appropriately via CSS.

      * I've not specified any widths -- browsers are generally pretty
      good at guessing widths themselves. I've added classes to the
      table cells so that you can use CSS to specify widths if required.

      * I've made sure that all output is properly urlencoded and special
      HTML characters are escaped. A mixture of printf(), htmlentities()
      and urlencode() makes this fairly easy and very clear.

      * I've output the first five columns using a foreach loop, which
      reduces repetition in your code.

      * I've used mysql_fetch_ass oc() which is significantly faster than
      repeated calls to mysql_result(). This also eliminates the need
      to call mysql_numrows() entirely.

      * I've added a line break after "</tr>" to make the resulting HTML
      slightly easier to read.

      --
      Toby A Inkster BSc (Hons) ARCS
      Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

      Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux

      Comment

      Working...