1. Home
  2. Computing & Technology
  3. PHP / MySQL

PHP MySQL Tutorial

By Angela Bradley, About.com

3 of 5

SQL Queries with PHP

Now that you have done one query, you can do more complicated queries using the same basic syntax. If you have forgotten the queries, you can review them in the MySQL glossary.

Let's try to do a query of our database for people who have cats for a pet. We will do this by adding a WHERE clause to set pet equal to Cat.

<?php
// Connects to your Database
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
$data = mysql_query("SELECT * FROM friends WHERE pet='Cat'")
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['name'] . "</td> ";
Print "<th>Color:</th> <td>".$info['fav_color'] . "</td> ";
Print "<th>Food:</th> <td>".$info['fav_food'] . "</td> ";
Print "<th>Pet:</th> <td>".$info['pet'] . " </td></tr>";
}
Print "</table>";
?>

3 of 5

Index: PHP MySQL Tutorial

  1. Connect to MySQL
  2. Retrieve Data
  3. SQL Queries with PHP
  4. Create Tables
  5. Insert Into Tables

Explore PHP / MySQL

More from About.com

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. PHP with MySQL
  5. PHP MySQL - PHP MySQL Tutorial - SQL Queries

©2008 About.com, a part of The New York Times Company.

All rights reserved.