Page 1 of 2
news script?
Posted: Thu Mar 18, 2004 12:30 pm
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

Posted: Thu Mar 18, 2004 12:45 pm
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'>";
}
}
Re: news script?
Posted: Thu Mar 18, 2004 12:46 pm
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.
Posted: Thu Mar 18, 2004 12:47 pm
by markl999
The = in the while is ok, as you do want to assign at that point

Re: news script?
Posted: Thu Mar 18, 2004 12:51 pm
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
Re: news script?
Posted: Thu Mar 18, 2004 1:00 pm
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?)
Posted: Thu Mar 18, 2004 1:03 pm
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!
Posted: Thu Mar 18, 2004 1:05 pm
by Illusionist
show the code you've changed around... what are you using??
Posted: Thu Mar 18, 2004 1:07 pm
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
Posted: Thu Mar 18, 2004 1:10 pm
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??
Re: news script?
Posted: Thu Mar 18, 2004 1:12 pm
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.
Posted: Thu Mar 18, 2004 1:17 pm
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....
Posted: Thu Mar 18, 2004 1:25 pm
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.
Posted: Thu Mar 18, 2004 1:28 pm
by markl999
Paste your updated code. Looks to me like Illusionist's suggestion should work ok.
Posted: Thu Mar 18, 2004 1:31 pm
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
