Problem running some code

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

Post Reply
losingMYmind
Forum Newbie
Posts: 3
Joined: Fri Jun 08, 2007 8:37 am
Location: Boston, MA

Problem running some code

Post 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);
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post 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)
losingMYmind
Forum Newbie
Posts: 3
Joined: Fri Jun 08, 2007 8:37 am
Location: Boston, MA

Post 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.
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post 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?
losingMYmind
Forum Newbie
Posts: 3
Joined: Fri Jun 08, 2007 8:37 am
Location: Boston, MA

Post by losingMYmind »

Thanks Blackbeard, that fixed it! much appreciation.
Post Reply