Total Pageviews

How does the Mysql result set works in php

Written By Naveen on Wednesday, December 7, 2011 | 8:58 AM

Normally if you want to connect to the mysql server the php function is mysql_connect('hostname','username','password')   and to connect the database mysql_select_db('database_name').


And then we will write a query and will execute like this
$resultset  =   mysql_query("SELECT * FROM employees");
Now from the $resultset we have to fetch the data.
In php we have 4 mysql functions to fetch the data from result set
  • mysql_fetch_row($resultset)
  • mysql_fetch_array($resultset)
  • mysql_fetch_assoc($resultset)
  • mysql_fetch_object($resultset)
see the code example below.

mysql_connect("hostname", "user", "password");
mysql_select_db("mydb");

$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
$row = mysql_fetch_row($result);
echo $row[0]; // 42
echo $row[1]; // the email value



$result = mysql_query("select id,email from mytable");
while ($row = mysql_fetch_object($result)) {
    echo $row->user_id;
    echo $row->fullname;
}
mysql_free_result($result);


while ($row = mysql_fetch_assoc($result)) {
   echo $row["id"];
   echo $row["email"];
}

while ($row = mysql_fetch_array($result)) {
    echo ( $row[0], $row[1]);  
    echo ( $row['id'], $row['email']);  
}



8:58 AM | 2 comments | Read More

How to upload file using php

Written By Naveen on Tuesday, November 29, 2011 | 8:21 AM

The following code and functions are used in file upload  and I would these are must for file uploading.


- move_uploaded_file() - using this function we can move the file from temp path to destination path.
- $_FILES -  using this variable we can get the file details.
- enctype="multipart/form-data"  we must need it in the form.
- method="POST" this should be post only.
- <input name="filename" type="file" /> -using this we will get file upload button.

In the HTML body tag write the following code
 <form enctype="multipart/form-data" action="fileupload.php" method="POST">
   Select a file to upload: <input name="filename" type="file" /><br />
   <input type="submit" name='fileupload' value="Upload File" />
</form>
The following code can be in the same file or it can in the action="fileupload.php"
<?php
if($_POST['fileupload']=='Upload File')
{
    $destination_path = "uploads/";
    $destination_path = $destination_path . basename( $_FILES['uploadedfile']['name']); 


    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $destination_path )) {
        echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
        " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
}
?>
$_FILES['uploadedfile']['name'] - contains the file name
$_FILES['uploadedfile']['tmp_name'] - contains the server temp path where it has been uploaded
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path) -  this will move the file from temp path to $destination_path


8:21 AM | 0 comments | Read More

php $_SESSION and $_COOKIE

Before going to know about the sessions and cookies lets see the difference between $_GET,$_POST , $_SESSION, $_COOKIE.

As we have already discussed about the $_GET and $_POST from  this link http://phptag.blogspot.com/2011/11/difference-between-get-post-and-request.html

As it says that the data will be carried through GET and POST methods from one page to another page.
For example if we have abc.php, xyz.php and pqr.php , lets assume that abc.php has a form with input elements. Then if we need to pass that data to xyz.php we can pass it through URL get method or POST method and again if we want to pass the data in xyz.php to pqr.php we have to pass using GET and POST methods.

SESSIONS
Lets see how it works if we use the $_SESSION
In abc.php if we can set the session like this

<?php 
session_start();
$_SESSION['name']='phptag';
?>

Then we can directly access $_SESSION['name'] variable in any php page with in the application by starting with session_start() like this

<?php
session_start();
echo $_SESSION['phptag'];
?>

we can delete the $_SESSION['name'] by using unset() and session_destroy() function like this

<?php
unset($_SESSION['name']);
//or
session_destroy(); //Finally, destroy the session.
?>

We can also set the session time limit in php.ini file or by using ini_set() function dynamically.

COOKIES
Cookies are always set in the client side for example Firefox, IE, Chrome and Opera etc...
That means whenever we set the cookie it will set in the browser for a particular time period.

How to set the cookie:
we can set th cookie using setcookie() function like this
<?php

$value = 'phptag';
setcookie("name", $value, time()+3600);
// name - cookie name
// $value - value that need to set
// time()+3600 - that means it will expire in 1 hour.
?>


How to access the cookie:

<?php
// It will print "phptag";
echo $_COOKIE['name'];
?>


How to delete the cookie:

<?php
setcookie ("name", "", time() - 3600);
// name - cookie name
// $value - should be empty
// time()-3600 - we should give expiration date is in the past
?>




5:58 AM | 0 comments | Read More

php echo and print difference


echo
 - Is a command only.
 - Faster than print
 - It can print multiple values.
  Example:
 <?php
  echo 1,2,3,4;
  echo $name,$email,$phone;
 ?>

print
 - Is a function.
 - It will return true(1) or false(0) or some values.
 - It can print only one value.
    Example:
    <?php
    print 1;
    print $name;
    if(print(1)){
         echo "print returned true";
    }
    ?>



4:38 AM | 0 comments | Read More

Difference between $_GET, $_POST and $_REQUEST

$_GET, $_POST and $_REQUEST  all these variables are predefined variables in php. Normally we have 2 methods to send the data to one page to another page. Those methods are nothing but GET and POST methods. Using this variable we can get the data by specifying the variable name for example, $_GET['name'], $_POST['email'] and $_REQUEST['phone'].

Using GET method:
we can get the data from URL
Example: www.example.com/index.php?name=phptag
Now from the above URL if you want to get the phptag value see the code below

<?php

$name = $_GET['name'];
//or
$name = $_REQUEST['name'];

?>

Using POST method:
we can get the in hidden mode, that means we cannot see the data in URL it will carry through the headers.
Normally we can use the post method by implementing the html form's see the code below

<form name='phptag' method='post'>
     <input type='text' name='name' value='phptag'>
     <input type='submit' name='demo' value='Click' >
</form>
<?php

$name = $_POST['name'];
//or
$name = $_REQUEST['name'];

?>

In the above examples the common thing that we have used is $_REQUEST which means it can be used for both GET and POST methods to get the data.
3:02 AM | 15 comments | Read More