news script?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

news script?

Post by Joe »

I have been trying to create a news script for my page which reads records from a mySQL database. All is working well with the script but it only seems to read 3 out of 5 records. Can anyone please help?

Script

<?php
$link = mysql_connect("???", "???", "???");
mysql_select_db("???") or die("couldnt connect" . mysql_error());
$sql = "SELECT * FROM news ORDER BY ID";
$result = mysql_query($sql) or die(mysql_error());

if ($row = mysql_num_rows($result))
{
do
{
echo "<img src='news.gif' border=1>";
$row = mysql_fetch_assoc($result);
echo "<br><u><b>.:" . $row['headline'] . ":.</U></b> posted by " . $row['username'] . "<br>";
echo $row['mainnews']."<br>";
echo "<hr color='white'>";
}
while ($row = mysql_fetch_array($result));
}
else
{
echo "Error!<p>";
echo "<br>";
mysql_close($link);
}
mysql_close($link);
?>


Regards


Joe 8)
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

....

Code: Select all

if ($row = mysql_num_rows($result))
{
do
{
echo "<img src='news.gif' border=1>";
$row = mysql_fetch_assoc($result);
echo "<br><u><b>.:" . $row['headline'] . ":.</U></b> posted by " . $row['username'] . "<br>";
echo $row['mainnews']."<br>";
echo "<hr color='white'>";
}
while ($row = mysql_fetch_array($result));
to

Code: Select all

if (mysql_num_rows($result)>0){
  while ($row = mysql_fetch_array($result)){
    echo "<img src='news.gif' border=1>";
    echo "<br><u><b>.:" . $row['headline'] . ":.</U></b> posted by " . $row['username'] . "<br>";
    echo $row['mainnews']."<br>";
    echo "<hr color='white'>";
  }
}
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Re: news script?

Post by TheBentinel.com »

Joe wrote:

Code: Select all

if ($row = mysql_num_rows($result))

 while ($row = mysql_fetch_array($result));
Those two lines should be using "==" instead of "="

One equal sets the value, even in an if statement. Kinda kooky, but they claim there's a great "under the covers" reason for it.

See if changing that helps.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

The = in the while is ok, as you do want to assign at that point :o
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Re: news script?

Post by Illusionist »

TheBentinel.com wrote:
Joe wrote:

Code: Select all

if ($row = mysql_num_rows($result))

 while ($row = mysql_fetch_array($result));
Those two lines should be using "==" instead of "="

One equal sets the value, even in an if statement. Kinda kooky, but they claim there's a great "under the covers" reason for it.

See if changing that helps.
I dont agree with either of those suggestions. The first because eh is not setting $row to beign wiht so it owuld jump over that everytime... And second in the while, you want to assign $row like mark said
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Re: news script?

Post by TheBentinel.com »

Wow, you're making use of the "test and set at the same time" thing.

Ok, can somebody slowly walk me through what this code is doing? It sets $row twice in the same loop, which looks funky but I guess isn't. Somebody, please bring me up to speed here.

Thanks!

(Sorry, I don't mean to hijack the thread. Has your problem been resolved?)
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Hmm nothing seemed to work and if I put a double = symbol i just get a mySQL error. What could the problem be here!
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

show the code you've changed around... what are you using??
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

I posted the code i am using in this forum post and I changed the values around which were given to me by other forum members. All of which failed to work.

Regards
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

and i said show the code that you've changed around. Meaning the new code you've tried... Was that really that hard to understand??
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Re: news script?

Post by TheBentinel.com »

I think your code is throwing all every other record, so you're displaying records 1, 3, and 5.

Maybe try:

Code: Select all

<?php
 $link = mysql_connect("???", "???", "???");
 mysql_select_db("???") or die("couldnt connect" . mysql_error());
 $sql = "SELECT * FROM news ORDER BY ID"; 
 $result = mysql_query($sql) or die(mysql_error()); 

  while (true)
 {
  $row = mysql_fetch_assoc($result);
  if ($row == false) break;
  echo "<img src='news.gif' border=1>";
  echo "<br><u><b>.:" . $row['headline'] . ":.</U></b> posted by " . $row['username'] . "<br>";
  echo $row['mainnews']."<br>";
  echo "<hr color='white'>";
 }
Probably a cleaner way to do that loop, but I'm shooting for fetching the records in only one place.
Last edited by TheBentinel.com on Thu Mar 18, 2004 1:17 pm, edited 1 time in total.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

yeah, your right TheBentinel, and i have to figure out why he has the $row = mysql_feth_assoc int he middle of that loop... and thats why i suggested using this code, but i don't think he even looked at it....
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Yes Illusionist i did actually see that code and i tried it. The only problem is that it worked far worse than the original.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Paste your updated code. Looks to me like Illusionist's suggestion should work ok.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Hey thanks everyone. In the end up I got TheBentinel.com's script to work perfect. Thanks a lot for that!

Regards


Joe 8)
Post Reply