what is my sql ,how connect database in my sql,php database
mysql
MySQL is currently the most popular open source database server in
existence. On top of that, it is very commonly used in conjunction with PHP
scripts to create powerful and dynamic server-side applications.
MySQL has been criticized in the past for not supporting all the
features of other popular and more expensive DataBase Management Systems.
However, MySQL continues to improve with each release (currently version 5),
and it has become widely popular with individuals and businesses of many different
sizes.
what
is a database?
A database is a structure that comes in two flavors: a flat
database and a relational database. A relational database is much more oriented
to the human mind and is often preferred over the gabble-de-gook flat database
that are just stored on hard drives like a text file. MySQL is a relational
database.
In a relational structured database there are tables that store
data. The columns define which kinds of information will be stored in the
table. An individual column must be created for each type of data you wish to
store (i.e. Age, Weight, Height).
On the other hand, a row contains the actual values for these
specified columns. Each row will have 1 value for each and every column. For
example a table with columns (Name, Age, Weight-lbs) could have a row with the
values (Bob, 65, 165). If all this relational database talk is too confusing,
don't despair. We will talk about and show a few examples in the coming
lessons.
creating
your first database
Most web hosts do not allow you to create a database directly
through a PHP script. Instead they require that you use the PHP/MySQL
administration tools on the web host control panel to create these databases.
Create a database and assign a new user to this database. For all of our beginning
examples we will be using the following information:
- Server - localhost
- Database - test
- Table - example
- Username - admin
- Password - 1admin
Note:
The table may change in the advanced lessons, but everything else will remain
the same!
The server is the name of the server we want to connect to.
Because all of our scripts are going to be placed on the server where MySQL is
located the correct address is localhost. If the MySQL server was on a different machine
from where the script was running, then you would need to enter the correct url
(ask your web host for specifics on this).
mysql
connect
Before you can do anything with MySQL in PHP you must first
establish a connection to your web host's MySQL database. This is done with the
MySQL connect function.
mysql localhost
If you've been around the
internet a while, you'll know that IP addresses are used as identifiers for
computers and web servers. In this example of a connection script, we assume
that the MySQL service is running on the same machine as the script.
When the PHP script and MySQL are
on the same machine, you can uselocalhost as the address you wish to connect to. localhost is a shortcut to just have the machine
connect to itself. If your MySQL service is running at a separate location you
will need to insert the IP address or URL in place of localhost. Please contact your
web host for more details if localhost does not work.
PHP & MySQL Code:
mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
echo "Connected to MySQL
";
?>
If you load the above PHP script
to your webserver and everything works properly, then you should see
"Connected to MySQL" displayed when you view the .php page.
The mysql_connect function takes
three arguments. Server, username, and password. In our example above these
arguments were:
- Server - localhost
- Username - admin
- Password - 1admin
The "or die(mysql..."
code displays an error message in your browser if --you've probably guessed it
-- there is an error in processing the connection! Double-check your username,
password, or server if you receive this error.
choosing the working database
After establishing a MySQL
connection with the code above, you then need to choose which database you will
be using with this connection. This is done with the mysql_select_db function.
PHP & MySQL Code:
mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
echo "Connected to MySQL
";
mysql_select_db("test") or die(mysql_error());
echo "Connected to Database";
?>
Comments
Post a Comment
Thank you for your Comment