Php Sql Table


 

Php Sql Table

 This example illustrates how to create table in php.

 This example illustrates how to create table in php.

Php Sql Table

 This example illustrates how to create table in php.

In this example we create a table "emp_table" with three fields, "emp_id", "emp_name" and "emp_designation". The "emp_id" is integer type, not null, auto increment and primary key define. The "emp_name" is varchar type and the "emp_designation" is also the varchar type define. We insert three value by the insert query. Finally we fetched all rows and columns by select query.

 

Table: emp_table

 

Source Code of sql_table.php 

<?php
  $mysql_db = "test";
  $mysql_user = "root";
  $mysql_pass = "root";
  $con = mysql_connect("localhost", $mysql_user, $mysql_pass);
  mysql_select_db($mysql_db, $con);

  $create_query = "CREATE TABLE emp_table (emp_id INT NOT NULL AUTO_INCREMENT, 
    emp_name VARCHAR(50),  emp_designation VARCHAR(50), PRIMARY KEY (emp_id))"
;
  mysql_query($create_query, $con);
  echo "Table <b>emp_table</b> Created Successfully!<br>";

  $insert = "INSERT INTO emp_table VALUES (1, 'sandeep', 'programmer')";
  $insert1 = "INSERT INTO emp_table VALUES (2, 'suman', 'sr. Gr Designer')";
  $insert2 = "INSERT INTO emp_table VALUES (3, 'ravi', 's/w developer')";
  mysql_query($insert, $con);
  mysql_query($insert1, $con);
  mysql_query($insert2, $con);
  echo "Data inserted successfull<br><br>";

  $result = mysql_query("SELECT * FROM emp_table");
  echo "<table border='1'>
    <tr>
      <th>ID</th>
      <th>Name</th>
      <th>Designation</th>
    </tr>";
    while ($row = mysql_fetch_array($result)) {
      echo "<tr>";
      echo "<td>" . $row['emp_id'] "</td>";
      echo "<td>" . $row['emp_name'] "</td>";
      echo "<td>" . $row['emp_designation'] "</td>";
      echo "</tr>";
    }
  echo "</table>";

  mysql_close($con);
?> 

Download Source Code

 

Output:

Ads