Case Study : Running Query SQL with PHP

Requirements : Webserver, PHP, and MySQL already installed computer, respectively – each. (Tips: You can install the bundle as AppServ, EasyPHP or XAMPP instead of installing three of these features one by one). Here I use XAMPP 1.7.2

To run a query – I mean, SQL (mysql) query, we can do this in several ways. For example, through the command prompt, PHPMyAdmin, or with a PHP script. Here is an example to perform database operations SQL query with PHP.

Practice: Creating a database

1. Make sure the database named “db_buku” does not exist.

2. Open your favorite text editor, and type the following php script (you can use the default Windows notepad). Customize your username and password with your MySQL settings.

/ * Configuration * /
$host = "localhost"; // hostname
$user = "root"; //mysql user name
$pass = ""; // i use no password
/ * Connect to database * /
$connect = mysql_connect ($host, $user, $pass) or die ("Check
Connection ");
/ * Create a database named db_buku * /
if ($connect) {
echo "Connection successful";
$createdb = mysql_query ("CREATE DATABASE IF NOT EXISTS
db_buku ") or die (mysql_error ());
else {echo "Connection Failed";}

3. Save the script above as createDB.php, and save it to your document root directory (usually located in htdocs or www folder)

4. Open your browser, type http://localhost/ in the address bar.

You will see the list of your document root content, include your createDB.php file.

5. Click on the file createDB.php to run it. If successful, it will display a confirmation that the connection was successful.

6. Voila! You already have a database named db_buku. Please check with your mysql command prompt.

Tips: You can try to run another SQL query like create table, select, delete, and so forth by changing the sql query in the script above. For example:
$createdb = mysql_query("CREATE DATABASE IF NOT EXISTS db_buku ") or die (mysql_error ());

replace with,

/* Query for use db_buku */
$selectdb = mysql_select_db("db_buku") or die (mysql_error ());
/*Query to make tb_buku */
$createtabel = mysql_query ("CREATE TABLE tb_buku(
TOTAL INT(2 ))");

Happy coding! :)
Hal yang juga menarik:

Hak Cipta

Semua skrip dan teknik dalam artikel di boleh digunakan sebagaimana kehendakmu tanpa perlu mencantumkan sumber. Kamu tidak boleh mengkopi seluruh artikel, dalam Bahasa Indonesia ataupun diterjemahkan ke dalam bahasa lain.

Related Articles:

Artikel terkait:


About the author

obviously, a girl. with sweet smile, off course. turning her 21 years trapped on Informatics departement and just started to having some crush with it lately. she uses wordpress. she loves php. she lo

Tinggalkan Balasan

Alamat surel Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

x( X-( B-) ;-) :wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :D :?: :-| :-x :-o :-P :-D :-? :-)) :-) :-( :-& :) :( :!: 8-O 8-) 8) (Русский) (yahoo) (worship) (woot) (wave) (unsure) (tongue) (thinking) (tears) (taser) (smileydance) (sleeping) (sick) (scenic) (rofl) (rock) (party) (panic) (okok) (nottalking) (ninja) (music) (muscle) (muhaha) (money) (mmm) (lonely) (lol) (lmao) (idiot) (hungry) (highfive) (heart_beat) (heart) (headspin) (hassle) (haha) (gym) (griltongue) (goodluck) (girlkiss) (funkydance) (fish_hit) (eyeroll) (evilsmirk) (evil_grin) (drinking) (doh) (devil) (dance) (cry) (cozy) (coffee) (brokenheart) (bringit) (blush) (bigeyes) (beer) (banana_rock) (banana_ninja) (banana_cool) (applause) (annoyed) (angry) (K) (: