PHP can connect to MySQL because it is a useful PHP module for it. Librarinya own module or file I do not know its contents, which is important we use that the library, in windows, library’s name is php_mysql.dll.
That the library consists of functions related to the MySQL databse connection, like mysql_connect (), mysql_select_db (), mysql_query (), mysql_fetch_row (), mysql_fetch_array () etc.. If the module is the rename or delete, then PHP can not communicate with MySQL.
If MySQL is on a local server, (in other words, there are on your own website), then the hostname used was localhost
sintax means that you use is:
mysql_connect ("localhost", "mysql_username_you_use", "password_from_username");
1. PHP & MySQL Connection
<?php
$ Hostmysql = "localhost";
$ Username = "mysqlusername";
$ Password = "mysqlpassword";
$ Database = "databasename";
$ Conn = mysql_connect ("$ hostmysql", "$ username", "$ password");
if (! $ conn) die ("Connection failed");
mysql_select_db ($ database, $ conn) or die ("Database not found");
?>
The explanation:
1. mysql_connect
used to make the connection from PHP to MySQL server. Data on the hostname, MySQL username, and password that is used has been represented by the variable $ hostmysql, $ username, $ password. Writing will be equal to:
mysql_connect ("localhost", "username", "password");
2. mysql_select_db
to select the database to be used.
3. if (! $ conn) die ("Connection failed");
if the connection failed to be created (! $ conn), it will display an error message
for more details go to http://php.net/manual/en/function.mysql-connect.php
Tip : Any operation related PHP with MySQL, would need a syntax like the above. for easier,
better saved first by the name db_config.php. If the syntax is needed again, then we do include the file db_config.php it.
2. Creating Tables in MySQL
<?php
include ("db_config.php");
mysql_query ("CREATE TABLE users (firstname VARCHAR (20), lastname VARCHAR (20), city VARCHAR (20 ))");
?>
The explanation:
1. include ("db-config.php");
include command is used to include a file (in the example above is the db-config.php file).
2. mysql_query
general format of this command is mysql_query (string query).
mysql_query will often be found in this article.
3. Entering data in the table
<?php
include ("db-config.php");
$ Insert = "INSERT INTO users (firstname, lastname, city) VALUES ('I', 'Own', 'Indonesia')";
mysql_query ($ insert) or die ("can not enter data into the table");
?>
4. Displaying data from a table
<?php
include ("db_config.php");
$ Query = "SELECT * FROM user";
$ Result = mysql_query ($ query);
$ Numrows = mysql_num_rows ($ result);
while ($ row = mysql_fetch_array ($ result))
{
echo "Number of data: $ numrows";
echo "Name: $ row [firstname]";
echo "Last Name: $ row [lastname]";
echo "City: $ row [city]";
}
?>
The explanation:
1. mysql_num_rows
used to calculate the number of lines drawn from the results of execution query (mysql_query).
2. while () {}
used for looping over the desired data is still there. (In the example above: will display all the contents of the table).
3. mysql_fetch_array
display data from a table in the form of arrays
For any other use (delete, update, etc.) that change only query strings only.