7 Replies - 298 Views - Last Post: 27 April 2013 - 12:29 AM Rate Topic: -----

#1 seyidesh  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 51
  • Joined: 24-November 10

Mysql_fetch_assoc ERROR

Posted 26 April 2013 - 06:39 PM

Hello guys, I have issues with the code below, can you please explain to me why i could not call the user_name out of database.

<?php
$tid = $_GET['id'];
if(isset($_SESSION['signed_in'])){
echo'
|| <a href="index.php"> Forum </a>';

}else{
	echo '<a href="index.php">Forum </a>';
}
?>

</div>

<?php
$cid = $_GET['id'];
$tid = $_GET['tid'];

		
//first select the category based on $_GET['cat_id']
$sql = "SELECT
			*
		FROM
			topics
		WHERE
			 cat_id='".$cid."' AND topic_id='".$tid."' LIMIT 1";
			
$result = mysql_query($sql) or die (mysql_error());

if(!$result)
{
	echo 'The topic could not be displayed, please try again later.';
}
else
{
	if(mysql_num_rows($result) == 0)
	{
		echo 'This topic doesn&prime;t exist.';
	}
	else
	{
		while($row = mysql_fetch_assoc($result))
		{
			//display post data
			echo '<table  id="topic">
					<tr>
						<th colspan="2" id="post_title">' . $row['topic_title'] . '</th>
					</tr>';
		
			//fetch the posts from the database
			$sql2 = "SELECT
						posts.topic_id,
						posts.post_content,
						posts.post_date,
						posts.post_creator,
						users.user_id,
						users.user_name
					FROM
						posts
					LEFT JOIN
						users
					ON
						posts.post_creator = users.user_id
					WHERE cat_id='".$cid."' AND topic_id='".$tid."'";
						
			$res2 = mysql_query($sql2) or die (mysql_error());
			
			
			
			while($row2 = mysql_fetch_assoc($res2)){
				$creator =$row2['user_name'];
				echo '<tr id="topic_post">
							<td colspan ="2" ><div id="user-post">' . $creator . '<br/>' . gmdate("F j, Y, g:i a", strtotime($row2['post_date'])) . '</div>
							<div id="post-content"><div id="PostContentTopic"> ' . $row['topic_title'] . '<hr /></div>' . htmlentities(stripslashes($row2['post_content'])) . '<div id="PostContentDate"><hr />
							' . $row2['user_name'] . '<br/>' . gmdate("F j, Y, g:i a", strtotime($row2['post_date'])) . '
							</div></div></td>
							
						  </tr>';
				}


The codes is not throwing error but the user_name's result is blank while i was able to call other things from the DB.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Mysql_fetch_assoc ERROR

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3332
  • View blog
  • Posts: 11,285
  • Joined: 12-December 12

Re: Mysql_fetch_assoc ERROR

Posted 26 April 2013 - 06:58 PM

Does it retrieve other data, but just not the user_name? That is, does it retrieve some rows?

Use print_r($row2); within your loop and view the source for the page to read this information.

BTW The mysql library is deprecated, see my signature.

BTW You shouldn't put an HR in the middle of a table - use css-borders. (Perhaps this is causing the information not to display properly :))

It should also be possible (on a brief look) to perform one query to request all the information you need, rather than nesting a second query within a loop.

This post has been edited by andrewsw: 26 April 2013 - 06:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 seyidesh  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 51
  • Joined: 24-November 10

Re: Mysql_fetch_assoc ERROR

Posted 26 April 2013 - 07:13 PM

I have just done the print_r($rows2) But the user_name is stil not printing, see the result
Array ( [topic_id] => 37 [post_content] => How to Create a Forum in PHP from Scratch In this tutorial weíll see how to make a forum using PHP and MySQL. We have to cover a lot of different things, so letís start! Some details about this tutorial You can download a compressed folder with the whole project inside. So sometimes I wonít show all the code of a file in order to focus on the important parts of the project. The code Iíll show is exactly the same than the one you can download, except for some comments. In the original project youíll have everything well documented (using phpDocumentor). Iíll ommit the tags in the tutorial, but every time you type PHP code in a file you should put it between them. This project doesnít follow a MVC pattern, but weíll use classes and try to separate the different functionalities. What and how [post_date] => 2013-04-27 12:40:52 [post_creator] => 1 [user_id] => [user_name] => )


View Postandrewsw, on 26 April 2013 - 06:58 PM, said:

Does it retrieve other data, but just not the user_name? That is, does it retrieve some rows?

Use print_r($row2); within your loop and view the source for the page to read this information.

BTW The mysql library is deprecated, see my signature.

BTW You shouldn't put an HR in the middle of a table - use css-borders. (Perhaps this is causing the information not to display properly :)/>)

It should also be possible (on a brief look) to perform one query to request all the information you need, rather than nesting a second query within a loop.

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3332
  • View blog
  • Posts: 11,285
  • Joined: 12-December 12

Re: Mysql_fetch_assoc ERROR

Posted 26 April 2013 - 07:18 PM

$rows2 doesn't include a user_id either, which is more significant. Check the data that is held in the database. Is the post_creator the same as the user_id and are they both populated in the database.

This post has been edited by andrewsw: 26 April 2013 - 07:20 PM

Was This Post Helpful? 0
  • +
  • -

#5 seyidesh  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 51
  • Joined: 24-November 10

Re: Mysql_fetch_assoc ERROR

Posted 26 April 2013 - 07:32 PM

The value of the two are not the same in the database i.e the user_id and the post_creator, the value is 5 & 1 respectively.


View Postandrewsw, on 26 April 2013 - 07:18 PM, said:

$rows2 doesn't include a user_id either, which is more significant. Check the data that is held in the database. Is the post_creator the same as the user_id and are they both populated in the database.

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3332
  • View blog
  • Posts: 11,285
  • Joined: 12-December 12

Re: Mysql_fetch_assoc ERROR

Posted 26 April 2013 - 07:38 PM

So.. your join

ON posts.post_creator = users.user_id

won't work.
Was This Post Helpful? 0
  • +
  • -

#7 seyidesh  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 51
  • Joined: 24-November 10

Re: Mysql_fetch_assoc ERROR

Posted 26 April 2013 - 07:43 PM

Can you please give another method i can use to write that portion of the code, my head is blank right now. thanks

View Postandrewsw, on 26 April 2013 - 07:38 PM, said:

So.. your join

ON posts.post_creator = users.user_id

won't work.

Was This Post Helpful? 0
  • +
  • -

#8 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3716
  • View blog
  • Posts: 5,976
  • Joined: 08-June 10

Re: Mysql_fetch_assoc ERROR

Posted 27 April 2013 - 12:29 AM

It's not the code that is the problem, it's the data. If the user with the ID 5 created the topic, then why is the "post_creator" value 1? How is the code supposed to know what user to show as the creator if the database is storing the wrong user ID value as the creator ID?

I suggest you look into using proper Foreign Key constraints, so that the database will enforce links like that and refuse to insert invalid data. (Of course, if the invalid data is referencing a valid row, even though it's the wrong row, a FK won't fix that.)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1