Mysql Database:: How To Connect To Databse
Mysql Database:: How To Connect To Databse
<?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);
}
$conn->close();
?>
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE testdb";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
}
else
{
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
$conn->close();
?>
How to get data from database table:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password,"testdb");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password,"testdb");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "UPDATE myguest SET firstname='glen' WHERE id='1'";
<title></title>
<script type="text/javascript">
function validate()
{
if(document.getElementById("t1").value>100)
{
alert("ID value should be less than 100");
}
else if(document.getElementById("t2").value.length<6)
{
alert("name length should minimum of 6 characters");
}
else
{
document.getElementById("form1").submit();
}
}
</script>
</head>
<body>
<div>
<form id="form1" action="store.php" method="get">
<label>ID:</label><input type="text" id="t1" name="t1" placeholder="id should be
less than 100"/><br><br>
<label>Name:</label><input type="text" id="t2" name="t2" placeholder="name
should not be less than 6 characters"/><br><br>
<button type="button" onclick="validate()">submit</button>
</form>
</div>
</body>
</html>
Store.php:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";
$id=(int)$_GET["t1"];
$uname=$_GET["t2"];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
Cookies:
What is a Cookie?
A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer.
Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can
both create and retrieve cookie values.
Syntax
setcookie(name, value, expire, path, domain, secure, httponly);
Only the name parameter is required. All other parameters are optional.
Create cookie:
Example:
Setcookie.php:
<?php
//set cookie using setcookie() method
setcookie("user","jordan",time()+86400,"/");
echo "<a href='getcookie.php'>Go to getcookie.php</a>";
?>
getcookie.php:
<?php
if(isset($_COOKIE["user"]))
{
//getting cookie value using its name
echo "<h1>The cookie value :</h1>".$_COOKIE["user"];
}
else
{
echo "cookie is not set";
}
?>
Deletecookie.php:
<?php
//to delete cookie
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'user' is deleted.";
?>
</body>
</html>
Sessions:
A session is a way to store information (in variables) to be used across multiple pages.
Session variables solve this problem by storing user information to be used across multiple pages (e.g.
username, favorite color, etc). By default, session variables last until the user closes the browser.
So; Session variables hold information about one single user, and are available to all pages in one application.
Note: The session_start() function must be the very first thing in your document. Before any HTML tags.
Createsession.php
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
<html>
<body>
<?php
?>
</body>
</html>
destroysession.php:
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// remove all session variables
session_unset();
</body>
</html>
$GLOBALS
$_SERVER
$_REQUEST
$GLOBALS:
Example:
<?php
$x=10;
$y=20;
function callme()
{
echo "accessed from function:".$GLOBALS['x'];
}
echo "ADD:".($x+$y)."<br>";
callme();
?>
$_SERVER:
<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['REQUEST_METHOD'];
echo "<br>";
echo $_SERVER['SERVER_PORT'];
?>
Output:
/SuperGlobalVariables/servervariable.php
localhost
localhost
/SuperGlobalVariables/servervariable.php
Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100
Safari/537.36
GET
80
$_REQUEST:
<?php
$uname=$_REQUEST["t1"]; // t1 is url parameter
$id=$_REQUEST["t2"]; //t2 is url parameter
echo "hai $uname welcome to new world";
?>