What I am upto: I am tryin' to develop a Blog CMS. There is a Database named blog in which there's a table named posts. The posts table has these fields: post_id, post_title, post_url, post_time, post_category, post_desc, post_username. Here, post_username is the username of the user who posted this post. Now, What I want to do is: I want to Generate a list of posts submitted by a user on his profile page using PHP and MySQL. And the Question: How Should I do it?
There must be 10 posts on a page with post_title, post_category and post_desc.

Recommended Answers

All 5 Replies

Start here. If you need help with issues, post your code, and explain where the error is.

Heya! I have already started it. Here is my code:

<?php
// Before this, I have connected to the database (succesful) and has selected the database too.
        $checkrow_query = "SELECT * FROM posts WHERE username = '$prof_real_username'";
	$checkrow = mysql_query($checkrow_query);
	$checkrows = mysql_num_rows($checkrow);
	if(!$checkrows)
	{
		echo "<div class=special\">This User Hasn't Submitted any links.</div>";
	}
	else
	{
		$query1 = "SELECT post_title FROM posts WHERE username = '$prof_real_username' ORDER BY post_time";
			$result1 = mysql_query($query1);
			$post_title = mysql_fetch_array($result1);

			$query2 = "SELECT post_url FROM posts WHERE username = '$prof_real_username' ORDER BY post_time";
			$result2 = mysql_query($query2);
			$post_url = mysql_fetch_array($result2);

			$query3 = "SELECT post_cat FROM posts WHERE username = '$prof_real_username' ORDER BY post_time";
			$result3 = mysql_query($query3);
			$post_cat = mysql_fetch_array($result3);

			$query4 = "SELECT post_desc FROM posts WHERE username = '$prof_real_username' ORDER BY post_time";
			$result4 = mysql_query($query4);
			$post_desc = mysql_fetch_array($result4);

			// Start Loading
			$times = 0;
			while($times <= 10)
			{
				echo "<div class=\"userscoop\"><h3><a href=\"" . 
				$post_url['$times']. 
				"\">"
				. $post_title['$times'] .
				"</a></h3><p>" . $post_desc['$times'] . "</p>
				<span class=\"time\">" . $post_time['$times'] . "</span>
				</div>";
				$times++;
			} // User Scoop Loading Ends
	}

?>

But when I execute it, nothing generates! The Source of the HTML contains only the div, blank h3 and black p. No Error, no nothing. So what's the problem? Thanks in Advance!

Well, I don't know about your table data, but if you want to show 10 links for a user, your logic is wrong. mysql_fetch_array only returns a single record, not all of them, so $post_url[$times] is empty.

So, How should I get all the data in the specified field? Here, I want to fetch ten post_title and print them. What Should I do?

Here i have added 2 lines is begining to check error
I have commented if check=0 to see whether result comes or not
try following code

<?php
	error_reporting(E_ALL); 
	ini_set("display_errors", 1); 
// Before this, I have connected to the database (succesful) and has selected the database too.
        $checkrow_query = "SELECT * FROM posts WHERE username = '$prof_real_username' order by post_time desc limit 10";
	$checkrow = mysql_query($checkrow_query);
	$checkrows = mysql_num_rows($checkrow);
/*	if(!$checkrows)
	{
		echo "<div class=special\">This User Hasn't Submitted any links.</div>";
	}
	else*/
	{
	
			// Start Loading
			$times = 0;
			while($postrow = mysql_fetch_array($checkrow))
			{
				echo "<div class=\"userscoop\"><h3><a href=\"" . 
				$postrow['post_url']. 
				"\">"
				. $postrow['post_title'] .
				"</a></h3><p>" . $postrow['post_desc'] . "</p>
				<span class=\"time\">" . $postrow['post_time'] . "</span>
				</div>";
				$times++;
			} // User Scoop Loading Ends
	}

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.