Page 1 of 1

Problem running some code

Posted: Fri Jun 08, 2007 8:42 am
by losingMYmind
I am new to PHP and I am having a small problem and I hope someone can help me with this. OK here is my problem and I am sure someone will see the wickedness of my bad coding.

When I run the code below it does return a result but not the one I want. Instead of returning the row with a status of 1 it is returning the row with the status of 0. What am I doing wrong?

Thanks in advance for any and all help on this.

Code: Select all

mysql_fetch_array($result);
while ($row = mysql_fetch_array($result)) {
if($row['status']=1) {
echo "<table>";
echo "<tr>";
echo "<td><h6>$row[subject]</h6>";
echo "<p class='small'>$row[date]</p>";
echo "<br /><p class='med'>$row[content]</p>";
echo "<br /><p class='small'>$row[author]</p></td>";
echo "</tr>";
echo "</table>";
}
}
mysql_close($con);

Posted: Fri Jun 08, 2007 8:55 am
by TheMoose
if($row['status']=1) is an assignment. It is setting the value of $row['status'] to 1, and then if it was able to do so, continues down into the IF statement. Use == (double equals) to check for a value: if($row['status'] == 1)

Posted: Fri Jun 08, 2007 10:35 am
by losingMYmind
Thanks ... for helping. I did what you said now when I run it nothing appears at all. Not even an error message.

Basically what I am trying to do is be able to have a "news" script that will only post active stories. In the data base there is a field for the status of each story. 1 being active and 0 being inactive. What I want is for the script to loop through the table and pull only the stories with a "1" in the status field.

Currently I have two test entries in the database. One with the status of "1" and the other with the status of "0" maybe I am being an idiot and the answer is right in front of my nose. BUt I can't see it.

Posted: Fri Jun 08, 2007 11:26 am
by blackbeard
Get rid of the first mysql_fetch_array, you're moving the pointer down one, and if there's only one record retrieved, you just passed it.

Also, are you checking that you're not getting an error message when you do the query?

Posted: Fri Jun 08, 2007 11:30 am
by losingMYmind
Thanks Blackbeard, that fixed it! much appreciation.