Pages

php faq

Monday, 26 March 2012


php frame works
1. Akelos       2.ash.mvc         3. cakePHP       4. Codelgniter    5.DIY     6. eZ Components

7.Fusebox   8.PHP ON TRAX    9.PHP Dev shell      10. Prado   11.YII   12. WACT     13. ZEND    14 .Zoop

15.wasp   16.PHP openbiz    17 .prado    18. seagull   19. symfony  


1....................................................
what is difference between print_r and var_dump?
Can anybody tell me the difference between var_dump() and print_r()
as I know that much only "print_r provides information about a variable,
var_dump provides a slightly more complex analysis."
prog:
<?php
$array=array(1,2,3,4,5);
print_r($array);
//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
var_dump($array);
//array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) }
?>
2...................................
waht is use of unset in php array ?
unset() destroys the specified variables.

The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy.

If a globalized variable is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.
<?php
function destroy_foo()
{
global $foo;
unset($foo);
}
$foo = 'bar';
destroy_foo();
echo $foo;
?>
3.....................................................................

Who is the father of PHP ?
Rasmus Lerdorf is known as the father of PHP.
4...............................................................................
What is the difference between $name and $$name?
$name is variable where as $$name is reference variable
<?php
$name='raju';
$$name='rani';
echo $raju;
?>//output:rani
5.....................................................................................
How can we submit a form without a submit button?
Java script submit() function is used for submit form without submit button
on click call document.formname.submit()
<form name="myform" action="handle-data.php">
Search: <input type='text' name='query' />

</form>
<script type="text/javascript">
function submitform()
{
document.myform.submit();
}
</script>

                                _____________
//  OUT PUT search |_____________|
6.................................................................................................................................
In how many ways we can retrieve the data in the result set ofMySQL using PHP?
We can do it by 4 Ways
1. mysql_fetch_row.
2. mysql_fetch_array
3. mysql_fetch_object
4. mysql_fetch_assoc
7..........................................................................................................................
what is an array?
A rray is a collection of hetrogenies data types.php is loosely typed language that
why we can store any type of elementes arrays .each element is combination
of "key & value".
<?php
//$arr = array(10,20,30);
$arr=array(0=>10,1=>'hi',2=>30);
print_r($arr);
?>
8.................................................................................................................
what is use of header() function in php?
The header() function sends a raw HTTP header to a client.
It is important to notice that header() must be called before any actual output is
sent (In PHP 4 and later, you can use output buffering to solve this problem)
9...........................................................................................................................
what is session_destroy?
session_destroy() destroys all of the data associated with the current session.
It does not unset any of the global variables associated with the session, or unset the
session cookie. To use the session variables again, session_start() has to be called.
<?php
session_start();
session_destroy();
header("location:login1.php");
?>
10..................................................................................................................................................
array function in php?
array_ change_ key_ case
array_ intersect
array_ key_ exists
array_ keys
array_ map
array_ merge_ recursive
array_ merge
array_ multisort
array_ pad
array_ pop
array_ product
array_ push
array_ rand
array_ reduce
array_ replace_ recursive
array_ replace
array_ reverse
array_ search
array_ shift
array_ slice
array_ splice
array_ sum
array_ udiff_ assoc
array_ udiff_ uassoc
array_ udiff






I think I found a bug! Who should I tell?
You should go to the PHP Bug Database and make sure the bug isn’t a known bug. If you don’t see it in the database, use the reporting form to report the bug. It is important to use the bug database instead of just sending an email to one of the mailing lists because the bug will have a tracking number assigned and it will then be possible for you to go back later and check on the status of the bug. The bug database can be found at » http://bugs.php.net/.
Source: http://www.php.net/manual/en/faq.general.php

 --
-- Database: `reddy`
--

--
-
-- Table structure for table `upload`
------------
------

CREATE TABLE IF NOT EXISTS `upload`
(
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(30) NOT NULL,

`type` varchar(30) NOT NULL,
`size` int(11) NOT NULL,
`content`
mediumblob NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT
CHARSET=latin1 AUTO_INCREMENT=13 ;

--
-- Dumping data for table `upload`
--
upload.php
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$conn=mysql_connect("localhost","root","");
mysql_select_db("reddy",$conn) or die('could not connect:' . mysql_error());

$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

mysql_query($query) or die('Error, query failed');
header('location:img.php');
echo "<br>File $fileName uploaded<br>";
}
?>
---------------------------------------------------------------
VIEW THE DATABASE  DATA
-------------------------------------------------------------
<?php

$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="reddy"; // Database name

// Connect to server and select database.
$con = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$query = "SELECT * FROM upload " ;
$result = mysql_query($query) or die("Query failed ($query) - " . mysql_error());
?>

<table width="1000" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="1000" border="1" cellspacing="0" cellpadding="3">
<tr>
<strong>List of Case Procedure </strong>
</tr>

<tr>
<td align="center" ><strong>id</strong></td>
<td align="center" ><strong>name</strong></td>
<td align="center"><strong>type</strong></td>

<td align="center" ><strong>Display</strong></td>
<td align="center" ><strong>delete</strong></td>
<td align="center" ><strong>Update</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>

<td><?php echo $rows["id"]; ?></td>
<td><?php echo $rows["name"];?></td>
<td><?php echo $rows["type"]; ?></td>

<td ><a href="view.php?id=<?php echo $rows['id']; ?>">Display</a></td>
<td ><a href="delete.php?id=<?php echo $rows['id']; ?>">Delete</a></td>
<td ><a href="update.php?id=<?php echo $rows['id']; ?>">Update</a></td>
</tr>

<?php
}
?>
<?php
mysql_close($con);
?>
----------------------------------------------------------------------------------
DELETE.PHP
------------------------------------------------------------------------------------  
<?php


// Your database connection code
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="reddy"; // Database name

// Connect to server and select database.
$con = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$id=$_REQUEST['id'];
echo $id;
$query = "DELETE FROM upload WHERE id = $id";


$result = mysql_query($query);
header("location:img.php");
echo "The data has been deleted.";

?>
==============================================
Display
===============================================  
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="reddy"; // Database name

// Connect to server and select database.
$con = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$id = $_GET['id'];
$query="SELECT * FROM uplode WHERE Id=$id";
// $image = mysql_query "SELECT * FROM image WHERE Id=$id";
$result = mysql_fetch_array($query);
header("Content-type: image/jpg");
echo $result['id'];
?>
------------------------------------------------------------------------------------------------------------------------------
register.php <HTML>
<?php
include "header1.php";
?>
<script type="text/javascript">
function formValidator(){
var firstname = document.getElementById('firstname');
var add = document.getElementById('add');
var state = document.getElementById('state');
var username = document.getElementById('username');
var password = document.getElementById('password');
var repassword = document.getElementById('repassword');
var email = document.getElementById('email');



if(isAlphabet(firstname, "Please enter only letters for your name")){
if(isNumeric(add, "Please enter a valid add")){
if(madeSelection(state, "Please Choose a State")){
if(lengthRestriction(username, 6, 8)){
if(isNumeric(password, "Please enter a valid password")){
if(isNumeric1(repassword, "Please enter a valid repassword")){
if(isNumeric2(email, "Please enter a valid email")){
return true;

}

}
}
}
}
}
}


return false;

}

function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}

function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9 a-zA-Z]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();

return false;
}
}

function isNumeric1(elem, helperMsg){
var numericExpression = /^[0-9 a-zA-Z]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
function isNumeric2(elem, helperMsg){
var numericExpression = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function madeSelection(elem, helperMsg){
if(elem.value == "Please Choose"){
alert(helperMsg);
elem.focus();
return false;
}else{
return true;
}
}

function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
function lengthRestriction(elem, min, max){
var uInput = elem.value;
if(uInput.length >= min && uInput.length <= max){
return true;
}else{
alert("Please enter between " +min+ " and " +max+ " characters");
elem.focus();
return false;
}
}
</script>
<BODY>
<table align= "center" width="100"border="1" bgcolor="green">
<form onsubmit='return formValidator()' name="login" method="POST" >
<tr><td> name:</td><td><input type="text" name="name" id="firstname"></td></tr>
<tr><td> add:</td><td><input type="text" name="add" id="add" ></td></tr>
<tr><td>State:</td><td> <select name="state" id="state">
<option>Please Choose</option>
<option> Andhra Pradesh</option>
<option>Arunachal Pradesh</option>
<option>Assam</option>
<option>Bihar</option>
<option>Chhattisgarh</option>
<option>Goa</option>
<option>Gujarat Haryana</option>
<option>Himachal Pradesh</option>
<option>Jammu andKashmir</option>
<option>Jharkhand</option>
<option>Karnataka</option>
<option>Kerala</option>
<option>Madhya Pradesh </option>
<option>Maharashtra</option>
<option>Manipur</option>
<option>Meghalaya</option>
<option>Mizoram</option>
<option>Nagaland</option>
<option>Orissa</option>
<option>Punjab</option>
<option>Rajasthan</option>
<option>Sikkim</option>
<option>Tamil Nadu</option>
<option>Tripura</option>
<option>Uttar Pradesh</option>
<option>Uttarakhand</option>

<option>West Bengal</option>

</select></td></tr>
<tr><td>username:</td><td><input type="username" name="username" id="username"></td></tr>
<tr><td>password:</td><td><input type="password" name="password" id ="password"></td></tr>
<tr><td>retype password:</td><td><input type= "password" name="retypepassword" id="repassword"></td></tr>

<tr><td>email:</td><td><input type="email" name="email" id="email"></td>
</tr>
<tr><td align="center" colspan="2" ><input type="submit" name="submit" value="submit" ><input type="reset" value="reset" ></td></tr>
</form></table></html>
<?php

if(isset($_POST['submit']))
{
$dbhost = "localhost";
$dbname = "karana";
$dbuser = "root";
$dbpass = "";
mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
// print_r($_POST);
$name = $_POST['name'];
$adder = $_POST['add'];
$color= $_POST['color'];
$email1= $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];
$state=$_POST['state'];
$email=$email1.$color;
//echo "$add";
$checkuser = mysql_query("SELECT username FROM user WHERE username='$username'");
$username_exist = mysql_num_rows($checkuser);
if($username_exist > 0)
{
echo "<table align='center' ><tr><td> 'Im sorry but the username you specified has already been taken. Please pick another one.' </td></tr></table>";
unset($username);
exit();
}
$query = "INSERT INTO user (name,email,username,password,state ,adder) VALUES('$name','$email','$username','$password','$state','$adder')";//$qry=mysql_query("insert into user (name,add) values('$name','$email')");
// echo "query is".$qry;
mysql_query($query);

// mysql_close();
// header("location:login1.php");?>
<script type="text/javascript">
window.location="login1.php";
</script>
<?php

}
?>
<?php

include "footer.php";
?>






------------------------------------------------------------------------------------------------------------
login.php

============================================================

<?php
include "header1.php";//header the site
if(isset($_POST['login'])){
include "config.php";//DATA BASE CONNECT
$username=$_POST['username'];
$password=$_POST['password'];
$sql="SELECT * FROM user WHERE username='$username' and password='$password'";
$result=mysql_query($sql);
$login=mysql_num_rows($result);
if($login)
{
session_start();
$_SESSION["username"] = $username;
// echo $_SESSION['uid']['password'];

// $_SESSION['uid']=1234;

?>
<script type="text/javascript">
window.location="index.php";
</script>
<?php
}
else
{?>
<table align="center"><tr><td ><FONT COLOR="red" size="+2">Wrong Username or Password
</td></tr><?php
}
}
?>
<html><?php

?>
<script type='text/javascript'>
function formValidator(){
var firstname = document.getElementById('firstname');
var password = document.getElementById('password');

if(isAlphabet(firstname, "Please enter only letters for your name")){
if(isNumeric(password, "Please enter a valid password")){

return true;
}
}
return false;
}

function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}

function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9 a-zA-Z]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isAlphabet(elem, helperMsg){
var alphaExp = /^[0-9 a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
</script>
<form onsubmit='return formValidator()' name="login" method="POST" >
<table align="center" border="2" bgcolor="yellow">
<tr><td align="center" colspan="3" font color="red"><b>Login Into Your Account</td></tr>
<tr><td>User Name: </td><td><input type='text' id='firstname' name="username"></td></tr>
<tr><td>password:</td><td> <input type='password' id='password' name="password"></td></tr>
<tr><td align="center"colspan="2"><input type='submit' value=" Login " name="login"></td></tr>
<tr><td><a href="forgott.php"> Forgott Password ?</a></td>
<td><a href="register.php"> Become A Member</a></td></tr><table>
</form>
<?php
include "footer.php";//footer the site
?>
------------------------------------------------------------------------------------------------------------

forgottpassword.php
-------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------

<?php
include "header1.php";
?>
<form method="post"onsubmit='return formValidator()'action="">
<table border="1" align="center" bgcolor="blue">
<tr><td align="center" colspan="2"><b> Forgott password</td></tr>
<tr><td>E-mail: </td><td><input type="email" name="email" size="24"></td></tr>
<tr><td align="center" colspan="2"><input type="submit" name="submit" value="submit"></td></tr>
</form> </table>
<?php
include "config.php";
if(isset($_POST['submit'])){
$email=$_POST['email'];
$sql = "SELECT password FROM user WHERE email='$email'";
$display=mysql_query($sql);
$row = mysql_fetch_assoc($display);
if($row){
$_SESSION['uid'] = $row;
echo "password->" .$_SESSION['uid']['password'];
//exit;
// header("location :11.php");
}
else
{
echo "Wrong E-mail";
}
}

include "footer.php";

?>



-----------------------------------------------------------------------
------------------------------------------------------------------------------
editpassword.php
------------------------------------------------------------------------
------------------------------------


<?php
include "header.php";
?><html>
<script type="text/javascript">
function che(){
var username = document.getElementById('username');
var password = document.getElementById('password');
var newpassword = document.getElementById('newpassword');
var confirmnewpassword = document.getElementById('confirmnewpassword');
if(isNumeric(username, "Please enter a valid username")){
if(isNumeric1(password, "Please enter a valid password")){
if(isNumeric2(newpassword, "Please enter a valid repassword")){
if(isNumeric3(confirmnewpassword, "Please enter a valid confirmnewpassword")){
return true;
}
}
}
}

return false; }
function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}

function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9 a-zA-Z]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isNumeric1(elem, helperMsg){
var numericExpression = /^[0-9 a-zA-Z]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isNumeric2(elem, helperMsg){
var numericExpression = /^[0-9 a-zA-Z]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isNumeric3(elem, helperMsg){
var numericExpression = /^[0-9 a-zA-Z]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
</script>
<body>
<form onsubmit="return che()" name="submit" method="post" >
<table align="center" bgcolor="ffff##">

<tr><td>username</td><td><input type="text" name="username" id="username"></td></tr>
<tr><td>password</td><td><input type="password" name="password" id="password"></td></tr>
<tr><td>newpassword</td><td><input type="password" name="newpassword" id="newpassword"></td></tr>
<tr><td>confirmnewpassword</td><td><input type="password" name="confirmnewpassword" id="confirmnewpassword"></td></tr>
<tr><td align="center" colspan="3"><input type="submit" name="submit" value="submit" ></td></tr>

</table>
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
include 'config.php';
$username = $_POST['username'];
$password = $_POST['password'];
$newpassword = $_POST['newpassword'];
$confirmnewpassword = $_POST['confirmnewpassword'];
echo " username->".$username." <br> newpassword = ".$newpassword." ";
$query=("UPDATE `user` SET `password` = '".$newpassword."' WHERE `username` ='".$username."'" );
$result=mysql_query($query);

if($result)
{
?>
<script type="text/javascript">
//window.location="login1.php";
</script>
<?php
}
else
{
echo "Records are not inserted";
}
}/*
if($password!= mysql_result($result))
{
echo "You entered an incorrect password";
}
if($newpassword=$confirmnewpassword) {

$sql=mysql_query("UPDATE `user` SET `password` = '".$newpassword."' WHERE `username` ='".$username."'" );
if($sql)
{
echo "Congratulations You have successfully changed your password";
}
else
{
echo "The new password and confirm new password fields must be the same";
}
}
} */
?>


<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>
<html>
<body>

<form action="" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

page1.php
<? php

session_start();
$sno="1001";
$_session['uid']="12345";

?>
<a href ="page2.php">gotonext</a>

page2.php

<?php

session_start();
echo $-session['uid'];
echo $sno;

?>

out put:
 12345
 

Most Reading