PHP MySQL Select Query

PHP mysql_query() function is used to execute select query. Since PHP 5.5, mysql_query() function is deprecated. Now it is recommended to use one of the 2 alternatives.

  • mysqli_query()
  • PDO::__query()

There are two other MySQLi functions used in select query.

  • mysqli_num_rows(mysqli_result $result): returns number of rows.
  • mysqli_fetch_assoc(mysqli_result $result): returns row as an associative array. Each key of the array represents the column name of the table. It return NULL if there are no more rows.

PHP MySQLi Select Query Example

Example

 

<?php  
$host = 'localhost:3306';  
$user = '';  
$pass = '';  
$dbname = 'test';  
$conn = mysqli_connect($host, $user, $pass,$dbname);  
if(!$conn){  
  die('Could not connect: '.mysqli_connect_error());  
}  
echo 'Connected successfully<br/>';  
  
$sql = 'SELECT * FROM emp4';  
$retval=mysqli_query($conn, $sql);  
  
if(mysqli_num_rows($retval) > 0){  
 while($row = mysqli_fetch_assoc($retval)){  
    echo "EMP ID :{$row['id']}  <br> ".  
         "EMP NAME : {$row['name']} <br> ".  
         "EMP SALARY : {$row['salary']} <br> ".  
         "--------------------------------<br>";  
 } //end of while  
}else{  
echo "0 results";  
}  
mysqli_close($conn);  
?> 

Output:

Connected successfully
EMP ID :1 
EMP NAME : ratan 
EMP SALARY : 9000 
--------------------------------
EMP ID :2 
EMP NAME : karan 
EMP SALARY : 40000 
--------------------------------
EMP ID :3 
EMP NAME : jai 
EMP SALARY : 90000 
--------------------------------

PHP MySQL Create Database

Since PHP 4.3, mysql_create_db() function is deprecated. Now it is recommended to use one of the 2 alternatives.

  • mysqli_query()
  • PDO::__query()

PHP MySQLi Create Database Example

Example

<?php  
$host = 'localhost:3306';  
$user = '';  
$pass = '';  
$conn = mysqli_connect($host, $user, $pass);  
if(! $conn )  
{  
  die('Could not connect: ' . mysqli_connect_error());  
}  
echo 'Connected successfully<br/>';  
  
$sql = 'CREATE Database mydb';  
if(mysqli_query( $conn,$sql)){  
  echo "Database mydb created successfully.";  
}else{  
echo "Sorry, database creation failed ".mysqli_error($conn);  
}  
mysqli_close($conn);  
?>  

Output:

Connected successfully
Database mydb created successfully.

Install PHP

To install PHP, we will suggest you to install AMP (Apache, MySQL, PHP) software stack. It is available for all operating systems. There are many AMP options available in the market that are given below:

  • WAMP for Windows
  • LAMP for Linux
  • MAMP for Mac
  • SAMP for Solaris
  • FAMP for FreeBSD
  • XAMPP (Cross, Apache, MySQL, PHP, Perl) for Cross Platform: It includes some other components too such as FileZilla, OpenSSL, Webalizer, Mercury Mail, etc.

If you are on Windows and don’t want Perl and other features of XAMPP, you should go for WAMP. In a similar way, you may use LAMP for Linux and MAMP for Macintosh.

Download and Install WAMP Server

Click me to download WAMP server

Download and Install LAMP Server

Click me to download LAMP server

Download and Install MAMP Server

Click me to download MAMP server

Download and Install XAMPP Server

Click me to download XAMPP server

How to install XAMPP server on windows

We will learn how to install the XAMPP server on windows platform step by step. Follow the below steps and install the XAMPP server on your system.

Step 1: Click on the above link provided to download the XAMPP server according to your window requirement.

Install PHP

Step 2: After downloading XAMPP, double click on the downloaded file and allow XAMPP to make changes in your system. A window will pop-up, where you have to click on the Next button.

Install PHP

Step 3: Here, select the components, which you want to install and click Next.

Install PHP

Step 4: Choose a folder where you want to install the XAMPP in your system and click Next.

Install PHP

Step 5: Click Next and move ahead.

Install PHP

Step 6: XAMPP is ready to install, so click on the Next button and install the XAMPP.

Install PHP

Step 7: A finish window will display after successful installation. Click on the Finish button.

Install PHP

Step 8: Choose your preferred language.

Install PHP

Step 9: XAMPP is ready to use. Start the Apache server and MySQL and run the php program on the localhost.

How to run PHP programs on XAMPP, see in the next tutorial.

Install PHP

Step 10: If no error is shown, then XAMPP is running successfully.

Install PHP

ER Model to Relational Model

As we all know that ER Model can be represented using ER Diagrams which is a great way of designing and representing the database design in more of a flow chart form.

It is very convenient to design the database using the ER Model by creating an ER diagram and later on converting it into relational model to design your tables.

Not all the ER Model constraints and components can be directly transformed into relational model, but an approximate schema can be derived.

So let’s take a few examples of ER diagrams and convert it into relational model schema, hence creating tables in RDBMS.


Entity becomes Table

Entity in ER Model is changed into tables, or we can say for every Entity in ER model, a table is created in Relational Model.

And the attributes of the Entity gets converted to columns of the table.

And the primary key specified for the entity in the ER model, will become the primary key for the table in relational model.

For example, for the below ER Diagram in ER Model,

ER model to Relational - Entity to Table

A table with name Student will be created in relational model, which will have 4 columns, idnameageaddress and id will be the primary key for this table.


Relationship becomes a Relationship Table

In ER diagram, we use diamond/rhombus to reprsent a relationship between two entities. In Relational model we create a relationship table for ER Model relationships too.

In the ER diagram below, we have two entities Teacher and Student with a relationship between them.

ER diagram relationship into table in rdbms

As discussd above, entity gets mapped to table, hence we will create table for Teacher and a table for Student with all the attributes converted into columns.

Now, an additional table will be created for the relationship, for example StudentTeacher or give it any name you like. This table will hold the primary key for both Student and Teacher, in a tuple to describe the relationship, which teacher teaches which student.

If there are additional attributes related to this relationship, then they become the columns for this table, like subject name.

Also proper foriegn key constraints must be set for all the tables.


Points to Remember

Similarly we can generate relational database schema using the ER diagram. Following are some key points to keep in mind while doing so:

  1. Entity gets converted into Table, with all the attributes becoming fields(columns) in the table.
  2. Relationship between entities is also converted into table with primary keys of the related entities also stored in it as foreign keys.
  3. Primary Keys should be properly set.
  4. For any relationship of Weak Entity, if primary key of any other entity is included in a table, foriegn key constraint must be defined.