PHP and MySQL primer

PHP and MySQL primer

This article is for InCTF'12.
Update: My team Eulerians made it to the finals! 😀
Update2: My team came 6th(All India) in the final event 🙂


Howdy! This is a PHP and MySQL primer. It assumes that you have installed Apache, PHP and MySQL.


PHP is a general-purpose server-sidescripting language originally designed for web development to produce dynamic web pages.

A simple hello world program in PHP:

echo "Hello World!";

Using PHP with MySQL

PHP is a great scripting language but for developing scalable projects you'll need a database. In this case I am going to show you how to connect and use PHP with a MySQL database.

Connect to a database

mysql_connect("localhost", "username", "password") or die(mysql_error());
echo "Connection to MySQL successful!";

Selecting a database

Once we are connected to MySQL we need to connect with the database we are going to be working on.

mysql_select_db("databasename") or die(mysql_error());
echo "Connected to database!"

Executing SQL queries

Okay great! We have connected to MySQL and we are connected to our database! Now what? Well now you can execute SQL queries to interact with the database.

For execution of a SQL query on the active database PHP provides a function


The function accepts an SQL statement as its parameter

For example here is a code that executes SQL code for creating a table

     CREATE TABLE test(
         name VARCHAR(45),
         PRIMARY KEY(id)

Well so thats a wrap!

About Ganesh

Page with Comments

  1. excellent ganesh!! 🙂
    thanks for sharing such article!! this will surely help me in some way!!! 🙂
    and keep updating articles like this!

Leave a Reply

Your email address will not be published. Required fields are marked *