1. Computing

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

Related Video
Basic PHP Syntax
Using PHP With HTML
  1. About.com
  2. Computing
  3. PHP / MySQL
  4. PHP with MySQL
  5. PHP MySQL - PHP MySQL Tutorial - SQL Queries

©2013 About.com. All rights reserved.