Page 1 of 1

Different Info through variables???

Posted: Tue Apr 07, 2009 10:10 am
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.

Re: Different Info through variables???

Posted: Tue Apr 07, 2009 11:23 am
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.

Re: Different Info through variables???

Posted: Tue Apr 07, 2009 7:29 pm
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
 

Re: Different Info through variables???

Posted: Wed Apr 08, 2009 7:29 am
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

Re: Different Info through variables???

Posted: Wed Apr 08, 2009 9:30 am
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)

Re: Different Info through variables???

Posted: Wed Apr 08, 2009 12:07 pm
by ghogilee
I think that you didn't understand :).. Post your code and post your table structure here

Re: Different Info through variables???

Posted: Wed Apr 08, 2009 5:55 pm
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)

Re: Different Info through variables???

Posted: Thu Apr 09, 2009 2:32 am
by ghogilee
change into:

Code: Select all

 
if (mysql_num_rows($takeuser) < 1)
 

Re: Different Info through variables???

Posted: Thu Apr 09, 2009 9:35 am
by kedora19
Thanks... IT WORKED. DO you have any website for expanding my knowledge of the _GET feature

Re: Different Info through variables???

Posted: Thu Apr 09, 2009 2:47 pm
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