PHP-MySQL retrieval or getting data Good day i want to have a page that have search, and view data the search come up with firstname lastname email address phone john smith @ 169 911 view the view will be a link that shows their additional information.
There are lots of great tutorials that explain all about using PHP and MYSQL to store and retrieve data and display it. Just do a google search.
if( $_POST ) { $firstname = $_POST["firstname"]; $lastname = $_POST["lastname"]; $fetch = mysql_query( "SELECT firstname,lastname,email,address,phone FROM table WHERE firstname='".$firstname."' AND lastname='".$lastname."'" ); $array = mysql_fetch_array( $fetch ); echo "<strong>First Name:</strong> ".$array["firstname"]."<br />"; echo "<strong>Last Name:</strong> ".$array["lastname"]."<br />"; echo "<strong>Email:</strong> ".$array["email"]."<br />"; echo "<strong>Address:</strong> ".$array["address"]."<br />"; echo "<strong>Phone Number:</strong> ".$array["phone"]."<br />"; } PHP: You'll need to make a form to go with this, who's method is POST, with two input boxes who's names are firstname and lastname. Of course, this is an extremely basic example that I've cooked up for you for what you want.
i use this code and nothing happen no data is displayed <?php // code to connect to the database function connect() { $username = "webuser"; $password = ""; $database = "collinsdemo"; $server = "intranet"; $conn = mysql_connect($server, $username, $password); mysql_select_db($database); return $conn; } if( $_POST ){ $firstname = $_POST["firstname"]; $lastname = $_POST["lastname"]; $fetch = mysql_query( "SELECT firstname,lastname,email,address,phone FROM table WHERE firstname='".$firstname."' AND lastname='".$lastname."'" ); $array = mysql_fetch_array( $fetch ); echo "<strong>First Name:</strong> ".$array["firstname"]."<br />"; echo "<strong>Last Name:</strong> ".$array["lastname"]."<br />"; echo "<strong>Email:</strong> ".$array["email"]."<br />"; echo "<strong>Address:</strong> ".$array["address"]."<br />"; echo "<strong>Phone Number:</strong> ".$array["phone"]."<br />";} ?> <table width="41%" border="0" cellpadding="3" cellspacing="1" bordercolor="1" bgcolor="#FFFFFF"> <td bgcolor="#F8F7F1"><? echo "<strong>First Name:</strong> ".$array["firstname"]."<br />"; ?></td> </tr> <tr> <td bgcolor="#F8F7F1"><? echo "<strong>Last Name:</strong> ".$array["lastname"]."<br />";?></td> </tr> </table>
You need to set up a form: <form method="post" action=""> First Name: <input type="text" name="firstname" /> Last Name: <input type="text" name="lastname" /> <input type="submit" value="Submit" /> </form> Code (markup): Replace that with your <table>...</table>.
Warning: mysql_query() [function.mysql-query]: Access denied for user 'apache'@'localhost' (using password: NO) in /var/www/localhost/htdocs/15408/contacts.php on line 22 Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /var/www/localhost/htdocs/15408/contacts.php on line 22 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/localhost/htdocs/15408/contacts.php on line 23 First Name: Last Name: Email: Address: Phone Number: line 22 and 23 $fetch = mysql_query( "SELECT firstname,lastname,email,address,phone FROM table WHERE firstname='".$firstname."' AND lastname='".$lastname."'" ); $array = mysql_fetch_array( $fetch );
how can i put this data in a datable format like first name last name </head> <body> <br> <br> <h1>Vodacom Customer Relation Management</h1> <form method="post" action="<?php $PHP_SELF; ?>"> <div align="center"> <input type="text" name="search" size=25 maxlength=25> <input type="Submit" name="Submit" value=" customer search"> </div> </form> <?php //connect to mysql $username = ""; $password = ""; $database = ""; $server = ""; $db_handle = mysql_connect($server, $username, $password); $db_found = mysql_select_db($database, $db_handle); $search=$_POST["search"]; $result = mysql_query("SELECT * FROM contacts WHERE firstname LIKE '%$search%'"); while($row=mysql_fetch_array($result)) { $firstname=$row["firstname"]; $lastname=$row["lastname"]; $email=$row["email"]; $phone=$row["phone"]; echo "$firstname <br> $lastname <br> $email <br> $phone <br>"; } ?> </body> </html>
$firstname = $_POST["firstname"]; $lastname = $_POST["lastname"]; $fetch = mysql_query( "SELECT firstname,lastname,email,address,phone FROM table WHERE firstname like '%".$firstname."%' AND lastname like '%".$lastname."%'" ); $array = mysql_fetch_array( $fetch ); but for this you must have a knowledge of how to create databse, table and , how to connect with database.......and then you can fetch this type of query or suitable result..... in case of trouble you can take a help from php.net website..
this code works fine it display the data but i want them in a table format $db_handle = mysql_connect($server, $username, $password); $db_found = mysql_select_db($database, $db_handle); $search=$_POST["search"]; $result = mysql_query("SELECT * FROM contacts WHERE firstname LIKE '%$search%'"); while($row=mysql_fetch_array($result)) { $firstname=$row["firstname"]; $lastname=$row["lastname"]; $email=$row["email"]; $phone=$row["phone"]; echo "$firstname <br> $lastname <br> $email <br> $phone <br>"; } ?>
Make sure you connect to the database. You've made a connect function, but not done anything with it: <?php // code to connect to the database function connect() { $username = "webuser"; $password = ""; $database = "collinsdemo"; $server = "intranet"; $conn = mysql_connect($server, $username, $password); mysql_select_db($database); return $conn; } if( $_POST ){ connect( ); $firstname = $_POST["firstname"]; $lastname = $_POST["lastname"]; $fetch = mysql_query( "SELECT firstname,lastname,email,address,phone FROM table WHERE firstname='".$firstname."' AND lastname='".$lastname."'" ); $array = mysql_fetch_array( $fetch ); echo "<strong>First Name:</strong> ".$array["firstname"]."<br />"; echo "<strong>Last Name:</strong> ".$array["lastname"]."<br />"; echo "<strong>Email:</strong> ".$array["email"]."<br />"; echo "<strong>Address:</strong> ".$array["address"]."<br />"; echo "<strong>Phone Number:</strong> ".$array["phone"]."<br />";} ?> PHP: Should work. You should also make a disconnect function to exit the database once the above has been done. If you want to use yours with a while loop, then for each record just have: while ( ) { echo "<td>whatever</td>"; echo "<td>whatever</td>"; } PHP: Create your table outside of the loop to work properly.
$trs2=""; $trs1="<table width=500 border=0 cellspacing=0 cellpadding=0><tr><th scope=row>First name</th><td>Last name</td><td>Email</td><td>Phone</td></tr>"; while($row=mysql_fetch_array($result)) { $firstname=$row["firstname"]; $lastname=$row["lastname"]; $email=$row["email"]; $phone=$row["phone"]; $trs2.="<tr><th scope=row>".$firstname."</th><td>".$lastname."</td><td>".$email."</td><td>".$phone."</td></tr>"; } $trs3="</table>"; $mydata=$trs1.$trs2.$trs3; echo $mydata Enjoy!
You really should be using mysql_real_escape_string() on all user input, otherwise you are vulnerable to sql injections.
Hello, I'm traying make dictionary online. So, i whant to make search of word. Here is mai code: <input type='text' name='lietuviskai'> <form method="post" action="Paieska.php"> <input type="submit" value="Ieskoti" name="ieskoti"> </form> <?php function connect(){ $host="***"; $username="****"; $password="****"; $db_name="japonu_zodynas"; $tbl_name="zodziai"; // Connects to your Database $conn = mysql_connect("$host", "$username", "") or die(mysql_error()); mysql_select_db("$db_name") or die(mysql_error()); return $conn; } if ($_POST){ connect( ); $search=$_POST["lietuviskai"]; $fetch = mysql_query( "SELECT * FROM zodziai WHERE lietuviskai='".$search."'" ); $array = mysql_fetch_array( $fetch ); echo "<strong>Lietuviskai:</strong> ".$array["lietuviskai"]."<br>"; echo "<strong>Angliskai:</strong> ".$array["english"]."<br>"; echo "<strong>Tarimas:</strong> ".$array["tarimas"]."<br>"; echo "<strong>Hiragana:</strong> ".$array["hiragana"]."<br>"; echo "<strong>Katakana:</strong> ".$array["katakana"]."<br>"; } else {echo "Iveskite zodi";} ?> PHP: but i can't get result. Maybe someone can help my?
You've got your top input box outside your form, it should be inside: <form method="post" action="Paieska.php"> <input type='text' name='lietuviskai'> <input type="submit" value="Ieskoti" name="ieskoti"> </form> Code (markup):