Running Query SQL with PHP

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! 🙂

Oleh littleflow3r

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 loves to satisfied her shopping hobbies just via looking around in online shop. oh yeah, she got an A for her english subject^^

Tinggalkan Balasan

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.