Database Connection
Now that we have our data entered into MySQL and made sure our PHP Server works it is time to start writing code. I would recommend a decent PHP Editor, Notepad++ for a simple project like this, or for a major project I would choose NetBeans IDE. Those are just the editors I recommend, feel free to choose whichever editor you are comfortable with. Now onto the code.

First up, we have to make a connection to the database. Since MySQLi is not on servers with a PHP version < 5 I will use MySQL to write out the database connections.

1
2
3
4
5
6
7
8
9
10
<?php
$dbHost = 'localhost'; // localhost will be used in most cases
// set these to your mysql database username and password.
$dbUser = 'searchuser'; 
$dbPass = 'searchpass';
$dbDatabase = 'searchdb'; // the database you put the table into.
$con = mysql_connect($dbHost, $dbUser, $dbPass) or trigger_error("Failed to connect to MySQL Server. Error: " . mysql_error());
 
mysql_select_db($dbDatabase) or trigger_error("Failed to connect to database {$dbDatabase}. Error: " . mysql_error());
?>

Remember to change the $dbUser, $dbPass, $dbDatabase and $dbHost to the values of your database. If you do not the script will error out and tell you what is wrong.

2 Responses to “Simple SQL Search Using PHP and MySQL”

  1. Hi I’m building a magazine database for my work and found this tutorial really helpful, is there any way you could add pagination to it and only show a few records at a time.

  2. Hi there.

    Thanks a lot for your effective search engine. I appreciate your hard work. I was wondering if you could tell me how i would hyper link the title, display only a portion of the contents and then click and follow link for more details of the content to a separate page. Just like google style please?

    Thanks,

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
© 2012 Help Source for Coders - Get Slunked! Suffusion theme by Sayontan Sinha
Stop SOPA