Search This Blog

Saturday 14 November 2015

Get comma separated column values from database using php

<?php

$con=mysql_connect('localhost','root','');

mysql_select_db('db',$con);

$result = mysql_query("SELECT * FROM  posts");

while($rows = mysql_fetch_array($result)) {

   $title=explode(',', $rows['title']);

   foreach($title as $row) {

      echo $row."<br>";

   }

}

?>

Saturday 31 October 2015

Get a date after 10 days from current date in PHP.

<?php

 echo date('M d, Y', strtotime('+10 days') );

?>

Sunday 4 October 2015

Update check box value into database using php.

<?php

if(isset($_POST['submit']))

{

mysql_query("update tablename set clname=value where clname=value");
}
?>
<table>
<tr>
<td><form method="post" action="">

<input type="checkbox" name="name" value="1"<?php if($data['name'] == '1') echo 'checked'; ?>/>

<input type="submit" name="submit" >

</form></td></tr>

</table>

Delete multiple rows from mysql with checkbox in php.

if(isset($_POST['Submit']))

{

foreach($_POST['check'] as $val1)
{
mysql_query("delete from tablename where id=".$val1."");

}

}

?>

<?php

$rec=mysql_query("select * from table name");

while($row=mysql_fetch_array($rec))

{

?>

<input type="checkbox" name="check[]" value="<?php echo $row['id'];?>" />

<?php }?>

<input type="submit" name="Submit" value="Delete" />

Monday 2 February 2015

Convert the first character of "welcome" to uppercase.

The ucfirst () function converts the first character of a string to uppercase.


<?php

$a="welcome";

echo ucfirst($a);

?>

Thursday 29 January 2015

Border radius in css.


The border-radius property is used to create rounded corners:

Example:

<a href="#home" style="background: #C00; border-radius: 50px;">Home</a>

Friday 23 January 2015

Fetch data randomly from database in PHP.

Fetch data randomly from MySQL database is to use "ORDER BY rand() function.



Example:



$result=mysql_query("select * from tablename order by rand() limit 5");

Count string length in PHP.

The strlen() function returns the length of a string.





Example:



<?php



echo strlen("welcome");



?>

Reverse word in a string using PHP.

The strrev() function reverses a string.



Example:

<?php

echo strrev("Welcome"); // outputs "emocleW"

?>

Confirmation Dialog Using JavaScript.

<a href="?del=<?php echo $row['id']; ?>" onclick="javascript:return confirm('Are you sure that you want to delete this record??');">Delete </a>

Thursday 22 January 2015

Find the number of rows in a table using MySQL.

<?php

$count=mysql_query("select count(colum_name) from table_name");

while($row2=mysql_fetch_array($count))

{

echo $row2['colum_name'];

}

?>

Current date and time in php.

<?php

date_default_timezone_set("Asia/Kolkata");

echo date('l jS F Y h:i:s A');



?>

Open a directory, and read its contents.

<?php

$dir = "employe/"; //directory name



if ($dr = opendir($dir))

{
while (($file = readdir($dr)))
{
echo "Filename:=" . $file . "<br>";
}

}

?>

Wednesday 21 January 2015

Delete file from folder in PHP.

use unlink() function



Example:

<?php



$delete_file="foldername/filename.gif";

unlink($delete_file);


?>

Send email using php.



To send email using PHP, you use the mail() function.



Example:



$subject="Test";

$message="Test msg...";

$to="emailid@gmail.com";

$from="example@gmail.com";

$headers .= 'From:'.$from. "\r\n";

mail($to,$subject,$message,$headers");

Upload files in PHP.




<?php

if($_POST['Submit']=="Submit")

{

$target_path = "uploads/";

$imgnew_name=$_FILES['file']['name'];

$target_path = $target_path.basename($imgnew_name);

move_uploaded_file($_FILES['file']['tmp_name'], $target_path);
$msg="File uploaded...";
}
?>

<form id="form1" name="form1" method="post" action="" enctype="multipart/form-data">
<label>
<input name="file" type="file" id="file" />
</label>
<label>
<input type="submit" name="Submit" value="Submit" />
</label>
</form>
<?php echo $msg; ?>

Open file in php and read the file.




<?php

$myFile = "testFile.txt";

$myFile =fopen($myFile, 'r') or die("can't open file");

while(!feof($myFile))

{

echo fgets($myFile). "<br>";

}

?>

Tuesday 20 January 2015

Open file in PHP.

<?php


$myFile = "testFile.txt";


fopen($myFile, 'r') or die("can't open file");


?>

Retrieve data from database in PHP.

<table width="100%" border="1" cellspacing="1" cellpadding="0">

<tr>

<td>Name</td>

<td>Email</td>

</tr>

<?php
$rec=mysql_query("select * from employe");
while($row=mysql_fetch_array($rec))
{
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['email']; ?></td>
</tr>
<?php }?>
</table>

Include file in PHP.

We can include a file using "include() " or "require()" function with file path as its parameter.



Example:



<?php include("header.php"); ?>



<?php require("header.php"); ?>

Insert data in database using PHP.

The INSERT INTO statement is used to add new records to a database table.



Example:

mysql_query("INSERT INTO table_name(colum1,colum2,colum3)

VALUES (value1, value2, value3)");

Create a Table using PHP and MySQL.

<?php


$con=mysqli_connect("localhost","user","password","test_db");


// Check connection


if (mysqli_connect_errno())


{


echo "Failed to connect to MySQL: " . mysqli_connect_error();

}


// Create table

$sql="CREATE TABLE Persons(FirstName CHAR(30),LastName CHAR(30),Age INT)";


// Execute query

if (mysqli_query($con,$sql))

{

echo "Table persons created successfully";

}

else

{

echo "Error creating table: " . mysqli_error($con);

}

?>

Create a database using PHP and MySQL.

<?php


$conn= mysql_connect("localhost", "username", "password");




$sql = 'CREATE Database test_db';


$retval = mysql_query( $sql, $conn );


if(! $retval )

{

die('Could not create database: ' . mysql_error());

}

echo "Database test_db created successfully\n";

mysql_close($conn);

?>

Connect Mysql Database with PHP.

We can connect Mysql Database with PHP using Procedural and Object oriented style like below




$con= mysql_connect("localhost", "username", "password");


mysql_select_db("database",$con);

Use of "echo" in php

It is used to print a data in the webpage,

Example:

<?php

echo 'Hello World';

?>