Different Info through variables???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kedora19
Forum Commoner
Posts: 34
Joined: Tue Apr 07, 2009 10:05 am

Different Info through variables???

Post by kedora19 »

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.
ghogilee
Forum Newbie
Posts: 19
Joined: Tue Apr 07, 2009 8:45 am
Location: Novi Sad, Serbia

Re: Different Info through variables???

Post by ghogilee »

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:

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
?>
 
I hope it helps to understand how you can retrieve user (or anything else) from url from this simple example.
kedora19
Forum Commoner
Posts: 34
Joined: Tue Apr 07, 2009 10:05 am

Re: Different Info through variables???

Post by kedora19 »

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:

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
?>
 
I hope it helps to understand how you can retrieve user (or anything else) from url from this simple example.
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.

but why does this error come up

Code: Select all

 
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
 
ghogilee
Forum Newbie
Posts: 19
Joined: Tue Apr 07, 2009 8:45 am
Location: Novi Sad, Serbia

Re: Different Info through variables???

Post by ghogilee »

Did you creat sour users table in mysql database? Or did you open your connection to it?

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 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
kedora19
Forum Commoner
Posts: 34
Joined: Tue Apr 07, 2009 10:05 am

Re: Different Info through variables???

Post by kedora19 »

ghogilee wrote:Did you creat sour users table in mysql database? Or did you open your connection to it?

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 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
Ya a did open the connection but through another page, but I'll try this way and see If it works

EDIT: Nope didn't work came up with the same error (I filled everything out correctly, I think)
ghogilee
Forum Newbie
Posts: 19
Joined: Tue Apr 07, 2009 8:45 am
Location: Novi Sad, Serbia

Re: Different Info through variables???

Post by ghogilee »

I think that you didn't understand :).. Post your code and post your table structure here
kedora19
Forum Commoner
Posts: 34
Joined: Tue Apr 07, 2009 10:05 am

Re: Different Info through variables???

Post by kedora19 »

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
?>
 
I changed the database info to just a description cause I don't want to tell you the password (for obvious reasons)
ghogilee
Forum Newbie
Posts: 19
Joined: Tue Apr 07, 2009 8:45 am
Location: Novi Sad, Serbia

Re: Different Info through variables???

Post by ghogilee »

change into:

Code: Select all

 
if (mysql_num_rows($takeuser) < 1)
 
kedora19
Forum Commoner
Posts: 34
Joined: Tue Apr 07, 2009 10:05 am

Re: Different Info through variables???

Post by kedora19 »

Thanks... IT WORKED. DO you have any website for expanding my knowledge of the _GET feature
ghogilee
Forum Newbie
Posts: 19
Joined: Tue Apr 07, 2009 8:45 am
Location: Novi Sad, Serbia

Re: Different Info through variables???

Post by ghogilee »

kedora19 wrote:Thanks... IT WORKED. DO you have any website for expanding my knowledge of the _GET feature
http://www.php.net/manual/en/reserved.variables.get.php
Post Reply