Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Sql Mysql-example PHP SQL Script

Related Tutorials


 
 

Share on Google+Share on Google+

PHP SQL Script

Advertisement
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:

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 4, 2009

Related Tutorials

Discuss: PHP SQL Script  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Dinesh
May 11, 2011
SQL in PHP

Hi, It's good tutorial on using SQL query in PHP. The PHP and MySQL is good tool for creating web applications. There are many frameworks in PHP which helps the programmer to develop complex websites and web applications. Thanks
DMCA.com