Forum Moderators: coopster

Message Too Old, No Replies

Creating links from db content

         

contraband

6:45 pm on Jan 17, 2006 (gmt 0)

10+ Year Member



hi there

i am trying to extract a field from one of my tables, and with that value, create a link to another page.

for example,

i am listing all my users in my database. Then, with that username at the text for the link, i create a link to their profile page.

at the moment i have the following code to select the usernames from my table:

while ($row = mysql_fetch_array($query)) {
echo "<p>","username: " , $row['username'];

thanks in advance,

andy

grandpa

6:57 pm on Jan 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This could get you the results as you want them. Don't forget the closing bracket }

while ($row = mysql_fetch_array($query)) {
echo "<p>","<a href=\"http://www.domain.com/" . $row['username'] . "\">" . $row['username'] . "</a>";
}

contraband

6:58 pm on Jan 17, 2006 (gmt 0)

10+ Year Member



thank you my friend =)

contraband

6:02 pm on Jan 18, 2006 (gmt 0)

10+ Year Member



just one last little problem..

when the user enters their username in th txt box and click submit i use the following code to check it against the database field:

if ($row['field5'] = $username) {
echo "Character ". $var . " Found";
}
else {
echo "Character does not exist";
}

the thing is that whatever i type in the input box it says:

Character (what you entered in the box) Found

am i doing the completely wrong?

thanks in advance,

andy

kamakaze

6:08 pm on Jan 18, 2006 (gmt 0)

10+ Year Member



If the variable you are checking is a string then it needs to be surrounded by single quotes. Also you need to use the right comparison operator. In this case you want == which means equal to.

What you were doing before was setting the value of $username equal to the value of $row['field5'] hence why it was echoing out whatever was typed in.


if ($row['field5'] == '$username') {
echo "Character ". $var . " Found";
}
else {
echo "Character does not exist";
}

contraband

6:40 pm on Jan 18, 2006 (gmt 0)

10+ Year Member



hm... now it just says:

Character does not exist

whatever i type in :s

grandpa

7:02 pm on Jan 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Have you looked at the content of field5 to make sure it contains what you are looking for? One of the easiest ways to troubleshoot most scripts is to echo the results of your last event, in this case querying the database. Once you have the result then take a quick look.

echo $row['field5'];

contraband

7:28 pm on Jan 18, 2006 (gmt 0)

10+ Year Member



i think i am doing something majorly wrong.
i have run the $row['field5']; thing and it has returned a value, i entered it in and it still says:

Character (whatever you entered in) does not exist

i will show you the code i am using:

$var = @$_GET['username'] ;
$trimmed = trim($var); //trim whitespace from the stored variable

// check for an empty string and display a message.
if ($trimmed == "")
{
echo "<p>Please enter a username</p>";
exit;
}

//connect to database
mysql_connect("localhost","***********","*******");

//database
mysql_select_db("*****************") or die("Unable to select database");

// SQL Query
$query = "select * from userfield where field5 like \"%$trimmed%\"";

$result = mysql_query($query) or die("Couldn't execute query");

while ($row = mysql_fetch_assoc($result)) {
if ($row['field5'] == '$var') {
echo "Character ". $var . " Found";
}
else {
echo "Character ". $var . " does not exist";
}
}

contraband

8:06 pm on Jan 18, 2006 (gmt 0)

10+ Year Member



solved it. =)

how do you say not equal to?

is it <>?

grandpa

8:06 pm on Jan 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hmmm, you solved it while I was typing the following diatribe ;) Congrats.
how do you say not equal to?
!=

<save the following for a rainy day>

I wouldn't characterize what you have as 'majorly wrong', but to my thinking there is a flaw in your logic.

After you retrive the recorset you are running a while loop over the entire set... that's first record to last. For each record your either echoing the Found or the Not Found Statement. Basically, I think that what you are displaying is the result of your comparison against the last record in the recordset.

In a situation like this you have a couple of ways of doing things. The first, and the method I prefer, is to craft your query so that you only return one record or only those records that match your criteria vs returning the entire table.

Alternatively, you can exit or break your processing over the entire dataset as soon as you find a match. The exit() and break() functions will work for this.

To see if that's the problem here, you can insert an exit into your code:

while ($row = mysql_fetch_assoc($result)) {
if ($row['field5'] == '$var') {
echo "Character ". $var . " Found";
exit();
}
else {
echo "Character ". $var . " does not exist";
}
}

contraband

8:15 pm on Jan 18, 2006 (gmt 0)

10+ Year Member



ooh.. your way is a lot better =)

i will use that,

thank you =)

grandpa

1:21 am on Jan 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Keep in mind there is a significant difference between how the exit and break functions work. Exit stops any more execution in your script.. it's finished. Break will allow you continue processing when you break out of the curent loop.

There are times when one is always better than the other.