web hosting line
1.866.605.2484
Free Web Hosting

PHP/MySQL Tutorial - PHP MySQL Connection

How to Query the Database

Step 3: There are at least two ways to query a database. One is to enter the command in PHP.

Another way is to define the command as a variable.

In this part of the tutorial we will show the first way. The command will look like this:

mysql_query($query);

The command can be repeated over and over again in the source code. All you need to do is to change the variable.

Here is the complete code that should be used to create a MySQL table in PHP:

<?php
$user="username";
$password="password";
$database="database";
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="CREATE TABLE tablename(id int(6) NOT NULL auto_increment,first varchar(15) NOT NULL,last varchar(15) NOT NULL,field1-name varchar(20) NOT NULL,field2-name varchar(20) NOT NULL,field3-name varchar(20) NOT NULL,field4-name varchar(30) NOT NULL,field5-name varchar(30) NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))";
mysql_query($query);
mysql_close();
?>

Enter your database, MySQL username and MySQL password in the appropriate positions on the first three lines above.

The next query should fill in the table. Here is a sample one:

$query = "INSERT INTO tablename VALUES
('','$field1-name','$field2-name','$field3-name','$field4-name','$field5-name')";

You can't insert more values than the number of fields you have created with the first query.

Step 4: Display table data
Step 5: Select individual records 

Previous Next


PHP Hosting
(c) Copyright 2008 SiteGround Web Hosting Provider. All rights reserved
Resources: Tutorials . Video Tutorials . Web hosting FAQ . Joomla templates . WordPress themes . Web hosting forum
Quick links: eCommerce hosting . Joomla hosting . Magento hosting . phpBB hosting . WordPress hosting . Blog Hosting . cPanel hosting . FTP hosting . Flash hosting . Website Design
Copyright © 2009 SiteGround.com Inc