PHP SQL Script

PHP SQL Script is useful to perform the operation of SQL queries in PHP. The PHP is a server side scripting language and open source software.

PHP SQL Script

     

PHP SQL Script is useful to perform the operation of SQL queries in PHP. The PHP is a server side scripting language and open source software. The PHP supports many databases like MySQL, Oracle, Sysbase, Solid etc. 

Understand with Example

The Tutorial illustrate an example from 'PHP SQL Script'. To make use of sql query in PHP, we create sql_script.php begins with <?php and end with ?>. The <?php enclosed variable con which is assigned to the mysql_connect ( ) function that accept the parameter like "local host", "user" and password to execute the SQL query in PHP. We create a MySQL database which consists of a table "emp_counter" with three column that is id of integer type, name of varchar type , reset_counter of datetime type. We insert a values ('1', 'sandeep', 'SYSDATE()' ) in the table. The Select statement is used to return the records details from table 'emp_counter'. Finally the print is used to print the successful insertion of values into the table 'emp_counter' database.

Table: emp_counter

Source Code of sql_script.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);

  $drop_table = "DROP TABLE if exists emp_counter ";
  mysql_query($drop_table, $con);

  $create_query = "CREATE TABLE emp_counter (id INT NOT NULL AUTO_INCREMENT,  name VARCHAR(250), reset_counter DATETIME, PRIMARY KEY(id))";

  mysql_query($create_query, $con);
  print("Table Creation for <b>emp_counter</b> successful!<p>");

  $insert = "INSERT into emp_counter VALUES (1, 'sandeep', SYSDATE())";

  mysql_query($insert, $con);
  print("Inserted new counter successfully for this page");
?>

Download Source Code

 

Output: