Forum Moderators: coopster
I know it all involves somthing with mysql_query, and mysql_fetch_array, but I cannot understand the concept at all. I can understand how to select certain pieces of data, but no way in hell to reference it or call it to echo. I hope I'm making sense, but do you know how to reference a row when it could be different every time?
For the use of a scenario, I am calling three rows of data from a database, grouping it by the id number. I want to put the data in different places on an html page, (its for news, the title, author, news_text would all look alike, but the data would come after one another in kind of a template, and would repeat itself, but all on the same page.
Please help me understand this concept and how to group and call different rows of information, i'm at a standstill here and my mind just isn't getting the "logic" of it. Thx for reading this and your help also.
The sequence of events usually goes like this..
mysql_connect and mysql_select_db) mysql_query) while and mysql_fetch_assoc or mysql_fetch_array) This excellent webMonkey article on PHP/MySQL [hotwired.lycos.com] should get you started.
[pre]
mysql_connect( 'whatever' );
or die( "Error connecting: ".mysql_error());
mysql_select_db('whatever');
or die( "Error selecting database: ".mysql_error());
$sql = 'select title, author, news_text from news where id = 1';
$result = mysql_query($sql)
or die( "Error selecting news: ".mysql_error());
while ( $row = mysql_fetch_assoc($result) ) {
print '<h1>'.$row['title'].'</h1>';
print '<p>'.$row['news_text'].'</p>';
print '<span class="byline">Written by '.$row['author'].'</span>';
}
[/pre]
Hope that helps.
As of now I understand how to select data from 3 different rows, but i'm only able to echo the data one after another. ~<Dangit!>~.
That code will display all the results. If 10 rows have the id of 1 then it will display a page of 10 results, one after the other.
(By the way, 'id' is a name that usually given to a field that uniquely identifies one and only one row in the table, so you might want to rename it).
Yeah I know that, I was trying to do an example, but heres my real example lol.
I have a database called television_news which has various amounts of data about television news such as news_id (primarykey), title, newstext, and posted user.
I have a design of how I want the information for that to show up but since this is the front page of my site, I only want to display the newest 3 news articles entered (being the 3 highest news_ids are the 3 most recent).
If I design how I want one to look, is there a way to make the other three go into that way also? Picture it as *title* at the top, under it is *posted_user*, and under that is *newstext*. It would pull each row of information that matches the criteria (newest 3 ids) and put the information for EACH row into that template displaying each after another (just like a post).
I hope I said that sufficiently and thx for helping, its so confusing.
$sql = 'select * from news ORDER BY id DESC limit 3
';
That solves that which I already knew how to solve, but can you give me a sample of the
for(i = 0; i< 3; i++)
{
echo "...."
}
situation, I'm not completely sure what it means.
while($row = mysql_fetch_array())
{
echo "<P><B>$row[title]</B> - $row[newstext]<BR> - $row[posted_user]</P>";
}
This will format it as paragraphs with a bold title. You could as easily use <H> tags or fit it in a table, depending on your needs.
WBF