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

PHP MySQL Tutorial

By , About.com Guide

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>";
?>

Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

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

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

All rights reserved.