Echo not working after mysql_fetch_assoc line

Hey guys, I am trying to take mysql data and put it into a table on the page. Every thing was working, but then I changed something and it stopped working. The code is:

[php]<?php
include(‘cn.php’);
@session_start();

					$cid = $_GET['cid'];
					$tid = $_GET['tid'];
					echo $cid . " " . $tid;
					$sql = "SELECT * FROM topics WHERE category_id='".$cid."' AND id='".$tid."' LIMIT 1";
					$res = mysql_query($sql) or die(mysql_error());
					if (mysql_num_rows($res) == 1) {
						echo "<table width='100%'>";
						if ($_SESSION['loggedInUser']) { echo"<tr><td colspan='2'><input type='submit' value='Add Reply' onClick=\"window.location = 'post_reply.php?cid=".$cid."&tid=".$tid."'\" /><hr /></td></tr>"; } else { echo "<tr><td colspan='2'><p>Please login <a href='login.php'>here</a> to add your reply</p><hr />"; }
						while ($row = mysql_fetch_assoc($res)) {
							echo "while";
							$sql2 = "SELECT * FROM posts WHERE category_id='".$cid."' AND topic_id='".$tid."'";
							echo $sql2 ."\n";
							$res2 = mysql_query($sql2) or die(mysql_error());
							while ($row2 = mysql_fetch_assoc($res2)) {
								echo "while";
								$userName = $row2['post_creator'];
								$sql4 = "SELECT * FROM user WHERE user_name='$userName'";
								echo $sql4 . "\n";
								echo "res";
								$res4 = mysql_query($sql4) or die(mysql_error());
								echo "row";											//Last echo that runs
								$row4 = mysql_fetch_assoc($res4) or die(mysql_error()); 
								echo "img";
								$userImg = $row4['image'];
								echo $userImg;
								echo "info done";
							
								echo "<tr><td valign='top' style='border: 1px solid #000000;'><div style='min-height: 125px;'>".$row['topic_title']."<br /> by ".$row2['post_creator']." - ".$row2['post_date']."<hr />".$row2['post_content']."</div></td><td width='200' valign='top' align='center' style='border: 1px solid #000000;'><img src='user_pics/$userImg' height='100' width='100' /></td></tr><tr><td colspan='2'><hr /></td></tr>";
						}
							echo "</table>";
							$old_views = $row['topic_views'];
							$new_views = $old_views + 1;
							$sql3 = "UPDATE topics SET topic_views='".$new_views."' WHERE category_id='".$cid."' AND id='".$tid."' LIMIT 1";
							$res3 = mysql_query($sql3) or die(mysql_error());
						}
					} else {
						echo "<p>This topic does not exist.</p>";
					}
				?>[/php]

The part where I try to echo out “img” never runs… I have no clue why. Every other echo up to that point works. Does anyone see the problem? (Knowing my mistakes, its probably extremely simple :D) Thanks!

I don’t immediately see any true errors. I would try a couple of things:

First, I would change your $sql4 query to this:[php]$sql4 = “SELECT * FROM user WHERE user_name LIKE ‘$userName’”;[/php]

Second, I would add the following after your echo “row”; line:
[php]echo "Number of rows: ".mysql_num_rows($res4);[/php]

This will tell us if your query is actually returning anything.

Let me know how things look after trying this…

It says “Number of rows: 0”. So the query just isn’t finding anything?

Actually, I just figured it out… another page did not input the right data. Thank you for your help!

Glad you got it working, let us know if you have any other issues.

Sponsor our Newsletter | Privacy Policy | Terms of Service