How to Query Your MySQL Database

Tutorial on How to Execute Commands on Your MySQL Server

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.