php print

In php print is used to display or output a string


print "Hello World"; // string are written inside double quotes

Php connect to mysql database

Create a new page and save as conn.php and paste the below stated code


<?php

function conectDB() {
$host = "localhost";
$username = "root";
$password = "";
$db = "Write your database name"; // Write your database name here
$connect = mysql_connect($host,$username,$password);
if(!$connect){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Write your database name") or die('Could not connect: ');
}
?>
 
Then we can include the conn.php on every pages that connects to mysql database

<?php
include "conn.php";
$dbHandle = conectDB();
?>

Php mysql database introduction

php mysql database introduction

Create database with php mysql

<?php

mysql_query('
CREATE TABLE IF NOT EXISTS `table_sample` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 75 ) NOT NULL,
`email` VARCHAR( 128 ) NOT NULL
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;');

?>

Insert Data Into a Database - php mysql tutorials

Simple coding for insert data into adatabase in php and mysql.

include "conn.php";

$fieldname1 = $_REQUEST['fieldname1"];
$fieldname2 = $_REQUEST['fieldname2'];
$fieldname3 = $_REQUEST['fieldname3'];


$query = sprintf("INSERT INTO databasename (fieldname1, fieldname2, fieldname3)
VALUES('%s', '%s', '%s')",
mysql_real_escape_string($fieldname1),
mysql_real_escape_string($fieldname2),
mysql_real_escape_string($fieldname3));

$result = mysql_query ($query, $connect);

Update Data In a Database - php mysql tutorials

The simple examples of how to update Data in a Database is

<?php

$Host = "localhost";
$User = "root";
$Pass = "";
$Name = "your databaseName";

$con = mysql_connect($Host, $User, $Pass);
$db = mysql_select_db($database_name, $con);

$sql = "UPDATE `table_name` SET `name` = 'Youname', `email` = 'emailid' WHERE `id` = '2';
$result = mysql_query($sql);
?>

Delete Data from Database - php mysql tutorials

The simple examples of how to delete data from Database is
<?php
$Host = "localhost";
$User = "root";
$Pass = "";

$Name = "your databaseName";
$con = mysql_connect($Host, $User, $Pass);

$db = mysql_select_db($Name, $con);
$sql = "DELETE FROM `table_name` WHERE `id` = '2' ";

//you can use this
//$sql = "DELETE FROM `table_name` WHERE `name` = 'Yourname'";

$result = mysql_query($sql);
?>

Select Data From a Database - php mysql tutorials

Select Data From a Database - php mysql tutorials

Php mysql order by clause

The ORDER BY clause is used to sort the data in ascending order(by default) or in descending order.

To sort the records in a ascending order, you can use the ASC keyword.
To sort the records in a descending order, you can use the DESC keyword.
Syntax
SELECT column_name FROM table_name ORDER BY column_name ASC|DESC

php mysql Where clause

Syntax
SELECT column_name FROM table_name WHERE column_name operator value

Go back to previous page using javascript

If you like to enable your visitors to go back to the previous page by clicking back button in your websites. use this code

Copy and paste this code into your HTML
<form>
<input type="button" value="Back to Previous Page" onClick="javascript: history.go(-1)">
</form>

Go Back to previous page with a simple link

<a href="javascript: history.go(-1)">Back</a>

How to upload joomla site to server using ftp?

1. Upload your newly created Joomla site to the server via FTP.
2. Create the MySql Database.
3. Edit the configuration.php file and set the newly created server database name, username, password correctly. U don't have to change the host name usually its "localhost" but if your server MySql is belong to a different IP u need to put that ip in the hostname.
4. upload/replace the configuration.php file.
5. Export your Local Joomla(PC) database (e.g using PhpMyadmin) and import the same in your Server Database.
Its all done......

simple CAPTCHA example in PHP

A CAPTCHA is a challenge response test used on computers to check if the user is human.A common kind of CAPTCHA that is used on websites requires that the visitor type the letters and numbers of a distorted image. This method is based on the fact that is difficult for computers to extract the text from the image while it is very easy for humans.


put a small  image in the same directory where both file exist then the image name must be img.jpg

Send Mail with Submitted Form Data and a File Attachment in php

The given PHP example helps you to send the mail with submitted form data and file attachment.The attached file should be .doc or .txt (you can modify it).You only need to change the 'To' email address from sendmail.php file.

Send an Email with Multiple Attachments in PHP

The below PHP example helps you to send an email with multiple attachment file.

sendmail.html

How to redirect a Page Using Javascript in php

If you don't like using header() as I like to redirect page mid-page. This is implemented using
Javascript written through PHP function. Has a die() function at the end for people who has
disabled their Javascript in their browser.

<?php
function redirect($new_location){
   echo ("<script type='text/javascript'>
   document.location.replace('$new_location');
   </script> ");
   die();
}
?>

current Yahoo userid status in php

The given PHP example returns the current Yahoo status(online/offline) against a yahoo userid.

<?php
function yahoo($id){
    $url = 'http://opi.yahoo.com/online?u=';
    $data = file_get_contents($url . $id);
    if (trim(strtolower(strip_tags($data))) != 'user not specified.') {
        return (strlen($data) == 140) ? 'online' : 'offline';
    } else {
    return trim(strip_tags($data));
    }
    }
echo yahoo("yahoo_userid");
?>

How to make alternate row colours in php

The given PHP example helps you to display the row with alternate colours.

<?php
$array_data[] = array("ID", "Name", "Email");
$array_data[] = array("1", "Bill", "bill@phpmoot.com");
$array_data[] = array("2", "Jan", "jan@test.com");
$array_data[] = array("3", "Imran", "imran@phpmoot.com");
$array_data[] = array("4", "Qasim", "qasim@test.com");
$array_data[] = array("5", "phpMoot", "test@phpmoot.com");
// Header rows
print ("<table border='1' width='100%'>");
$alternate = "2"; // number of alternating rows
foreach($array_data as $key => $val){
if ($alternate == "1") {
$colour = "#66CCFF";
$alternate = "2";
}
else {
$colour = "#CCCCFF";
$alternate = "1";
}

print ("<tr bgcolor='$colour'><td>".$val[0]."</td><td>".$val[1]."</td><td>".$val[2]."</td></tr>
");
}
print ("</table>");
?>

PHP – Multiple File Upload on server

The given PHP example helps you to upload the multiple files on server at once.


<?php

$file_to_upload = 5;
$file_dir  = "./"; // directory where you want to upload the files, and don't forget to CHMOD 777 to this folder
if ($_POST) {
  for ($i=0;$i<$file_to_upload;$i++) {
   if (trim($_FILES['myfiles']['name'][$i])!="") {
     $newfile = $file_dir.$_FILES['myfiles']['name'][$i];
     move_uploaded_file($_FILES['myfiles']['tmp_name'][$i], $newfile);
     $j++;
   }
  }
}
if (isset($j)&&$j>0) print "Your file(s) has been uploaded.<br>";
print "<form method='post' enctype='multipart/form-data'>";
for($i=0;$i<$file_to_upload;$i++) {
  print "<input type='file' name='myfiles[]' size='30'><br>";
}
print "<input type='submit' name='action' value='Upload'>";
print "</form>";
?>

A Simple Password Authentication Form - PHP

The given PHP example helps you to make a simple password authentication form.
<?php
if (!empty($loginme)) {
  $connection = mysql_pconnect($dbhost='localhost',$dbuser='root',$dbpass='root');
  mysql_select_db($database='test',$connection);
  $query = "SELECT id FROM customer WHERE email='".mysql_real_escape_string($admin)."' AND password='".mysql_real_escape_string($password)."'";
  $result = mysql_query($query, $connection);
  $num = mysql_num_rows($result);
  if ($num > 0){
     header("Location: home.php"); // do what you want
     exit;
  }
  else{
     echo "username and password are either incorrect or not in the database";
  }
}
?>
<form method="post" action="">
Username: <input type="text" name="admin"><br>
Password: <input type="password" name="password"><br>
<input type="submit" name="loginme" value="Login Me">
</form>
Related Posts Plugin for WordPress, Blogger...