Trill Designs: Simple Mysql Database Search - Trill Designs

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Simple Mysql Database Search

#1 User is offline   Jaan Icon

  • Site Admin
  • Icon
  • Group: Administrators
  • Posts: 83
  • Joined: 23-November 09
  • LocationEstonia

Posted 25 January 2010 - 09:20 PM

Hello

Here i will show how to make a simple searchengine. First we must make a form:

search.html

<form action="search.php" method="post">
<input type="text" name="find" size="30" /><br />
<input type="submit" value="Search" />
</form>


Now we must make this php part.

search.php

<?php
// First connect to database
$host = "localhost";
$dbuser = "username";
$dbpass = "password";
$db = "database";

$con = mysql_connect($host, $dbuser, $dbpass);
if(!$con){
die(mysql_error());
}

$select = mysql_select_db($db, $con);
if(!$select){
die(mysql_error());
}

// Now collect all info into [B]$item[/B] variable

$item = $_REQUEST['find'];

// This will take all info from database where row [I]tutorial[/I] is $item and collects it into [B]$data[/B] variable

$data = mysql_query("SELECT * FROM tutorials WHERE tutorial LIKE '%$item%'");

// This creates a loop which will repeat itself until there are no more rows to select from the database. We getting the field names and storing them in the $row variable. This makes it easier to echo each field.

while($row = mysql_fetch_array($data)){
echo $row['date']. "<br>";
echo $row['id']. "<br>";
}

?>

0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users