Sunteți pe pagina 1din 5

14. Program to create a New Database using PHP and Mysql.

<?php
if(!mysql_connect('servername','username','password'))
{
die('Connection failed!'.mysql_error());
}
$sql = "create database newdb";
if(mysql_query($sql))
{
echo "Database created";
}
else
{
echo mysql_error();
}
?>
------------------------------------------------------------Note: When we are using XAMPP or WAMP,
servername = localhost, username = root, password is empty.
15. Program to connect to the server and selecting database.
<?php
if(!mysql_connect('servername','username','password'))
{
die('Connection failed!'.mysql_error());
}
if(!mysql_select_db("dbname"))
{
die('Database unavailable'.mysql_error());
}
?>
------------------------------------------------------------Note: When we are using XAMPP or WAMP,
servername = localhost, username = root, password is empty.
16. Program to Insert records into the table in Database.
<?php

if(!mysql_connect('servername','username','password'))
{
die('Connection failed!'.mysql_error());
}
if(!mysql_select_db("dbname"))
{
die('Database unavailable'.mysql_error());
}
$sql = "INSERT INTO tablename('sno','name','pwd') VALUES('101','Surya','surya123')";
if(mysql_query($sql))
{
echo "Record Inserted";
}
else
{
echo mysql_error();
}
?>
------------------------------------------------------------Note: When we using XAMPP or WAMP,
servername = localhost, username = root, password is empty.
17. Program to fetch records from the table in Database.
<?php
if(!mysql_connect('servername','username','password'))
{
die('Connection failed!'.mysql_error());
}
if(!mysql_select_db("dbname"))
{
die('Database unavailable'.mysql_error());
}
$sql = "SELECT * FROM tablename";
$data

= mysql_query($sql);

while($rec = mysql_fetch_row($data))
{
echo "$rec[0]<br>";
echo "$rec[1]<br>";
echo "$rec[2]<br>";
}
?>
------------------------------------------------------------Note: When we are using XAMPP or WAMP,
servername = localhost, username = root, password is empty.
18. Program to Store a image in Database.
<?php
if(isset($_POST['sub']))
{
$cont = file_get_contents($_FILES['f1']['tmp_name']);
$type = $_FILES['f1']['type'];
$cont=addslashes($cont);
mysql_connect("servername","username","password");
mysql_select_db("dbname");
$sql="insert into tablename values ('','$cont','$type')";
if(mysql_query($sql))
echo "Inserted";
else
echo mysql_error();
}
?>
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="f1">
Image : <input type="submit" value="Upload" name="sub">
</form>
------------------------------------------------------------Note: When we using XAMPP or WAMP,
servername = localhost, username = root, password is empty.
19. Program to Read image from Database.
<?php
header("content-type:image/jpeg");

mysql_connect("servername","username","password");
mysql_select_db("databasename");
$sql = "select * from tablename";
$data = mysql_query($sql);
while($rec=mysql_fetch_row($data))
{
echo "$rec[1]<br>";
}
?>
------------------------------------------------------------Note: When we using XAMPP or WAMP,
servername = localhost, username = root, password is empty.
20. Program to create a simple Registration form.
registration.php
<html>
<head>
</head>
<body>
<h4>Registration Form</h4>
<form method="post" action="insert.php">
No :<input type="text" name="sno"><br>
Name :<input type="text" name="name"><br>
User name :<input type="text" name="uname"><br>
Password :<input type="password" name="pwd"><br>
<input type="submit" value="Register">
</form>
</body>
</html>
insert.php
<?php
if(!mysql_connect('servername','username','password'))
{
die('Connection failed!'.mysql_error());
}
if(!mysql_select_db("dbname"))
{
die('Database unavailable'.mysql_error());

}
$sno = $_POST['sno'];
$name = $_POST['name'];
$uname = $_POST['uname'];
$pwd = $_POST['pwd'];
$sql = "INSERT INTO tablename(sno, name, uname, pwd) VALUES('$sno', '$name','$uname',
'$pwd')";
if(mysql_query($sql))
{
echo "Registerd Successfully";
}
else
{
echo mysql_error();
}
?>
------------------------------------------------------------Note: When we using XAMPP or WAMP,
servername = localhost, username = root, password is empty.

S-ar putea să vă placă și