0

Hi there, since recently i have been trying to master HTML, and now i am full ready to take on PHP therefore, just like HTML instead of making many .html files | i want to use the PHP dynamic page function that can show me pages from database. usually as you can see in the URL id=?dsdf ( something like that )

Just one simple example about what to do in SQL and the php page will be perfect

Thanks

3
Contributors
7
Replies
9
Views
6 Years
Discussion Span
Last Post by Clanstrom
0

In your page you put the following code:

<?php

if(isset($_GET['p']) && !empty($_GET['p'])){
 $p = $_GET['p'];
 //please put mysql queries and most of the code dealing with mysql in a separate page unlike this one
 $query = "SELECT `content` FROM `your_table` WHERE `id`='$p'";
 if (@$mysql_query = mysql_query($query)){
  if (mysql_num_rows($mysql_query)==1){
   $content = mysql_result($mysql_query, 0, 'content');
  }
 }
}else{
 $p=1;
}

?>

<html>
 <p><?php echo $mysql_result; ?></p>
</html>

This is a simple but insecure way to display page content according to the id of the page. You can make a table and store all the id and content and other stuff.

If you wanna make it secure, please put all the queries and everything dealing with mysql in a separate page and require or include it in your main page.

Edited by Nahiyan: n/a

0

Let say you have a table for your blog (with id, title and post fields):

<?php
	$id = $_GET['id'];
	if(ctype_digit($id))
	{
		$query = mysql_query("SELECT * FROM blog WHERE id = '$id' limit 1");
		if(mysql_num_rows($query) == 1)
		{
			while($row = mysql_fetch_assoc($query))
			{
				echo $row['title'];
				echo $row['post'];
				echo $row['id'];
			}	
		}
		else
		{
			echo 'not found';
		}
	}
	else
	{
		echo 'not valid';
	}
?>

Check these links for more information:
http://www.php.net/manual/en/function.mysql-fetch-assoc.php
http://www.php.net/manual/en/function.ctype-digit.php

1

In your page you put the following code:

<?php

if(isset($_GET['p']) && !empty($_GET['p'])){
 $p = $_GET['p'];
 //please put mysql queries and most of the code dealing with mysql in a separate page unlike this one
 $query = "SELECT `content` FROM `your_table` WHERE `id`='$p'";
 if (@$mysql_query = mysql_query($query)){
  if (mysql_num_rows($mysql_query)==1){
   $content = mysql_result($mysql_query, 0, 'content');
  }
 }
}else{
 $p=1;
}

?>

<html>
 <p><?php echo $mysql_result; ?></p>
</html>

This is a simple but insecure way to display page content according to the id of the page. You can make a table and store all the id and content and other stuff.

If you wanna make it secure, please put all the queries and everything dealing with mysql in a separate page and require or include it in your main page.

Sorry, I didn't saw you have already posted a solution. I opened the thread and went away for a bit.. ^_^'

0

Thanks guys, but where is the link ( href ) ? . I mean i am suppose to send variables through the GET Fuction, but that can only be achieved when i click a link like ( Home, About us, contact. ... )

So, where do i integrate those links and what will be the relationship with the above code

1

You can simply make links with address like : yourpage.php?p=3

According to my code if your users visit yourpage.php, the content which has id of '1' will be displayed.

You obviously need a mysql table to get this code to work, your table may look something like this:

id    content    title     name
1     hey guys   my title  my site name
2     hey guys1  my title  my site name
3     hey guys2  my title  my site name

So if you visit "yourwebsite/yourpage.php" then you will find the text 'hey guys' on the page
If you visit "yourwebsite/yourpage.php?p=2" then you will find the text 'hey guys1' on the page

Edited by Nahiyan: n/a

Votes + Comments
0

Thank you so much, i am now trying it. I will post here if i encounter any errors. Although i am sure i won't.

thanks

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.