0% found this document useful (0 votes)
0 views5 pages

php-1

The document provides various PHP examples for connecting to a MySQL database, creating a database and table, inserting data, and selecting data. It also includes simple PHP programs for adding two numbers, calculating the factorial of a number, and determining if a number is odd or even. Each example includes code snippets and explanations of the functionality.

Uploaded by

pashupatidaas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
0 views5 pages

php-1

The document provides various PHP examples for connecting to a MySQL database, creating a database and table, inserting data, and selecting data. It also includes simple PHP programs for adding two numbers, calculating the factorial of a number, and determining if a number is odd or even. Each example includes code snippets and explanations of the functionality.

Uploaded by

pashupatidaas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

PHP mysql connect

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).

Next, it creates a new mysqli object called $conn which establishes a


connection to the MySQL server using the database configuration details.

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".

Finally, the script closes the connection by calling the $conn->close()


function.

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();
?>

Inserting into Table


<?php
$servername = "localhost";
$username ="username";
$password ="password";
$dbname ="database";
//creating connection
$conn = new mysqli($servername, $username, $password, $dbname);
// check connection
if ($conn->connect_error)
{
die("connection error");
}
// sql to insert record
$sql="insert into student11 (id,firstname,lastname,phoneno,email)
values('1,'ram,'thapa,'98564578,'ram@gmail.com)";
$result = $conn->query("$sql");
if ($result==true)
{
echo'new record added successfully';
}
else
{
echo'failed';
}
$conn->close();
?>
Select data from Table
<?php
$servername = "localhost";
$username ="username";
$password ="password";
$dbname ="database";
$conn=new mysqli($servername,$username,$password,$dbname);
if ($conn->connect_error){
die("connection error");
}
$sql = "select firstname,lastname,email,mark from student";
$result = $conn->query($sql);
if( $result->num_rows>0){

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>

You might also like