Dren Posted October 22, 2013 Share Posted October 22, 2013 Hi, i'm trying to create a function that generates a new user page for every users registered such as mydomain.tf/php?username=JhonDoe I'm new to php and all the codes I tried to write just failed ... Do you have any tutorials here or anyone who could help with simple codes? Thanks! Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted October 22, 2013 Solution Share Posted October 22, 2013 With dynamic content you dont create a new .php file for every user. You would have one .php file that generates the page with the requested users data from the database. For example your go to mydomain.tf/user.php?username=JhonDoe and you should see the info about JhonDoe Here is some example code for user.php <?php $con = mysql_connect("localhost","root",""); mysql_select_db('my_database'); // get username from url if (isset($_GET['username']) && !empty($_GET['username'])) { // sanitize username $username = mysql_real_escape_string($_GET['user']); // get users details form databse $sql = "SELECT username, name, website, email FROM members_table WHERE username='$username'"; $result = mysql_query($sql,$con); // check that the query found a result if(mysql_num_rows() == 1) { // get the data from the query $user = mysql_fetch_assoc($result); // display users webpage ?> <html> <head> <title>User <?php echo $user['name'] ?></title> </head> <body> <h1><?php echo $user['username'] ?></h1> <p><b>Email:</b> <?php echo $user['email'] ?></p> <p><b>Website:</b> <?php echo $user['website'] ?></p> </body> </html> <?php } // end if (line 15) // queyr did not find the username else { // display error echo $username .' could not be found'; } } // end if (line 7) // someone didn't request for the username else { echo 'Please provide username'; } ?> Quote Link to comment Share on other sites More sharing options...
Dren Posted October 22, 2013 Author Share Posted October 22, 2013 With dynamic content you dont create a new .php file for every user. You would have one .php file that generates the page with the requested users data from the database. For example your go to mydomain.tf/user.php?username=JhonDoe and you should see the info about JhonDoe Here is some example code for user.php <?php $con = mysql_connect("localhost","root",""); mysql_select_db('my_database'); // get username from url if (isset($_GET['username']) && !empty($_GET['username'])) { // sanitize username $username = mysql_real_escape_string($_GET['user']); // get users details form databse $sql = "SELECT username, name, website, email FROM members_table WHERE username='$username'"; $result = mysql_query($sql,$con); // check that the query found a result if(mysql_num_rows() == 1) { // get the data from the query $user = mysql_fetch_assoc($result); // display users webpage ?> <html> <head> <title>User <?php echo $user['name'] ?></title> </head> <body> <h1><?php echo $user['username'] ?></h1> <p><b>Email:</b> <?php echo $user['email'] ?></p> <p><b>Website:</b> <?php echo $user['website'] ?></p> </body> </html> <?php } // end if (line 15) // queyr did not find the username else { // display error echo $username .' could not be found'; } } // end if (line 7) // someone didn't request for the username else { echo 'Please provide username'; } ?> Thank you! It works really fine, that was exactly what i was looking for Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.