The most viewed items
<?php
$server = 'localhost';
$username = 'root';
$password = '';
$db = 'TNW';
$conn = mysqli_connect($server, $username, $password, $db);
if($conn){
$query = "DROP TABLE `username`";
$query_run = mysqli_query($conn, $query);
if($query_run){
echo 'Successfully deleted a table.';
}else{
echo 'Deleting a table Error.';
}
}else{
echo 'Connecting to a database Error.';
}
?>
<?php
$server = 'localhost';
$username = 'root';
$password = '';
$db = 'TNW';
$conn = mysqli_connect($server, $username, $password, $db);
if($conn){
$query = "CREATE TABLE `username`(
UserId int(30) NOT NULL AUTO_INCREMENT PRIMARY KEY,
FirstName varchar(30) NOT NULL,
LastName varchar(30) NOT NULL,
UserName varchar(30) NOT NULL,
Password varchar(50) NOT NULL
)";
$query_run = mysqli_query($conn, $query);
if($query_run){
echo 'Successfully created a table.';
}else{
echo 'Creating a table Error.';
}
}else{
echo 'Connecting to a database Error.';
}
?>
<?php
$server = 'localhost';
$username = 'root';
$password = '';
$db = 'TNW';
$conn = mysqli_connect($server, $username, $password, $db);
if($conn){
echo 'Successfully connected to a database -' .$db. '.';
}else{
echo 'Connecting to a database Error.';
}
?>
<?php
$server = 'localhost';
$user = 'root';
$password = '';
$conn = mysqli_connect($server, $user, $password);
if($conn){
$query = "DROP DATABASE `TNW`";
$query_run = mysqli_query($conn, $query);
if($query_run){
echo 'Successfully deleted a Database.';
}else{
echo 'Deleting a database Error.';
}
}else{
echo 'Connection Error';
}
?>
<?php
$server = 'localhost';
$user = 'root';
$password = '';
$conn = mysqli_connect($server, $user, $password);
if($conn){
$query = "CREATE DATABASE `TNW`";
$query_run = mysqli_query($conn, $query);
if($query_run){
echo 'Successfully created a Database.';
}else{
echo 'Creating a database Error.';
}
}else{
echo 'Connection Error';
}
?>
The following is the example of connecting to database server in php.
<?php
$server = 'localhost';
$user = 'root';
$password = '';
$conn = mysqli_connect($server, $user, $password);
if($conn){
echo 'Successfull connected to the server.';
}else{
echo 'Connection Error.';
}
?>
We can set the limitations in uploading files. For example, we allow to upload only jpg or jpeg images and the maximum file size is 800KB.
<?php
@$name = $_FILES['file']['name'];
@$type = $_FILES['file']['type'];
@$size = $_FILES['file']['size'];
@$extension = substr($name, strpos($name, '.')+1);
@$max_size = 800000;
@$tmp_name = $_FILES['file']['tmp_name'];
@$location = 'tnw87/';
if(isset($name) && !empty($name)){
if($extension == 'jpg' || $extension == 'jpeg'){
if($size < $max_size){
if(move_uploaded_file($tmp_name, $location.$name)){
echo 'Successfully uploaded!';
}else{
echo 'Upload Errors!';
}
}else{
echo 'The maximum file size is 800KB';
}
}else{
echo 'Only jpg or jpeg image is allowed!';
}
}else{
echo 'Please choose a file to upload';
}
?>
<form action = "upload.php" method = "POST" enctype = "multipart/form-data">
<input type = "file" name="file"><br /><br />
<input type = "submit" value = "Submit">
</form>
In this example, we will upload file to the designated location. So, we will create a folder to store the files we've uploaded and give name to that folder. I give it as tnw87. Ok, lets start write the following codes and discover how it works.
<?php
$name = $_FILES['file']['name'];
$type = $_FILES['file']['type'];
$size = $_FILES['file']['size'];
$tmp_name = $_FILES['file']['tmp_name'];
if(@isset($name) && @!empty($name)){
$location = 'tnw87/';
if(move_uploaded_file($tmp_name, $location.$name)){
echo $name. ' is successfully uploaded.';
}else{
echo 'Upload Errors';
}
}else{
echo 'Please select file to upload';
}
?>
<style type = "text/css">
form{margin-top : 100px; margin-left : 50px;}
</style>
<form action = "upload.php" method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "file"></br></br>
<input type = "submit" value = "Upload">
</form>
When we upload a file into one location, the file is firstly saved in temporary location as a temp file. Ok, lets discover how it works. We will write the following codes and test it.
<?php
$name = $_FILES['file']['name'];
$type = $_FILES['file']['type'];
$size = $_FILES['file']['size'];
$tmp_name = $_FILES['file']['tmp_name'];
if(@isset($name) && @!empty($name)){
$location = 'tnw87/';
if(move_uploaded_file($tmp_name, $location.$name)){
echo $name. ' is successfully uploaded.';
}else{
echo 'Upload Errors';
}
}else{
echo 'Please select file to upload';
}
?>
<style type = "text/css">
form{margin-top : 100px; margin-left : 50px;}
</style>
<form action = "upload.php" method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "file"></br></br>
<input type = "submit" value = "Upload">
</form>
<?php
@$name = $_FILES['file']['name'];
@$type = $_FILES['file']['type'];
@$size = $_FILES['file']['size'];
if(@isset($name) && @!empty($name)){
echo 'File name is <strong> '.$name.'</strong>, File type is <strong>'.$type.'</strong> nad File size is <strong> '.$size.'</strong>';
}else{
echo 'Please select file to upload';
}
?>
<style type = "text/css">
form{margin-top : 100px; margin-left : 50px;}
</style>
<form action = "upload.php" method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "file"></br></br>
<input type = "submit" value = "Upload">
</form>
<?php
$time = time();
$time_now = date('D M Y & H:i:s', $time);
$time_modified = date ('D M Y @ H:i:s', strtotime('+ 1 year') );
echo 'The current time is '.$time_now.'.</br>The time modified is '.$time_modified.'.' ;
?>
<?php
$redirect_page = 'https://www.tnw87.com';
$redirect = true;
if($redirect == true){
header('location:' .$redirect_page);
}
?>