Forum Moderators: coopster
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
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
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";
}
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";
}
}
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";
}
}