Different Info through variables???
Moderator: General Moderators
Different Info through variables???
okay, I want to use the GET feature so users can type in "example.com/users.php?id=123" and the user "123's" page will be shown.
how do I write that in php though???
I tried (as a starter) using the GET feature as the ID just a number displayed on the page, but how to I make that variable ID redirect the user to the other users page.
ex. "http://www.example.com/users.php?id=123"
is a different page then
"http://www.example.com/users.php?id=321"
my idea would just be so when a user signs up to my website I don't have to manually create a public page for him.
how do I write that in php though???
I tried (as a starter) using the GET feature as the ID just a number displayed on the page, but how to I make that variable ID redirect the user to the other users page.
ex. "http://www.example.com/users.php?id=123"
is a different page then
"http://www.example.com/users.php?id=321"
my idea would just be so when a user signs up to my website I don't have to manually create a public page for him.
Re: Different Info through variables???
That id number must be user id in the database. For example, you have mysql table for users. Fields are:
id username pass
-- ---------- ----
123 John someblah
Then in your php script (ie users.php) you create response from URL, or like you preffer to say "GET feature". How this works is really simple, when you have URL like users.php?id=123, your script would be:
I hope it helps to understand how you can retrieve user (or anything else) from url from this simple example.
id username pass
-- ---------- ----
123 John someblah
Then in your php script (ie users.php) you create response from URL, or like you preffer to say "GET feature". How this works is really simple, when you have URL like users.php?id=123, your script would be:
Code: Select all
<?php
//first open connection to your database
$id = mysql_real_escape_string($_GET['id']); //catch the id number from url, store it and escape slashes from possible attack.
$takeuser = mysql_query("SELECT * FROM users WHERE id='$id' LIMIT 1"); //Right now script check id number from url against the database
if (mysql_num_rows($takeuser) < 1) { //checks if we have that id number in our database
echo 'User doesn\'t exist!'; //inform the user that we found 0 result
} else { //or if we found some
while ($row=mysql_fetch_array($takeuser)) { //Taking the result set
echo 'User: ' . $row['username'] . '<br/>'; //Displaying the result (ie you can have various fields in your table, display what you want)
} //closing the loop
} //closing the "if" statement
?>
Re: Different Info through variables???
Thank you SOOOO much... I asked this question on another forum but no one could give me a detailed answer. So i found this forum and asked it and you gave me a much better answer then any of my others.ghogilee wrote:That id number must be user id in the database. For example, you have mysql table for users. Fields are:
id username pass
-- ---------- ----
123 John someblah
Then in your php script (ie users.php) you create response from URL, or like you preffer to say "GET feature". How this works is really simple, when you have URL like users.php?id=123, your script would be:
I hope it helps to understand how you can retrieve user (or anything else) from url from this simple example.Code: Select all
<?php //first open connection to your database $id = mysql_real_escape_string($_GET['id']); //catch the id number from url, store it and escape slashes from possible attack. $takeuser = mysql_query("SELECT * FROM users WHERE id='$id' LIMIT 1"); //Right now script check id number from url against the database if (mysql_num_rows($takeuser) < 1) { //checks if we have that id number in our database echo 'User doesn\'t exist!'; //inform the user that we found 0 result } else { //or if we found some while ($row=mysql_fetch_array($takeuser)) { //Taking the result set echo 'User: ' . $row['username'] . '<br/>'; //Displaying the result (ie you can have various fields in your table, display what you want) } //closing the loop } //closing the "if" statement ?>
but why does this error come up
Code: Select all
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
Re: Different Info through variables???
Did you creat sour users table in mysql database? Or did you open your connection to it?
And remember this is just a slight explanation what you can do with variables in your URL.
And one more thing: read about mysql_connect, mysql_query, mysql_num_rows and other mysql functions on php.net http://www.php.net/manual/en/ref.mysql.php
Code: Select all
<?php
$link = mysql_connect('yourhost', 'username', 'password'); //replace with yur own values for connection to your database
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$dbname = 'yourdatabase';
mysql_select_db($dbname);
// here goes the other part of the script
///
mysql_close($link); //and then we close connection
?>
And one more thing: read about mysql_connect, mysql_query, mysql_num_rows and other mysql functions on php.net http://www.php.net/manual/en/ref.mysql.php
Re: Different Info through variables???
Ya a did open the connection but through another page, but I'll try this way and see If it worksghogilee wrote:Did you creat sour users table in mysql database? Or did you open your connection to it?
And remember this is just a slight explanation what you can do with variables in your URL.Code: Select all
<?php $link = mysql_connect('yourhost', 'username', 'password'); //replace with yur own values for connection to your database if (!$link) { die('Could not connect: ' . mysql_error()); } $dbname = 'yourdatabase'; mysql_select_db($dbname); // here goes the other part of the script /// mysql_close($link); //and then we close connection ?>
And one more thing: read about mysql_connect, mysql_query, mysql_num_rows and other mysql functions on php.net http://www.php.net/manual/en/ref.mysql.php
EDIT: Nope didn't work came up with the same error (I filled everything out correctly, I think)
Re: Different Info through variables???
I think that you didn't understand
.. Post your code and post your table structure here
Re: Different Info through variables???
Code: Select all
<?php
$link = mysql_connect('localhost', 'mydatabaseusername', 'databasepass'); //replace with yur own values for connection to your database
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$dbname = 'batabasename';
mysql_select_db($dbname);
// here goes the other part of the script
$id = mysql_real_escape_string($_GET['id']); //catch the id number from url, store it and escape slashes from possible attack.
$takeuser = mysql_query("SELECT * FROM users WHERE id='$id' LIMIT 1"); //Right now script check id number from url against the database
if (mysql_num_rows($user_name) < 1) { //checks if we have that id number in our database
echo "User doesn't exist!" ;
} else { //or if we found some
while ($row=mysql_fetch_array($username)) { //Taking the result set
echo 'User: ' . $row['user_name'] . '<br/>'; //Displaying the result (ie you can have various fields in your table, display what you want)
} //closing the loop
} //closing the "if" statement
///
mysql_close($link); //and then we close connection
?>
Re: Different Info through variables???
change into:
Code: Select all
if (mysql_num_rows($takeuser) < 1)
Re: Different Info through variables???
Thanks... IT WORKED. DO you have any website for expanding my knowledge of the _GET feature
Re: Different Info through variables???
http://www.php.net/manual/en/reserved.variables.get.phpkedora19 wrote:Thanks... IT WORKED. DO you have any website for expanding my knowledge of the _GET feature