How to Do a Live Search With PHP and MySQL
The PHP and MySQL languages integrate together, so you can create dynamic Web pages on your website. The MySQL database contains the data, and you use the PHP language to send queries to the database. The returned values are stored in a record set. You use the record set variable to display the values to your readers.
Instructions
-
-
1
Right-click the PHP file you want to use to do a live search in your database. Click "Open With," then click your PHP editor.
-
2
Make the connection to the server. PHP has an internal function that connects to a MySQL server name. You must supply the username and password for the server to allow you to connect. The following code connects to the MySQL server:
mysql_connect("localhost", "username", "password") or die(mysql_error());
Replace "localhost" with the name of your server. Replace the username and password with your own.
-
-
3
Select the database on the server. After the server connection is made, you must connect to the database. Add the following code to connect to the database:
mysql_select_db("mydb") or die(mysql_error());
Replace "mydb" with the name of your own database.
-
4
Create the query. You use the "select" statement to retrieve live data from the database. The following code creates a MySQL query:
$sql = "select * from customers";
In this example, a list of customers is retrieved from the "customers" table.
-
5
Perform the query on the server. The following code runs the query on the server:
$records= mysql_query($sql)
-
1