Connect PHP with MySQL



 

 1.CONNECT PHP WITH MYSQL




Password: admin123



[root@localhost linuxpert]# cd /var/www/html

[root@localhost html]# gedit



type the following in gedit and save it as form.html



<html>

<head>

<title>LOGIN</title></head>

<body>

<form action="connect.php" method="post">

<p>

"Enter course no"<input type="text" name="cid"></p>

<p>"enter the coursename"<input type="text" name="cname"></p>

<p>"click here to submit"<input type="submit" name="submit"></p>

</form>

</body>



Type the following in gedit and save it as connect.php



<?php

$cid=$_POST['cid'];

$cname=$_POST['cname'];

$con=@mysql_connect("localhost","root","")or die (mysql-error());

echo "connected to database";

$db=@mysql_select_db("student",$con)or die(mysql_error());

echo "selected database";

$str="insert into courses values($cid,'$cname')";

$res=@mysql_query($str) or die(mysql_error());

if($res>0)

{

echo "Record created";

}

?>



TYPE THE FOLLOWING IN THE BROWSER





When press the submit button


 now go to terminal



[linuxpert@localhost ~]$ mysql -u root -p\

> > Enter password:



Welcome to the MySQL monitor.  Commands end with ; or \g. Your MySQL connection id is 28
Server version: 5.1.45 Source distribution



mysql> show databases;

+--------------------+

| Database         |

+--------------------+

| information_schema |

| mysql             |

| student          |

| test              |

+--------------------+

4 rows in set (0.11 sec)



mysql> use student ;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A Database changed


mysql> connect

Connection id:   29

Current database: student



mysql> show tables;

+-------------------+

| Tables_in_student |

+-------------------+

| course           |




| courses          |

| students        |

+-------------------+

3 rows in set (0.00 sec)



mysql> select * from courses;

+------+-------+

| cid  | cname |

+------+-------+

|   1 | java  |

|   1 | java  |

+------+-------+

2 rows in set (0.02 sec)

 php program to connect php with mysql database db | php program for linking php with database mysql

0 comments:

Post a Comment