Jump to content

[SOLVED] Php Retrive MySql Data & .htaccess mod_rewrite.


Recommended Posts

Hello, I've started to code a community site. I'm aware there are loads out there, but unless I do it all myself theres to much code to-go through to find out what it does.. so I'm starting one from scratch.

 

Right now I have a database table called "Users" each user has an "ID" & "username"

 

www.mysitename.com/index.php?users.mainpage=1

or

www.mysitename.com/?users.mainpage=1

 

works and brings you to that users main page. '1' represents the users ID number.. so that would load user '1'. I want to make mod_rewrite allow me to also put in the users name... so the username Mike is linked to ID 1. so

 

www.mysitename.com/?users.mainpage=mike would goto www.mysitename.com/?users.mainpage=1

now I am to understand I have to use a sitemap, or external php script (I want to use php) I just can't for the life of me figure out exactly how or what i need to-do. I've done a fair amount of searching but nothing. So if any has any tutorials or links suggestions or simply knows how and can make me a sample that would be awesome. I can also be found on IRC / msn with the following info:

 

msn = morrisf89@hotmail.com

irc = irc.freenode.net in channel #__mike

 

Thanks.

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)/$ user.php?name=$1				[nc]

 

Assuming you only have unique usernames, you could do this

<?php
if(isset($_GET['name'])){
    $sql = mysql_query("SELECT * FROM users WHERE userName='{$_GET['name']}'")or die(mysql_error());
}elseif(isset($_GET['id'])){
    $sql = mysql_query("SELECT * FROM users WHERE id='{$_GET['id']}'")or die(mysql_error());
}else{
    $error = TRUE;
}
if(!$error){
    $row = mysql_fetch_array($sql);
    echo 'Viewing '.$row['user'].'\'s profile';
}else{
    echo 'Invalid user';
}
?>

awesome thank you so much, editied it and it worked great!! just 2 more quick questions...

 

I've edited it so

 

www.mysite.com/mike/  will goto www.mysite.com/index.php?users.mainpage=1

 

via your mod rewrite & php script. Now my first question is

www.mysite.com/mike/ works...

www.mysite.com/mike dosn't... ?? how come?

 

and secondly. once a user gets redirected is there anywhere to keep the address in the bar the same? so when they get sent to www.mysite.com/index.php?users.mainpage=1 it stays as www.mysite.com/mike/

 

 

Thanks agaiN!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.