php-1
php-1
Q)How can you connect a database with PHP? Demonstrate with an example.
<?php
$servername = "localhost";
$username ="your_username";
$password ="your_password";
$dbname ="your_databse";
//creating connection
$conn = new mysqli($servername, $username, $password, $dbname);
// check connection
if ($conn->connect_error)
{
die("connection error");
}
else{
echo "connected successfully";
}
$conn->close();
?>
It starts by defining the database configuration details, including the
server name (localhost), username (your_username), password (your_password),
and database name (your_database).
After creating the connection, the script checks if the connection was
successful or not using an if statement. If the connection fails, the script
will output an error message and stop the script execution by calling the
die() function. Otherwise, if the connection is successful, it will output a
message saying "Connected successfully".
Creating Databse
<?php
$servername ="localhost";
$username ="username";
$password ="password";
//creating connection
$conn = new mysqli($servername, $username, $password);
// check connection
if($conn->connect_error)
{
die("connection error");
}
// sql to create database
$sql = "create database student";
$result = $conn->query($sql);
if($result == true)
{
echo "database created successfully";
}
else
{
echo "error creating database";
}
$conn->close();
?>
Creating Table
<?php
$servername = "localhost";
$username ="username";
$password ="password";
$dbname ="database_name";
//creating connection
$conn = new mysqli($servername, $username, $password, $dbname);
// check connection
if($conn->connect_error)
{
die("connection error");
}
// sql to create table
$sql = "create table student(id int,firstname varchar(50),
lastname varchar(50),phoneno int,email varchar(50))";
$result = $conn->query($sql);
if ($result == true)
{
echo "table created successfully";
}
else
{
echo "error creating table";
}
$conn->close();
?>
while($row=$result->fetch_assoc()){
echo "firstname" . $row["firstname"] . "lastname" . $row["lastname"] . "mark"
. $row["mark"] . "email" . $row["email"] . "<br>";
}
}
else {
echo"0 results";
}
$conn->close();
?>
PHP program to add two numbers
<html>
<head>
<title>sum</title>
</head>
<body>
<form method="post">
First number:
<input type="text" name="n1">
Second number:
<input type="text" name="n2">
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit']))
{
$a = $_POST['n1'];
$b = $_POST['n2'];
$c = $a+$b;
echo "sum is ".$c;
}
?>
</body>
</html>
Factorial of number using PHP
<html>
<head>
<title>fact</title>
</head>
<body>
<form method="post">
<input type="text" name="n">
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST["submit"]))
{
$n = $_POST['n'];
$i;
$f=1;
for($i= 1;$i<=$n;$i++)
{
$f= $f*$i;
}
echo "factorial is".$f;
}
?>
</body>
</html>
Find if a number is odd or even number using PHP
<html>
<head>
<title>sum</title>
</head>
<body>
<form method="post">
Enter a number:
<input type="text" name="n1" >
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit']))
{
$a = $_POST['n1'];
if ($a % 2 == 0) {
echo $a . " is even";
} else {
echo $a . " is odd";
}
}
?>
</body>
</html>