473,382 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

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

while($i<$num){
$id=mysql_result($res,$i,'xid');
$rank=mysql_result($res,$i,'xrank');
$title=mysql_result($res,$i,'xtitle');
$artist=mysql_result($res,$i,'xartist');
$taglink=mysql_result($res,$i,'xtaglink');
$img=mysql_result($res,$i,'ximg');
$ref=mysql_result($res,$i,'xref');
$swf=mysql_result($res,$i,'xswf');

if($i%2==0){
$color='#BCBCDE';
$i++;
}else{
$color='#FFFFFF';
$i++;
}
echo'<tr>';
echo' <td bgcolor="'.$color.'" width="33"><font face="Verdana"
size="1">'.$id.'</font></td>';
echo' <td bgcolor="'.$color.'" width="28"><font face="Verdana"
size="1">'.$rank.'</font></td>';
echo' <td bgcolor="'.$color.'" width="183"><font face="Verdana"
size="1">'.$title.'</font></td>';
echo' <td bgcolor="'.$color.'" width="213"><font face="Verdana"
size="1">'.$artist.'</font></td>';
echo' <td bgcolor="'.$color.'" width="110"><font face="Verdana"
size="1">'.$ref.'</font></td>';
echo' <td bgcolor="'.$color.'" width="54"><font face="Verdana"
size="1">
<a href="mtvdel.php?id='.$id.'&dbf=mtv_topvid_ph">del ete</
a></font></td>';
echo' <td bgcolor="'.$color.'" width="53"><font face="Verdana"
size="1"><a href="mtvedit.php?
id='.$id.'
&rank='.$rank.'
&title='.$title.'
&by='.$artist.'
&taglink='.$taglink.'
&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

May 22 '07 #1
2 1475
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.php');
$q='select * from '.$dbf_vid.' order by xrank';
$res=mysql_query($q) or die('error: cannot perform query on new');
$num=mysql_numrows($res);
$i=0;

while($i<$num){
$id=mysql_result($res,$i,'xid');
$rank=mysql_result($res,$i,'xrank');
$title=mysql_result($res,$i,'xtitle');
$artist=mysql_result($res,$i,'xartist');
$taglink=mysql_result($res,$i,'xtaglink');
$img=mysql_result($res,$i,'ximg');
$ref=mysql_result($res,$i,'xref');
$swf=mysql_result($res,$i,'xswf');

if($i%2==0){
$color='#BCBCDE';
$i++;
}else{
$color='#FFFFFF';
$i++;
}
echo'<tr>';
echo' <td bgcolor="'.$color.'" width="33"><font face="Verdana"
size="1">'.$id.'</font></td>';
echo' <td bgcolor="'.$color.'" width="28"><font face="Verdana"
size="1">'.$rank.'</font></td>';
echo' <td bgcolor="'.$color.'" width="183"><font face="Verdana"
size="1">'.$title.'</font></td>';
echo' <td bgcolor="'.$color.'" width="213"><font face="Verdana"
size="1">'.$artist.'</font></td>';
echo' <td bgcolor="'.$color.'" width="110"><font face="Verdana"
size="1">'.$ref.'</font></td>';
echo' <td bgcolor="'.$color.'" width="54"><font face="Verdana"
size="1">
<a href="mtvdel.php?id='.$id.'&dbf=mtv_topvid_ph">del ete</
a></font></td>';
echo' <td bgcolor="'.$color.'" width="53"><font face="Verdana"
size="1"><a href="mtvedit.php?
id='.$id.'
&rank='.$rank.'
&title='.$title.'
&by='.$artist.'
&taglink='.$taglink.'
&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_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
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.
May 22 '07 #2
shotokan99 wrote:
this works fine with me, but is there much better approach compared to
what im doing?
Yes.

<?php
require('conn.php');
$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');
$i=0;

$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">dele te</a></td>',
htmlentities(urlencode($data['xid'])));

$editurl = sprintf('id=%s&rank=%s&title=%s&by=%s&taglink=%s&r ef=%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($editurl));

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_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
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
May 22 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Alexandre | last post by:
Hi, Im a newb to dev and python... my first sefl assigned mission was to read a pickled file containing a list with DB like data and convert this to MySQL... So i wrote my first module which...
13
by: Christoph Brunner | last post by:
Hi, on the sun homepage i had submit to the bugparade a request for feature enhancement for the JAXB API. After a period of time sun called me to post my request to a newsgroup an get comments...
2
by: garyusenet | last post by:
I could do with something similiar, can you tell me if you think this would work for me, and if there's any advantage in working with controls this way than how I currently am. At the moment...
6
by: Rene Pijlman | last post by:
The code below results in an exception (Python 2.4.2): HTMLParser.HTMLParseError: bad end tag: "</foo' + 'bar>", at line 4, column 6 Should it? The end tag it chokes on is in comment, isn't...
9
by: Harold Crump | last post by:
Greetings, I have a fairly vanilla PHP web application that stores and retrieves data in a MySQL database. Users will be adding a lot of special characters such as single and double quotes,...
6
by: patrick j | last post by:
Hi I've been testing out the "Conditional Comment" for IE. This is because for my web-site I want to have two style sheets, one for IE 6 and one for other browsers. Thus I hope to have my...
15
by: linq936 | last post by:
Hi, I am reading book <<Expert C Programming>>, it has the following quiz, a //* //*/ b In C and C++ compiler what does the above code trun out? I think it is simple for C compiler, it is...
4
by: zach | last post by:
I know how to take data from a form and insert it into MySql. For a comment form on something like a blog or news article, is using addslashes() all that is needed to prevent unwanted malicious...
5
helimeef
by: helimeef | last post by:
I designed a little PHP/MySQL forum (just for fun), and I have commenting set up and top threads on the homepage and whatnot. I'm relatively new to PHP, and this is the biggest project I've done, but...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.