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

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
as of now im using this approach to take data:
$q='select * from  '.$dbf_vid.' order by xrank';
$res=mysql_query($q) or die('error: cannot perform query on new');


 echo'     <td bgcolor="'.$color.'" width="33"><font face="Verdana"
 echo'     <td bgcolor="'.$color.'" width="28"><font face="Verdana"
 echo'     <td bgcolor="'.$color.'" width="183"><font face="Verdana"
 echo'      <td bgcolor="'.$color.'" width="213"><font face="Verdana"
 echo'      <td bgcolor="'.$color.'" width="110"><font face="Verdana"
 echo'      <td bgcolor="'.$color.'" width="54"><font face="Verdana"
            <a href="mtvdel.php?id='.$id.'&dbf=mtv_topvid_ph">delete</
 echo'      <td bgcolor="'.$color.'" width="53"><font face="Verdana"
size="1"><a href="mtvedit.php?
 echo'    </tr>';
}//end while

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


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:

Quoted text here. Click to load it

if (mysql_numrows($res) === 0) {
  // do_something when no rows found
  // and exit
while ($row = mysql_fetch_assoc($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

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


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

shotokan99 wrote:

Quoted text here. Click to load it


$q="SELECT xid,xrank,xtitle,xartist,xref,xtaglink,ximg,xswf
    FROM $dbf_vid
    ORDER BY xrank;";  
$res=mysql_query($q) or die('error: cannot perform query on new');

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

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

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

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

    printf('<td class="delete"><a href="mtvdel.php?id=%s&amp;dbf=mtv_topvid_ph">delete</a></td>',

    $editurl = sprintf('id=%s&rank=%s&title=%s&by=%s&taglink=%s&ref=%s&img=%s&swf=%s',
    printf('<td class="edit"><a href="%s">edit</a></td>', htmlentities($editurl));  

    echo "</tr>\n";


  * 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_assoc() 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
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux

Site Timeline