|
Home >> FAQs/Tutorials >> PHP Script Tutorials and Tips >> Index
PHP Script Tips - Working with MySQL Database
By: FYICenter.com
Part:
1
2
3
4
5
6
7
(Continued from previous part...)
How To Create a Database?
A database in a MySQL server is a logical container used to group tables and other data objects
together as a unit. If you are a the administrator of the server, you can create and delete databases
using the CREATE/DROP DATABASE statements. The following PHP script shows you
how to create and drop an database called "fyi":
<?php
$con = mysql_connect('localhost');
$sql = 'CREATE DATABASE fyi';
if (mysql_query($sql, $con)) {
print("Database fyi created.\n");
} else {
print("Database creation failed.\n");
}
$sql = 'DROP DATABASE fyi';
if (mysql_query($sql, $con)) {
print("Database fyi dropped.\n");
} else {
print("Database drop failed.\n");
}
mysql_close($con);
?>
If you run this script, you will get something like this:
Database fyi created.
Database fyi dropped.
How To Select an Exiting Database?
The first thing after you have created a connection object to the MySQL server
is to select the database where your tables are locate, by using the mysql_select_db()
function. If your MySQL server is offered by your Web hosting company, they will
assign a database to you and provide you the database name. You should use this
name to select your database as your current database. The following script shows
you how to select a database called "fyi". It also shows you how to put all the
database connection statements in a single include file, and re-use it in all of
your PHP pages.
Create the include file, connect.php, with the following statements:
<?php
$server = "localhost";
$username = "";
$password = "";
$database = "fyi";
$con = mysql_connect($server, $username, $password);
mysql_select_db($database);
?>
To test this database connection and selection include file, try the following
script:
<?php
include "mysql_connection.php";
$sql = 'SHOW TABLES';
if ($rs = mysql_query($sql, $con)) {
print(mysql_num_rows($rs) . " tables in the database.\n");
} else {
print("SHOW TABLES failed.\n");
}
mysql_close($con);
?>
You will get something like this:
0 tables in the database.
How To Run a SQL Statement?
You can run any types of SQL statements through the mysql_query() function.
It takes the SQL statement as a string and returns different types of data depending
on the SQL statement type and execution status:
- Returning FALSE, if the execution failed.
- Returning a result set object, if the execution is successful on a SELECT statement or other statement returning multiple rows of data.
- Returning TRUE, if the execution is successful on other statements.
Here is a good example of running a SQL statement with the mysql_query() function:
<?php
include "mysql_connection.php";
$sql = 'SELECT sysdate() FROM dual';
$rs = mysql_query($sql, $con);
$row = mysql_fetch_array($rs);
print("Database current time: ". $row[0] ."\n");
mysql_close($con);
?>
If you run this script, you will get something like this:
Database current time: 2006-02-26 21:34:57
(Continued on next part...)
Part:
1
2
3
4
5
6
7
|