Showing posts with label tutorial. Show all posts
Showing posts with label tutorial. Show all posts

for loop

for loop is to run your script in many times

example

for ($i=0;$i<10;$i++){
echo $i;

}

//the output will be like this
0123456789

if else condition

if ($today =="sunday"){
echo "enjoy your holiday";
}
else {
echo "have a nice day";
}

Create a Table

we use CREATE TABLE statement to create table in mysql

//sql statement to create table
$sql="CREATE TABLE (column_name1 data_type,
column_name2 data_type,
column_name3 data_type) ";

example:

$con = mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

// Create database
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}

// Create table
mysql_select_db("my_db", $con);
$sql = "CREATE TABLE customers
(
Name varchar(15),
designation varchar(15),
Age int
)";

// Execute query
mysql_query($sql,$con);

?>

Create the database

To create the database, we need to use mysql_query function and CREATE DATABASE statetment

$conn = mysql_connect("localhost","root","root");
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}

if (mysql_query("CREATE DATABASE my_db",$conn))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}

?>

connect php to mysql

Normally the database that used in PHP langguange is MYSQL..MYSQL is also the open-source software.Below is the example of php-mysql connection

mysql_connect(servername,username,password);

$conn = mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

?>

php variable

A variable is an area of memory which is set aside to store information , this is assigned an identifier by the programmer . You can recognise variables in PHP because they are prefixed with the dollar ( $ ) sign . To assign a variable you use the assignment operator ( = ) . Here is an example of this.

$name = "myname";
$age = 45;