Data from database to a link???

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

Data from database to a link???

Post by kedora19 »

i wanted to know how you could turn the data that you've got from the database into a variable link.

right now i have this in my code. (the include db.php is my database connection)

Code: Select all

 
<?php
 include 'db.php';
  
//first open connection to your database
 
 $takeuser = mysql_query("SELECT full_name FROM users LIMIT 5"); //Right now script check id number from url against the database
   
 while ($row=mysql_fetch_array($takeuser)) { //Taking the result set
     echo 'Name: ' . $row['full_name'] . '<br/><br/>';
 } //closing the loop
   
 
 mysql_close($link); //and then we close connection
?>
 
everything works and it shows the names of the people in the database but is there as way to also add a hyperlink to the name.

example: Name: Bob Doug (the name would be linked to http://mysite.com/users.php?name=[what ever the name is]

I know how to use the variable "_get" all i want to know is how i could connect the two pages

Thanks,
Kedora19
PS: this page would home.php and would link to users.php
Last edited by Benjamin on Tue Apr 28, 2009 10:30 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Data from database to a link???

Post by requinix »

Do you know the HTML for a link? Do you know how to use echo? Do you know how to use variables?

Put them all together.
kedora19
Forum Commoner
Posts: 34
Joined: Tue Apr 07, 2009 10:05 am

Re: Data from database to a link???

Post by kedora19 »

Okay I thought I could do that, but how would I put it in a echo...
would I get rid of the [full_name] and just use $row in the url for the variable.

thats the only problem I have.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Data from database to a link???

Post by McInfo »

Instead of

Code: Select all

echo 'Name: ' . $row['full_name'] . '<br/><br/>';
write

Code: Select all

echo 'Name: <a href="users.php?name=' . urlencode($row['full_name']) . '">' . $row['full_name'] . '</a><br /><br />';
Then, in users.php write (Edit: INCORRECT)

Code: Select all

echo urldecode($_GET['name']);
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 1:48 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Data from database to a link???

Post by requinix »

McInfo wrote:Then, in users.php write

Code: Select all

echo urldecode($_GET['name']);
Actually, don't. The server decodes everything for you, so if you decode it again then you can get the wrong results.

If the name is "%abcdefg%" then $_GET["name"] will already be "%abcdefg%". Run it through urldecode and you'll get "«cdefg%".
kedora19
Forum Commoner
Posts: 34
Joined: Tue Apr 07, 2009 10:05 am

Re: Data from database to a link???

Post by kedora19 »

thanks... Mcinfo and tasairis this helped me A LOT
but I have one problem.

Code: Select all

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
this message pops up even though I have the right code.

here's my index.php code

Code: Select all

 
<?php
 include 'db.php';
  
//first open connection to your database
 
 $takeuser = mysql_query("SELECT full_name FROM users LIMIT 5"); //Right now script check name from url against the database
   
 while ($row=mysql_fetch_array($takeuser)) { //Taking the result set
     echo 'Name: <a href="users.php?name=' . urlencode($row['full_name']) . '">' . $row['full_name'] . '</a><br /><br />';
 } //closing the loop
   
 
 mysql_close($link); //and then we close connection
?>
 
and this is my users.php code

Code: Select all

<?php
 include 'db.php';
  
//first open connection to your database
  
 $name = mysql_real_escape_string($_GET['name']); //catch the name from url, store it and escape slashes from possible attack.
 $takeuser = mysql_query("SELECT * FROM users WHERE name='$name' LIMIT 5"); //Right now script check name from url against the database
    if (mysql_num_rows($takeuser) < 1) { //checks if we have that name in our database
     echo "Wrong Argument!"; //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 'Name: ' . $row['full_name'] . '<br/>';
     echo 'User: ' . $row['user_name'] . '<br/>'; //Displaying the result (ie you can have various fields in your table, display what you want)
     echo 'E-mail: ' . $row['user_email'] . '<br/>';
 } //closing the loop
 } //closing the "if" statement
  
 
 mysql_close($link); //and then we close connection
?>
Last edited by Benjamin on Wed Apr 29, 2009 7:33 pm, edited 2 times in total.
Reason: Changed code type from text to php.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Data from database to a link???

Post by McInfo »

tasairis wrote:Actually, don't. The server decodes everything for you, so if you decode it again then you can get the wrong results.
I guess that's what I get for not testing my code before I post it. Sorry about that.
PHP Manual: urldecode wrote:The superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results.
Edit: This post was recovered from search engine cache.
Post Reply