- This topic is empty.
-
AuthorPosts
-
February 1, 2012 at 10:14 am #36453
schart
ParticipantI put this in the PHP section, but it might as well be .htaccess . So I have this basic profile page, kind of like facebook’s:
/profile.php?id=1By using PHP, I want to get the username for that ID (which I know how to do), and then go like this:
Redirect
/profile.php?id=1
to go to
/SchartMy “get username from id” code:
$id = $_GET; //ID of page, (profile.php?id=1)
/* Also, a session with user information:
$username = $_SESSION; // Username of logged in user
$password = $_SESSION; // Password of logged in user
*/
$write = mysql_query("SELECT username FROM husers
WHERE id = '$id'");
$uname = "";
while ($row = mysql_fetch_assoc($write)) {
$uname .= $row; // Username of the profile page.
}
?>
http://underbakke.net/profile.php?id=1
http://underbakke.net/index.phpFebruary 1, 2012 at 2:14 pm #95914bungle
MemberExpanding on the code i gave you in the other thread at https://css-tricks.com/forums/discussion/15865/if-id-does-not-exists-php#Item_5
PHP can do the redirect for you with a header rewrite – but, and this is important, the header rewrite only works if you have not yet outputted anything else so this PHP code needs to come at the very top of your php file with no whitespace before it
$id = mysql_real_escape_string($_GET);
$sql = "SELECT * FROM users WHERE id=$id";
$result = mysql_query($sql);
if (mysql_num_rows($result)==0) {
echo ('User never existed');
}
else if (mysql_num_rows($result)==1) {
$userinfo = mysql_fetch_array($result);
if (!$userinfo) {
echo ('User no longer exists.');
}
else {
$username = $userinfo;
header('Location: http://www.yoursite.com/'.$username);
}
}
?>February 1, 2012 at 2:34 pm #95920bungle
Memberoh i see you want the url to change and then it to pull the correct page then you would need some .htaccess rules to make that work else each username will need a page
February 1, 2012 at 3:19 pm #95928bungle
MemberFebruary 1, 2012 at 3:31 pm #95929bungle
MemberAnd the original code would need to be expanded to account for username variables too so
oh i see you want the url to change and then it to pull the correct page. Then you need to do something like the following
if (isset($_GET)) {
$id = mysql_real_escape_string($_GET);
$sql = "SELECT * FROM users WHERE id=$id";
$result = mysql_query($sql);
if (mysql_num_rows($result)==0) {
echo ('User never existed');
}
else if (mysql_num_rows($result)==1) {
$userinfo = mysql_fetch_array($result);
if (!$userinfo) {
echo ('User no longer exists.');
}
else {
$username = $userinfo;
header('Location: http://www.yoursite.com/'.$username);
}
}
}
else if (isset($_GET)) {
$uname = mysql_real_escape_string($_GET);
$sql = "SELECT * FROM users WHERE username=$username";
$result = mysql_query($sql);
if (mysql_num_rows($result)==0) {
echo ('User never existed');
}
else if (mysql_num_rows($result)==1) {
$userinfo = mysql_fetch_array($result);
if (!$userinfo) {
echo ('User no longer exists.');
}
else {
echo('Welcome to your Profile Page '.$userinfo['full name');
}
}
}
?>February 1, 2012 at 5:07 pm #95947bungle
MemberSorry if i confused you with my earlier suggestion, I was suggesting you’d need a combination of both mod rewrite and the php. You’d obviously need to make sure the mod rewrite was selective enough not to catch the full redirected URL that php needs also and the header rewrite would need to be redone to avoid the double redirect.
On a different thought, if you were doing ajax you could alternately use a hashtag like “site#username” and then pull the pages with a get request to PHP but this would only work if you were using ajax as hashtags don’t get passed server side to PHP.
February 1, 2012 at 5:21 pm #95949Historical Forums User
Participant@schart : I would’ve been glad to know this site when I started :D
@bungle : I’d suggest rather not to use Ajax with hashtags until history.pushState is widely supported to use Ajax and give parameters to Php on reload. It’s also the way Facebook and Github do it, Facebook just also provides it with hashtags for older browsersFebruary 1, 2012 at 5:27 pm #95951bungle
Memberyou can have the php run the queries from either id or username anyway
so “site/profile/username” becomes rewritten to site/profile.php?u=username”
and the php then does
$username=mysql_real_escape_string($_GET);
“SELECT * from users where username=$username”February 1, 2012 at 5:29 pm #95952Historical Forums User
ParticipantWhy you don’t just deliver the name instead of the Id to the profile.php?
February 1, 2012 at 5:31 pm #95953Historical Forums User
ParticipantBungle was faster than me, because of my phone, this German autocorrect is killimg me!
February 1, 2012 at 5:36 pm #95955Historical Forums User
Participant“select Id from Table where Name=’$name'” and you have the id
February 1, 2012 at 5:39 pm #95956Historical Forums User
ParticipantI don’t think you can set the handler of .htaccess because that is done in .htaccess. There’s no way of using Php within .htaccess
February 1, 2012 at 7:19 pm #95969bungle
Member“Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /customers/9/7/5/underbakke.net/httpd.www/profile.php on line 25 Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /customers/9/7/5/underbakke.net/httpd.www/profile.php on line 92”
Your mysql_query() must be returning false rather than an array.
I would guess that is because your query needs to query username = “something” where as id can successfully be queried against a non quoted integer like id = 3
you would need to modify the query to
‘SELECT * from users WHERE username = “‘.$username.'”‘
so that the username is quoted out in the actual query string.
February 2, 2012 at 9:11 am #95997bungle
MemberSomething else must be wrong in the query though, that error means the query is failing
I would put an echo line in after the query to see exactly what the query that php is running is. You can then run that query manually and have sql tell you what’s wring with it.
$sql=’SELECT from blah blah’;
echo $sql;
$result = mysql_query($sql);February 2, 2012 at 1:26 pm #96006bungle
MemberCouple of different points for you
In the middle of this you have a series of queries run one after the other using a while loop to and mysql_fetch_assoc each time to fill a variable
You can fill all of these with a single query and mysql_fetch_array()
So instead
$result = mysql_query(“SELECT info,email,img FROM husers WHERE id = $id”);
$userinfo = mysql_fetch_array($result);
$info = $userinfo;
$email = $userinfo;
$img = $userinfo;will save you running multiple queries
Also, be careful when including variables in query statements
PHP will substitute in variables only inside double quotes
So $sql = ‘SELECT * from users where id = $id’;
will not work, instead you need
$sql = “SELECT * from users where id = $id”;
or
$sql = ‘SELECT * from users where id = ‘.$id;
and if you need to include a string rather than an integer then you need to quote that string inside the sql statement
so $sql = ‘SELECT * from users where username = “‘.$uname.'”‘;
will result in an actual query of SELECT * from users where username = “john” or whatever
Make sense?
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.