Just another PHP Basics tutorial for beginners - Part 2
- Type localhost on browser and select the phpmyadmin link
- Click create database
- Enter the name for your new database
- Create a new table in that database
- Enter the table name and number of fields you want
- Basically column datatypes wil be int or varchar
- Enter the table fields along with the datatypes and link save
- The default username and password the database is root and “”.
Database Connectivity
<?phpmysql_connect_db(“---hostname---“,”------database username----“,”-----database password----“);
mysql_select_db(“---database name—“);?>
For Example
<?php mysql_connect_db(‘localhost’,’root’,’’);
$connect = Mysql_select_db(‘test_db’);
if($connect)
{
echo “ db connected”;
}
else
{
echo “unable to connect db”;
}?>
PHP date and time
<?php $today= date(“Y-m-d”); // 2014-01-04
$time = date(“h-i-s”); // 12-04-28 ---- hours-mint-sec
$time=time(); //default system time
$range = date(“ h-i-s-y-m-d”); // 08-43-56-14-01-04 hour-min-sec-yr-month-date?>
PHP set and unset session
Session is mainly used for login and handle user actions. Before set and unset the session you have to start the session
<?php
session_start();
$_SESSION[‘username’] = "arunyokesh";
echo $_SESSION['username'];?>
OUTPUT: arunyokesh
Unset the session
<?php unset($_SESSION[‘username’]);
//or
$_SESSION[‘username’]=””;?>
PHP database insert
Database structure
╔═════════════════╦══════════════════════╗
║ usename ║ Password ║
╠═════════════════╬══════════════════════╬
║ ║ ║
║ ║ ║
║ ║ ║
║ ║ ║
║ ║ ║
║ ║ ║
╚═════════════════╩══════════════════════╝
index.php
<form action=’insert.php’ method=’post’ >
<label>username</label>
<input type=’text’ name=’username’>
<label>password</label>
<input type=’text’ name=’password’>
<input type=’submit’ name=’submit’ value=’submit’>
</form>
db_config.php
<?php
mysql_connect_db(‘localhost’,’root’,’’) or die();
mysql_select_db(“user_db”)or die();
?>
insert.php
<?php
include “dbconnect.php”; //including the db_connect file with this file
if($_POST) /// if post method came
{
if($_POST[‘submit’]) // if post method came with the name of submit
{
$name= $_POST[‘username’];
$pass=$_POST[‘password’];
$query = mysql_query(“INSERT INTO user_table (username,password) VALUES (‘$name’,’$pass’)”; // user_table is the the db table name
if($query)
{
echo “data inserted”;
}
else
{
echo “error in data insertion”;
}
}
}
?>
PHP database delete
Database structure
╔═════════════════╦══════════════════════╗
║ usename ║ Password ║
╠═════════════════╬══════════════════════╬
║ arun ║ password ║
║ kumar ║ kumar ║
║ ║ ║
║ ║ ║
║ ║ ║
║ ║ ║
╚═════════════════╩══════════════════════╝
delete.php
<?php
include “db_connect.php”;
//used to include that file with this current file
//u may also use require function
$delete_query = mysql_query(“delete from user_table where username=’arun ’ “);
if($delete_query)
{
echo “record deleted”;
}
else
{
echo “error occurred in deletion”;
}
?>
after deletion the database table will be
╔═════════════════╦══════════════════════╗
║ usename ║ Password ║
╠═════════════════╬══════════════════════╬
║ kumar ║ kumar ║
║ ║ ║
║ ║ ║
║ ║ ║
║ ║ ║
╚═════════════════╩══════════════════════╝
PHP Database Select / Display
display.php
<?php
include “db_connect.php”;
$query = mysql_query(“select * from user_table”); // * means all contents
while($row = mysql_fetch_array($query)) // while is used to fetch each row
{
echo $row[‘username’]; // db column name
echo<”<br/>”; //give line space while printing
echo $row[‘password’];
echo<”<br/>”; //give line space while printing
} // it will display all contents in the user_table
?>
Queries for selected columns
select username from user_table //for only usernameselect password from user_table // for only password column
OUTPUT :
kumar
kumar
PHP database Update
update.php
<?php
include “db_connect.php”;
$query=mysql_query(“update user_table set password=’abc’ where username =’kumar’”);
// where is used to give the condition in query
if($query) // if done
{
echo “data updated”;
}
else
{
echo “error occurred in updation”;
}
?>
after updation the database table will be
╔═════════════════╦══════════════════════╗
║ usename ║ Password ║
╠═════════════════╬══════════════════════╬
║ kumar ║ abc ║
║ ║ ║
║ ║ ║
║ ║ ║
║ ║ ║
╚═════════════════╩══════════════════════╝
Conclusion
I am happy to inform that you are learned the basic’s of PHP language , I hope you all understand ,if you really like this method give some suggestion to me for developing tutorials on other programming languages.
Thank you!!
Contact me :
Email : ayokesh.cse@gmail.com
Facebook : www.facebook.com/ayokesh
Reference
- PHP & MYSQL for dummies
- PHP.net
- W3schools.com
- PHP 6 A beginner’s Guide
If you like this article. click the applause button! 👏 Follow me on Github, Twitter, Facebook.