Page 1 of 1
Data from database to a link???
Posted: Tue Apr 28, 2009 9:55 pm
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
Re: Data from database to a link???
Posted: Wed Apr 29, 2009 12:00 am
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.
Re: Data from database to a link???
Posted: Wed Apr 29, 2009 9:31 am
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.
Re: Data from database to a link???
Posted: Wed Apr 29, 2009 5:47 pm
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)
Edit: This post was recovered from search engine cache.
Re: Data from database to a link???
Posted: Wed Apr 29, 2009 6:17 pm
by requinix
McInfo wrote:Then, in
users.php write
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%".
Re: Data from database to a link???
Posted: Wed Apr 29, 2009 7:07 pm
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
?>
Re: Data from database to a link???
Posted: Wed Apr 29, 2009 7:08 pm
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.