Forum Moderators: open
Anyway, if each record has an ID thats a primary key, how do I get one record/article?
I know it's something like:
SELECT * FROM articles
but do I put the record id after (WHERE record_id = 1)?
Everywhere I searched just had all kinds of complex ways to display lots of table data in various ways but I just want one row at a time. Such a noob question but thank you in advance to anyone that can answer.
If this is to show the last five results for a news page or something similiar, you can also LIMIT the query, ex:
SELECT * FROM articles LIMIT 5
That would return the last 5 records ascending by default.
Thanks man, I thought that method was more of a quick fix than an official method. Since I'm all new to databases, I'm not up to speed on proper coding yet.
Incidentally, I have one more question - suppose the following crude example:
<?php
$query = 'SELECT * FROM articles WHERE 1';
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$title= $row[1];
$date= $row[2];
$link = $row[3];
$category= $row[4];
$categoryLink= $row[5];
$story = $row[6];
?>
<div class="example1">
<?php
echo $title;
echo $date;
echo $link;
echo $category;
echo $categoryLink;
echo $story;
?>
</div>
If I have, say, 10 records full of articles and related info, and I want to manually pick which to display - how would I reuse the above info to show the data? Like if I wanted articles 1, 3, 5, and 9?
(Thanks in advance)
while ($row = mysql_fetch_array($result)) {
printf("ID: %s Name: %s", $row[0], $row[1]);
}
(example stolen from php.net example in manual)
It looks something like:
$query = 'SELECT * FROM articles WHERE article_id = $a_id'
<code to display xhtml and query result>
the above code goes in a file called showarticle.php (the same will apply to news and reviews respectively). Then on the index page where I want to display certain articles/reviews/news, I would use the following code:
<?php
$a_id = 4;
include('showarticle.php');
?>
..and just change the a_id value for the article I want to display. The only problem with doing this is that I will need to know the ID number for every article I insert that I want on the homepage and I will have to manually enter the above code for each display:
<?php
$a_id = 4;
include('showarticle.php');
?>
<?php
$a_id = 2;
include('shownews.php');
?>
<?php
$a_id = 12;
include('showreview.php');
?>
<?php
$a_id = 20;
include('showarticle.php');
?>
I'm sure there is a way I could incorporate a check box on the form I built to enter the content that gets inserted into the database where if the check box is checked, the php code is automatcially inserted on the home page. I just have no idea how to do that yet.
For the record, I realize that the above method is rather tedious and that there is always a better way. I changed the database to consolidate all articles/news/reviews/etc and will have them sorted on the site using a category table instead. As for the select items I want displayed on the home page, I just added a story_index_page field with a single CHAR that has a default value of N. When I want a particular story displayed on the home page, I set it to Y. The query statement on the main page looks for all records with that Y and sorts them by date. So much easier.