retrieving single data from database
Posted by: administrator (---.sympatico.ca)
Date: June 13, 2006 08:23AM

I have successfully created a php code to retrieve all data from database created in mysql. How do I create a php code that will ask user only a name and then search that name in the database and printed on screen the information about that name/user, if he/she exists in the database. Please help. Thank you in advance.

Options: ReplyQuote
Re: retrieving single data from database
Posted by: SkareCrow (---.dhcp.wnch.wa.charter.com)
Date: June 15, 2006 12:04PM

Here is a small peice of code I just made for you.
Hope it helps you.

<?PHP
$host = "localhost";
$user = "root";
$pass = "";
$db = "";

mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());

if (isset($_GET['user_name'])) {
$username = mysql_real_escape_string($_GET['user_name']);
$query = mysql_query("SELECT * FROM members WHERE user_name='".$user_name."' LIMIT 1"winking smiley or die(mysql_error());
if (mysql_num_rows($query) > 0) {
$row = mysql_fetch_assoc($query);
echo "The username {$row['user_name']} exists.<br> Other information about this person.";
print_r($row);
} else {
echo "The username {$user_name} does not exist.";
}
}

?>

<form action='<?PHP echo $_SERVER['PHP_SELF']; ?>' method='get'>
<input type='text' name='user_name'>
<input type='submit' value='Search User'>
</form>

Options: ReplyQuote
Re: retrieving single data from database
Posted by: SkareCrow (---.dhcp.wnch.wa.charter.com)
Date: June 15, 2006 12:05PM

sorry,
$username = mysql_real_escape_string($_GET['user_name']);
shout be
$user_name = mysql_real_escape_string($_GET['user_name']);

Options: ReplyQuote
Re: retrieving single data from database
Posted by: administrator (---.sympatico.ca)
Date: June 16, 2006 06:03AM

Thank you very much. Although I have not tried it yet, I will definitely let you know if everrything is okay. It looks like it is going to work. I really appreciated your precious time in creating me a php code. Thank you very much.

Options: ReplyQuote


Sorry, only registered users may post in this forum.