Secret mind

hi Friends, I am Aman gupta and a web developer. I learned many languages such as java, javaScript, python, clang, php, jquery, mysql database, Html and css and many more just to make a perfect and wonderfully website. Here i will upload related post of PHP, Secret of world, computer science, Feelings, GK, And Question paper.

Trending video

Responsive Ads Here

السبت، 28 ديسمبر 2019

mysql database with php

mysql database with php





1.Create database with php

Open a Connection to MySQL

Before we can access data in the MySQL database, we need to be able to connect to the server:

<?php
$servername = "localhost";
$username = "root";
$password = "";


// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "connection succesfully";

$conn->close();

?>


2.Create a database:

<?php
$servername = "localhost";
$username = "root";
$password = "";


// Create connection
$conn = new mysqli($servername,$username,$password);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "Create DATABASE demo";

if ($conn->query($sql) === TRUE) {
    echo "database has been cerated";
} else {
  echo "database has been not craeted".$conn->connect_error;
}

$conn->close();

?>

3. Create table with php:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "demo";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// sql to create table
$sql = "CREATE TABLE aman (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";

if ($conn->query($sql) === TRUE) {
    echo "Table aman created successfully";
} else {
    echo "Error creating table: " . $conn->error;
}
$conn->close();
?>

4. Insert single line data with php:
<?php
$servername = "localhost";
$username = "root";
$password = "p";
$dbname = "demo";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO aman (firstname, lastname, email)

VALUES ('Aman', 'Gupta', 'gaman5088@gmail.com')";
if ($conn->query($sql) === TRUE) {

    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>

insert multiple data in database with php:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "demo";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO aman (firstname, lastname, email)
VALUES ('varun', 'meena', 'varun@example.com');";
$sql .= "INSERT INTO aman (firstname, lastname, email)
VALUES ('vandita', 'rajput', 'vandita@example.com');";
$sql .= "INSERT INTO aman (firstname, lastname, email)
VALUES ('shubham', 'podder', 'shubham@example.com')";
if ($conn->multi_query($sql) === TRUE) {
    echo "New records created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>

output:-

Aman gupta

6.PHP MySQL Select Data:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "demo";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, firstname, lastname FROM aman";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

output:-




7. Insert data in database from html form:
<!DOCTYPE html>
<html>
<head>
<title>form </title>
</head>
<body>
<form action="" method="POST">

fName:
<input type="text" name="name">
<br><br>
lname <input type="text" name="lname"><br><br>

Email:<input type="Email" name="Email"><br><br>
<input type="submit" name="submit">
</form>
<?php
$ser="localhost";
$user="root";
$pass ="";
$dbname="demo";

$cn = new mysqli($ser,$user,$pass,$dbname);

if ($cn->connect_error) {

die("connection failed".$connect_error);
}
$sql = "INSERT INTO aman ( firstname,lastname,email) VALUES ('".$_POST["name"]."','".$_POST["lname"]."','".$_POST["Email"]."')";

if ($cn->query($sql)=== TRUE) {

   echo"<script> alert('new record enter successfylly');</script>";
# code...
}else
{
echo "<script>alert('error:".$sql."<br>".$cn->error."');</script>";
}
$cn->close();
 ?>
</body>
</html>


output:





هناك تعليقان (2):

Please do not enter any spam link in the comment box.