faqts : Computers : Programming : Languages : PHP : Database Backed Sites : MySQL

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

14 of 14 people (100%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

How do I extract records from an existing database using user input?

Mar 6th, 2002 00:02
Nick R, Elaine Voon,


<!--Hi,
Say for instance you're trying to draw the user_name and password from 
a database. You would create a form to allow the user to enter certain 
fields and then connect to the db and display the info.-->
<html>
<?php 
//COMMENT: CREATE  GLOBAL VARIABLES $name, $category, $PHP_SELF
global $name, $category, $PHP_SELF; 
?>
<body>
<!--COMMENT:  CREATE THE FORM-->
<form name="get_db_info" action="<?php echo $PHP_SELF; ?>">
User Name: <input type="text" name="user_name"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Submit">
</form>
<?php
//COMMENT: VARIFY USERNAME AND PASSWORD
if($user_name && $pass)
{
//COMMENT: CONNECT TO YOUR DB REPLACE USERNAME, PASS, AND //  DB WITH 
YOUR INFO.
 $link_id = mysql_connect
("localhost", "replace_your_user_name", "replace_your_pass");
 $result=mysql_select_db("replace_your_database", $link_id);
//COMMENT: CREATE TABLE FOR DISPLAY
echo " <table><tr> ";
//COMMENT: use a PHP select query to select USERname and PASS or all(*) 
the // info.
$result1=mysql_query("SELECT DISTINCT * FROM awards WHERE 
user_name='$user_name' && password='$password' ORDER BY user_name");
    //COMMENT: use a while loop to display the results
    while($data=mysql_fetch_array($result1))
   {
       echo "<td>".$data["user_name"]."</td>";
       echo "<td>".$data["password"]."</td></tr><tr>";
   }
echo "</tr></table>";
}
//COMMENT: if not user_name and password exit the script and give     
// button to re-enter
   else{echo "You need to enter your username and pass correctly <a 
href='#' onclick='history.go(-1);'> << Go Back </a>";
exit;}
?>
</body></html>