PHP-MySQL retrieval or getting data with search

Discussion in 'PHP' started by babyphp, Apr 15, 2008.

  1. #1
    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.
     
    babyphp, Apr 15, 2008 IP
  2. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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.
     
    CreativeClans, Apr 15, 2008 IP
  3. Xtrm2Matt

    Xtrm2Matt Active Member

    Messages:
    129
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #3
    
    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.
     
    Xtrm2Matt, Apr 15, 2008 IP
  4. babyphp

    babyphp Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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>
     
    babyphp, Apr 15, 2008 IP
  5. Xtrm2Matt

    Xtrm2Matt Active Member

    Messages:
    129
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #5
    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>.
     
    Xtrm2Matt, Apr 15, 2008 IP
  6. babyphp

    babyphp Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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 );
     
    babyphp, Apr 15, 2008 IP
  7. babyphp

    babyphp Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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>
     
    babyphp, Apr 15, 2008 IP
  8. singh.ajit05

    singh.ajit05 Peon

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    $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..
     
    singh.ajit05, Apr 15, 2008 IP
  9. babyphp

    babyphp Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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>";
    }



    ?>
     
    babyphp, Apr 15, 2008 IP
  10. Xtrm2Matt

    Xtrm2Matt Active Member

    Messages:
    129
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #10
    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.
     
    Xtrm2Matt, Apr 15, 2008 IP
  11. singh.ajit05

    singh.ajit05 Peon

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    $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!
     
    singh.ajit05, Apr 17, 2008 IP
  12. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #12
    You really should be using mysql_real_escape_string() on all user input, otherwise you are vulnerable to sql injections.
     
    matthewrobertbell, Apr 17, 2008 IP
  13. Cimorile

    Cimorile Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    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?
     
    Cimorile, Jul 11, 2009 IP
  14. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #14
    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):
     
    wd_2k6, Jul 11, 2009 IP
  15. Cimorile

    Cimorile Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Realy, thanx :)
     
    Cimorile, Jul 11, 2009 IP