fetch_row...mysql_result question

A

Anonymous

Guest
How would I get this code to automatically show the rows in the table without having to write the '0' and '1' etc. id's. I want to have a template that automatically adds the newest id without me having to update the script with the new id.

I had something working with a script like this...

Code:
$result = mysql_query("SELECT * FROM entries")
while ($info = mysql_num_row(blah blah blah));
foreach($info as $field);
print $field;

That isn't the exact code but it worked. The only problem is that it would print the id field and there was no way that I could format the text for the different columns.

Here is the code for entries.php which I'm working on...

Code:
<?
require("mysql_connect.php");
?>
<style type="text/css"> 

</style>
<body topmargin="0">

<table border="0" bordercolor="#ffffff" width="500" cellspacing="6" cellpadding="6" bgcolor="#dddddd" align="center" background="body_bg.jpg">
<tr>
<td bgcolor=#e7e7e7>
<?

$result = mysql_query("SELECT * FROM entries");

echo "<font size=4 style=\"line-height:6pt;\">";
printf(mysql_result($result,0,"title"));
echo "</font><br>&nbsp&nbsp&nbsp&nbsp<font size=0>";
printf(mysql_result($result,0,"date"));
echo "</font></td></tr><tr><td>";
printf(mysql_result($result,0,"entry"));
echo "</td></tr><tr><td bgcolor=#e7e7e7>";
echo "<font size=0><p align=right>";
printf(mysql_result($result,0,"post"));
echo "</tr></td>";


echo "<tr><td bgcolor=#e7e7e7>";
echo "<font size=4 style=\"line-height:6pt;\">";
printf(mysql_result($result,1,"title"));
echo "</font><br>&nbsp&nbsp&nbsp&nbsp<font size=0>";
printf(mysql_result($result,1,"date"));
echo "</font></td></tr><tr><td>";
printf(mysql_result($result,1,"entry"));
echo "</td></tr><tr><td bgcolor=#e7e7e7>";
echo "<font size=0><p align=right>";
printf(mysql_result($result,1,"post"));
echo "</tr></td><tr><td><br>";
?>
</tr></td></table>

Thanks in advance.
 
dmoney said:
How would I get this code to automatically show the rows in the table without having to write the '0' and '1' etc. id's. I want to have a template that automatically adds the newest id without me having to update the script with the new id.

I'm kind of baffled by your question -- it seems, rather, like you're asking two distinct questions.

If you don't want a particular column to be part of your query, just don't use it in your query (as a corollary to that, it's bad form to use "SELECT *" -- be explicit in which columns you select).

Now I don't know what you mean by "automatically adds the newest id". What do you mean by "adds"? What's adding what, where? When you say "add", I just think of INSERT, and I don't see any of those in your code. Could you perhaps repharse your question?

At any rate, your code is pretty crufty. For a better way to do a query, check out the first example on [http://www.php.net/manual/en/function.mysql-fetch-assoc.php]this page[/url].

You'll see that it skips the mysql_num_rows() kludge and uses mysql_fetch_assoc() so that you can retrieve columns by name, not by number.
 
thanks! fetch_array was what i needed. but i have another problem. how do i reverse order the arrays?
 
maybe:

"SELECT * FROM entries ORDER BY id DESC"

I think it will work...
 
That´s right! DESC will give you a descending order, the opposite is ASC... you know now what it does :p
See more in: Sorting Rows and Order By Optimisation

Anyway, for dmoney:
What swirlee wanted to say with:
If you don't want a particular column to be part of your query, just don't use it in your query (as a corollary to that, it's bad form to use "SELECT *" -- be explicit in which columns you select).
was:
Code:
SELECT col1,col2,col3 FROM entries
See more: SELECT Syntax
 
Back
Top