Wednesday, December 9, 2009

Basic Tutorial : PHP & MySQL

In this tutorial, we will try to do surgery - the basic operations on PHP MySQL.

1. Making connections
$ hostmysql = "localhost";
$ username = "mysqlusername";
$ password = "mysqlpassword";
$ database = "namadatabase";

$ conn = mysql_connect ( "$ hostmysql", "$ username", "$ password");
if (! $ conn) die ( "Connection failed");
mysql_select_db ($ database, $ conn) or die ( "Database not found");>

Script Explanation:
a. mysql_connect
used to make a connection from PHP to MySQL server. Data on the hostname, mysql username and password used was represented by the variable $ hostmysql, $ username, $ password. Writing will be equal to:
mysql_connect ( "localhost", "username", "password");
b. mysql_select_db
to select the database to be used.
c. if (! $ conn) die ( "Connection failed");
if the connection failed to be created ($ conn), then the error message will appear

Any operation related PHP with MySQL, will require the above syntax. make it easier, better kept prior konfig.php name. If the syntax is needed again, so we do include the file that konfig.php.

2. Create a table in MySQL
include ( "konfig.php");
mysql_query ( "CREATE TABLE user (
namadpnVARCHAR (20),
namablkg VARCHAR (20),
state VARCHAR (20 ))");?>

Explanation of script:
1. include ( "konfig.php");
command is used to include includes a file (in the example above is konfig.php file).
2. mysql_query
general format of this command is mysql_query (from the query string).
mysql_query will often be found in this article.

3. Entering data in the table
include ( "konfig.php");
$ insert = "INSERT INTO users (namadpn, namablkg, country)
VALUES ( 'I', 'Alone', 'Indonesia') ";
mysql_query ($ insert) or die ( "can not enter data into a table ");?>

4. Displaying data from tables
include ( "konfig.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 [namadpn]
";
echo "Name: $ row [namablkg]
";
echo "State: $ row [state]";
)?>

Explanation of script:

1. mysql_num_rows
used to calculate the number of rows obtained from the results of the execution of 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 tables in the form of an array

For any other use (delete, update, etc.) has changed only his query strings only.

No comments:

Post a Comment

semarang jawa tengah indonesia service keyboard service computer komputer kendal bali setting hotspot hacking password mysql protected username jasa setting jual beli
bobol password phpmyadmin debian 5 lenny ubuntu server surabaya sumatera american inggris access point microtic MikroTik jawa barat yamaha roland casio korg technic floppy disk emulator usb www universal cara ganti broadcast editing wireless Wi-Fi handphone novel health facebook Sepeda Fixie Jual Beli Sepeda Fixie Rose Network Sepeda Fixie Murah Wimax Wimax Indonesia, Long time waktu lorong waktu facebook twitter

Followers