Friday, May 14, 2010

The Difference Between mysql_fetch_array() And mysql_fetch_assoc() and mysql_fetch_object()

$rows = mysql_fetch_array( "select name, address from people");

Means you then get each of the row results as a straight array, meaning you dont necessarily need to know in advance the order elements arrive via the select.

foreach( $rows as $row )
echo $row[0] . ' lives at ' . $row[1] ;

$rows = mysql_fetch_assoc( "select name, address from people" );

Means you then get each of the row results as a named array elements, an associative array.

foreach( $rows as $row )
echo $row['name'] . ' lives at ' . $row['address'];

Which is now dependent on you knowing what the result columns are named.

$rows = mysql_fetch_object( "select name, address from people" );

Means you then get each of the row results using the object notation.

foreach( $rows as $row )
echo $row->name . ' lives at ' . $row->address ;

Which again relies upon your knowing what is coming out of the database, and in my opinion is better because a) its easier to read, and b) if you don't use OOP, it at least gets you thinking about and using OO notation.

-----------------------------------------------------------------------------------------------------

There are 3

mysql_fetch_row();
mysql_fetch_assoc();
mysql_fetch_array();

mysql_fetch_row() returns an array with integer indexing. i.e if you get
a result $rec=mysql_fetch_row(SOME RESULT) then you can only get the
result by specifying an integer index like $rec[0], $rec[1] e.t.c

mysql_fetch_assoc() returns an associative array that can only be
accessed by field name e.g. $rec[firstname], $rec[lastname] e.t.c

mysql_fetch_array() returns an associative array that can be accessed by
both of the above methods.

So mysql_fetch_row() will be best for speed but in more recent versions
of php associative arrays are more or less as fast.

Personally I use mysql_fetch_array(); as I like to use both associations
and integers.
-----------------------------------------------------------------------------------------------------

Getting data from MySQL with PHP is very convenient. But I found most of PHP beginners don't know the difference between two MySQL functions: mysql_fetch_array() and mysql_fetch_assoc(). If you don't understand the two functions, you always come up against such problem: why can't I see the result from MySQL?

Suppose we have a database called 'test' and it has a table called 'members' which stores some members' information. The goal is to fetch all the members' information. Consider such code below:


$conn=mysql_connect('localhost','root','');
$mysql_select_db('test');
$rs=mysql_query('SELECT * FROM members');



Then $rs is an array that stores all personal information. You can use mysql_fetch_array() or mysql_fetch_assoc() to fetch each row by calling the function repeatedly. Consider:


while ($row=mysql_fetch_array($rs,MYSQL_NUM)){
  // ...
}


Here $row is a normal array (integer-indexed array). You cannot get data with $row['name'], $row['birthday'], etc. You must use integer as the index, e.g. $row[0], $row[1]... How to get an associative array? You need to set the second parameter of mysql_fetch_array(). MYSQL_ASSOC tells mysql_fetch_array() to return an associative array. See below:


while ($row=mysql_fetch_array($rs,MYSQL_ASSOC)){
  // ... here you can use $row['name'], $row['birthday']...

}



mysql_fetch_array($rs,MYSQL_BOTH) equals to mysql_fetch_array($rs). There's no need to use MYSQL_BOTH, so we often omit it.

So you can use two methods to fetch data by specifying the second parameter to MYSQL_BOTH or omitting the second parameter. Then both of the styles, $row[0] and $row['name'], are available.

But where's mysql_fetch_assoc()? It seems not useful. Right. mysql_fetch_assoc($rs) is equivalent to mysql_fetch_array($rs,MYSQL_ASSOC). Obviously, when you need to an associated array (string-indexed array) only, mysql_fetch_assoc() is more convenient. By the way, mysql_fetch_assoc doesn't have the second parameter. It can only return associative array so it needs less memory space to store the result array.

----------------------------------------------------------------------------------------------------------




--
http://www.venkatmails.blogspot.com/

Venkat Mails, Fun , Cool pictures, Fun messages, Sardar Jokes, Quotations Moral stories Fun stories

http://www.venkatmails.blogspot.com/

The Difference Between mysql_fetch_array() And mysql_fetch_assoc()

Getting data from MySQL with PHP is very convenient. But I found most of PHP beginners don't know the difference between two MySQL functions: mysql_fetch_array() and mysql_fetch_assoc(). If you don't understand the two functions, you always come up against such problem: why can't I see the result from MySQL?

Suppose we have a database called 'test' and it has a table called 'members' which stores some members' information. The goal is to fetch all the members' information. Consider such code below:

$conn=mysql_connect('localhost','root','');
$mysql_select_db('test');
$rs=mysql_query('SELECT * FROM members');



Then $rs is an array that stores all personal information. You can use mysql_fetch_array() or mysql_fetch_assoc() to fetch each row by calling the function repeatedly. Consider:

while ($row=mysql_fetch_array($rs,MYSQL_NUM)){
// ...
}


Here $row is a normal array (integer-indexed array). You cannot get data with $row['name'], $row['birthday'], etc. You must use integer as the index, e.g. $row[0], $row[1]... How to get an associative array? You need to set the second parameter of mysql_fetch_array(). MYSQL_ASSOC tells mysql_fetch_array() to return an associative array. See below:

while ($row=mysql_fetch_array($rs,MYSQL_ASSOC)){
// ... here you can use $row['name'], $row['birthday']...
}


mysql_fetch_array($rs,MYSQL_BOTH) equals to mysql_fetch_array($rs). There's no need to use MYSQL_BOTH, so we often omit it.

So you can use two methods to fetch data by specifying the second parameter to MYSQL_BOTH or omitting the second parameter. Then both of the styles, $row[0] and $row['name'], are available.

But where's mysql_fetch_assoc()? It seems not useful. Right. mysql_fetch_assoc($rs) is equivalent to mysql_fetch_array($rs,MYSQL_ASSOC). Obviously, when you need to an associated array (string-indexed array) only, mysql_fetch_assoc() is more convenient. By the way, mysql_fetch_assoc doesn't have the second parameter. It can only return associative array so it needs less memory space to store the result array.

Friday, May 7, 2010

Complete Date Validation in Java Script Both DD-MM-YYYY or DD/MM/YYYYY formats

Complete Date Validation in Java Script Both DD-MM-YYYY or DD/MM/YYYYY formats

<html>
<head>
<script type="text/javascript">
    function valid_date(fdate)
    {

        //alert('Hii'+fdate);
        fdate = f.procdate.value;
        //alert(fdate);
        strfdate =fdate.split("/");
        len=strfdate.length;
        if(len!=3)
        {
            strfdate =fdate.split("-");
            len=strfdate.length;
        }
        fdays=strfdate[0];
        fmonth=strfdate[1];
        fyear=strfdate[2];
        /*alert(fday);
        alert(fmonth);
        alert(fyear);*/
        //alert(len);
        if(len!=3)
        {
            alert("Proceeding Date is not valid. Date Format is DAY-MON-YEAR");
            f.procdate.value='';
            return false;
        }

        var maxdays=getmaxdays(fmonth,fyear);
        //alert('Max Days:'+maxdays);
        if(maxdays<fdays)
        {
            alert("Days are excceded for the given month in proceeding Date");
            f.procdate.value='';
            return false;
        }
       
        var c=isNaN(fdays);
        if(c)
        {
            alert('Days in Proceeding Date Must be numeric only');
            f.procdate.value='';
            return false;
        }
        var c=isNaN(fmonth);
        if(c)
        {
            alert('Month in Proceeding Date Must be numeric only');
            f.procdate.value='';
            return false;
        }
        var c=isNaN(fyear);
        if(c)
        {
            alert('Year in Proceeding Date Must be numeric only');
            f.procdate.value='';
            return false;
        }
        if(fmonth>12)
        {
            alert("Month is not valid in proceeding Date");
            f.procdate.value='';
            return false;
        }
        else
        {
            alert('Valid Date');
            return true;
        }
    }
    function getmaxdays(mon,year)
    {

        mon++; mon--; year++; year--;
        if(mon==1)
        return 31;
        else if(mon==2)
        {

            if ((year % 100) == 0)
            {
            if ((year % 400) == 0)
            return 29;
            }
            else
            if ((year % 4) == 0) return 29;
            else
            return 28;
     
        }
        else if(mon==3)
        return 31;
        else if(mon==4)
        return 30;
        else if(mon==5)
        return 31;
        else if(mon==6)
        return 30;
        else if(mon==7)
        return 31;
        else if(mon==8)
        return 31;
        else if(mon==8)
        return 31;
        else if(mon==9)
        return 30;
        else if(mon==10)
        return 31;
        else if(mon==11)
        return 30;
        else if(mon==12)
        return 31;


    }

</script>
</head>
<body>
<form name='f'>
Enter Date: <input type=text name='procdate' id='procdate'  maxlength=20  onchange='return valid_date(this.value)'>
</form>
</body>
</html>

Saturday, March 27, 2010

Sorting functions in php sort(), asort() ksort() -- difference between sort() asort() arsort() rsort()

Sorting functions in php sort(), asort() ksort()
difference between sort() asort() arsort() rsort()

<?php

    $fruits = array("lemon", "orange", "banana", "apple");
    echo "The Original Array is:<br>";
    foreach ($fruits as $key => $val)
    {
        echo "fruits[" . $key . "] = " . $val . "<br>";
    }

    echo "<br>The Array After sort() is:<br>";
    sort($fruits);
    /*
        sort()
        This function sorts an array. Elements will be arranged
        from lowest to highest when this function has completed.

    */
    foreach ($fruits as $key => $val)
    {
        echo "fruits[" . $key . "] = " . $val . "<br>";
    }
    /*
        ----------------------------
OUTPUT---------------------
        fruits[0] = apple
        fruits[1] = banana
        fruits[2] = lemon
        fruits[3] = orange
        -------------------------------------------------------
    */

    /*
        asort()
        This function sorts an array such that array indices
        maintain their correlation with the array elements they are
        associated with. This is used mainly when sorting
        associative arrays where the actual element order is
        significant.
    */
    echo "<br>The Array After asort() is:<br>";
    $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
    asort($fruits);
    foreach ($fruits as $key => $val)
    {
        echo "$key = $val<br>";
    }
    /*
        --------------------OUTPUT------------------------
        c = apple
        b = banana
        d = lemon
        a = orange
        --------------------------------------------------

    */
   
    /*
        ksort()
    Sorts an array by key, maintaining key to data
    correlations. This is useful mainly for associative arrays.
    */
    echo "<br>The Array After ksort() is:<br>";
    $fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
    ksort($fruits);
    foreach ($fruits as $key => $val)
    {
        echo "$key = $val<br>";
    }
    /*
    --------------------OUTPUT----------------------------
    a = orange
    b = banana
    c = apple
    d = lemon
    ------------------------------------------------------
    */

    echo "<br><br><br><br>Revese Sort";
    $input = array ("Pinaple", "Orange", "Banana", "Mango", "Apple","Strawberry");
    $input1=$input;
    $input2=$input;
    $input3=$input;
    echo "<br>-----Original Array---------<br>";
    while (list ($key, $val) = each ($input))
    {
        echo "$key -> $val <br>";
    }
    sort($input1);
    echo "<br>-----After sort---------<br>";
    while (list ($key, $val) = each ($input1))
    {
        echo "$key -> $val <br>";
    }

    rsort($input2);
    echo "<br>-----After rsort---------<br>";
    while (list ($key, $val) = each ($input2))
    {
        echo "$key -> $val <br>";
    }
    arsort($input3);
    echo "<br>-----After rsort---------<br>";
    while (list ($key, $val) = each ($input3))
    {
        echo "$key -> $val <br>";
    }
?>


--
******************************************************
http://www.venkatmails.blogspot.com/

We are richer when we give and poorer when we keep!
-------------------------------------------------
By the next 10 months, our earth will become 3 degrees hotter than its now .
Our himalayan glaciers are melting at a rapid rate. So all of you lend your
hands to fight global warming -Plant more trees .Don't waste water
--Don't use or burn plastic. - Control paper use.
-------------------------------------------------
"The fastest way to succeed is to double your failure rate." --Thomas J. Watson, Founder of IBM
-------------------------------------------------
A lion wakes up everyday and starts running after a deer to survive.
A deer wakes up everyday and starts running from a lion to survive.
So the point is that it does not matter whether you are a lion or a deer.
You just have to keep running.

http://www.venkatmails.blogspot.com/
******************************************************

Javascript focus() , Text field focus. focus().----> Alternative code to focus()

Javascript focus()
Text field focus. focus().
Alternative code to focus()
Under IE, when an error occurs the cursor returns to the field with the error. Under Firefox, the cursor stays in the next (tabbed to) field. Is there a way to code this validation so that it would work for both??

Program:
<html>
<head>
<script language='javascript'>
function check()
{
    alert('hii');
    //document.joe.burns.focus(); This is valid for only internet explorer. And will not work on Firefox
    document.getElementById("
burns").focus();
    setTimeout("document.getElementById('burns').focus();",0);
}
function check2()
{
var letters2 = document.joe.tammy.value.length +1;
if (letters2 <= 4)
{document.joe.tammy.focus()}
else
{document.joe.chloe.focus()}
}

</script>
<body>
<FORM NAME="joe">
One:<INPUT TYPE="text" name="burns" id="burns" size="10" onchange="check()"><BR>
Two:<INPUT TYPE="text" name="tammy" size="10" onchange="check2()"><BR>
<INPUT TYPE="submit" VALUE="Click to Send" NAME="go">
</FORM>
</body>
</html>


--
******************************************************
http://www.venkatmails.blogspot.com/

We are richer when we give and poorer when we keep!
-------------------------------------------------
By the next 10 months, our earth will become 3 degrees hotter than its now .
Our himalayan glaciers are melting at a rapid rate. So all of you lend your
hands to fight global warming -Plant more trees .Don't waste water
--Don't use or burn plastic. - Control paper use.
-------------------------------------------------
"The fastest way to succeed is to double your failure rate." --Thomas J. Watson, Founder of IBM
-------------------------------------------------
A lion wakes up everyday and starts running after a deer to survive.
A deer wakes up everyday and starts running from a lion to survive.
So the point is that it does not matter whether you are a lion or a deer.
You just have to keep running.

http://www.venkatmails.blogspot.com/
******************************************************

Thursday, November 12, 2009

How to display all the reports or files in one directory, that have commmon substring in their name

How to display all the reports or files in one directory, that have commmon substring in their name http://localhost/reports/?P=DA*25000702001*.txt sample output for the above is: Index of /reports Name Last modified Size Description Parent Directory - DA-25000702001-1.txt 13-Nov-2009 12:34 9.1K DA-25000702001-2.txt 07-Nov-2009 17:05 1.9K DA-Shedule-25000702001-1.txt 13-Nov-2009 12:34 4.6K DA-Shedule-25000702001-2.txt 07-Nov-2009 17:05 2.1K HERE /?P=DA*25000702001*.txt is very important.

How to create photo slideshow using javascript and html

How to create photo slideshow using javascript and html

<html>
<head>
<script type="text/javascript">

var image1=new Image()
image1.src="http://localhost/DA/1.JPG"
var image2=new Image()
image2.src="http://localhost/DA/2.JPG"
var image3=new Image()
image3.src="http://localhost/DA/3.JPG"
var image4=new Image()
image4.src="http://localhost/DA/4.JPG"
var image5=new Image()
image5.src="http://localhost/DA/5.JPG"
var image6=new Image()
image6.src="http://localhost/DA/6.JPG"
var image7=new Image()
image7.src="http://localhost/DA/7.JPG"


</script>
</head>
<body>

<img src="1.JPG" name="slide" border="0" width="900" height="900" />
<script type="text/javascript">
<!--
var step=1
var whichimage=1
function slideit()
{
if (!document.images)
return
document.images.slide.src=
eval("image"+step+".src")
whichimage=step
if (step<7)
step++
else
step=1
setTimeout("slideit()",5400)
}
slideit()
function slidelink(){
if (whichimage==1)
window.location="link1.htm"
else if (whichimage==2)
window.location="link2.htm"
else if (whichimage==3)
window.location="link3.htm"
}
//-->
</script>
</body>
</html>