Monday, December 19, 2011

pull mysql data as options into select field

<?php
$con = mysql_connect("localhost","username","password"); //connection details
$db = mysql_select_db("databasename",$con); // database details
$qry = mysql_query("SELECT somefield FROM sometable WHERE something = 'somevalue' ")or die(mysql_error()); //actual query
echo "<select name=' '>"; 
echo "<option></option>"; //if you want the first one to be blank
while ($row = mysql_fetch_array($qry))
{
$i = $row['somefield'];
echo "<option>$i</option>";
}
echo "</select>";
mysql_close($con);
?>

how to save MD5 encrypted password into mysql table field

$qry = mysql_query("INSERT INTO table ( username, password ) VALUES ( 'name' , MD5('password')  ")or die(mysql_error());

add or subtract time in php


<?php
date_default_timezone_set('Asia/Dubai');
$rightnow = date('d-m-Y H:i:s');
echo "The time right now is $rightnow";
echo "<br />";
$after = 1200 + time();
$timeafter = date('d-m-Y H:i:s',$after);
echo "The time after 20 minutes will be $timeafter";
echo "<br />";
$before = time() - 1200;
$timebefore = date('d-m-Y H:i:s',$before);
echo "The time 20 minutes ago was $timebefore";
?>

Sunday, December 18, 2011

cron job

the following command will fire the script in cron tab

/usr/bin/php -q /home/username/public_html/file.php

file.php is the file that will be fired.
public_html is the folder where the file is saved.
username is your username

Friday, December 16, 2011

how to echo mysql retrieved value into a form as default value

// single field that has been retrieved from mysql database
$qry = mysql_query(" SELECT somefield FROM sometable WHERE something =  'some value'  ")or die(mysql_error());
$result = mysql_fetch_row($qry);
<form action="" method="">
<input type="" name="" value=" <?php echo $result[0]; ?>  ">
</form>
// in case of multiple values retrieved from mysql as a simple array

$qry = mysql_query(" SELECT somefield FROM sometable WHERE something =  'some value'  ")or die(mysql_error());
$result = mysql_fetch_array($qry);
<form action="" method="">
<input type="" name="" value=" <?php echo $result[0]; ?> "> //places the first value from mysql
<input type="" name="" value=" <?php echo $result[1]; ?> "> //places the second value
<input type="" name="" value=" <?php echo $result[2]; ?> "> //places the third value
<input type="" name="" value=" <?php echo $result[3]; ?> "> //places the fourth value and so on
</form>


how to use mysql_num_rows example

$qry = mysql_query(" SELECT * FROM abc WHERE something = 'something' ")or die(mysql_error());
$num = mysql_num_rows($qry);
if($num = 0)
{
// run code here
}
    else
    {
    // run code here
    }

Thursday, December 8, 2011

how to use multiple forms on a single page

just name the submit button like this


1st form
<form action="abc.php" method="post"> 
<input type="text" name="abc">
<input type="submit" name="abc">
</form>
2nd form

<form action="abc.php" method="post"> 
<input type="text" name="def">
<input type="submit" name="xyz">
</form>
process the firm results
<?php
if(count($_POST)){
if (!empty($_POST["abc"])){
run your code here for 1st form
}
elseif(!empty($_POST["xyz"])){
run your code here for 2nd form
}
}
?>

you can place as many forms on a single page. As long as the submit name is different, only 1 form will be processed at a time.

how to show current date in php

just copy the below code and place anywhere you want the date to appear. Shows date in dd-mm-yyyy format.

<?php echo date('d-m-Y'); ?> //Prints date in DD-MM-YYYY format
<?php echo date('Y-m-d'); ?> //Prints date in YYYY-MM-DD format which is used in Mysql database

if you want this as a default value in any input box when working with forms such as a date input box use this

<input type="date" name="" value="<?php echo date('d-m-Y'); ?>">