Searching...
Tuesday 23 October 2012

PHP : Mysql Select Query

6:18 pm

 Mysql Select Query

MySql Select Query
1.      The Select query is used to retrive data from a table of the database.
2.      When we give a select query in mysql_query() function then the return type of this function will be a resource of type mysql result on success and false on failure.
3.      Each column of a mysql result resource is identified by the column name or with column position number starts with 0,0 stands for first column.
4.      The difference functions used for retrieving current record from mysql result resource is
Mysql_fetch_array(ref of result[,type]);
It returns the current record as numeric array and assosiate array.
5.      The “type” argument value can be either the following pre-defined constants.
Mysql_NUM
Mysql_ ASSOC
Mysql_BOTH
                                 Mysql_fetch_row(ref of result)
It returns the current record as a numeric array with keys as column numbers.
                                           Mysql_fetch_object(ref of result)
It returns the current record as an object.
<body bgcolor="maroon" text="black">
<?php
echo "<h1>";
$con = mysql_connect("localhost","root","");
mysql_select_db("emp");
$rs = mysql_query("select * from emp1");
echo "no. of records-".  mysql_num_rows($rs)."<br>";
echo "no. of columns-".  mysql_num_fields($rs)."<br><br>";
/*$row = mysql_fetch_array($rs);
$row=  mysql_fetch_array($rs,MYSQL_NUM);
$row = mysql_fetch_array($rs,MYSQL_ASSOC);
$row =  mysql_fetch_array($rs,MYSQL_BOTH);
$row = mysql_fetch_row($rs);
$row = mysql_fetch_assoc($rs);
print_r($row);*/
$row = mysql_fetch_object($rs);
echo $row->empno."--".$row->ename."--".$row->sal;
mysql_data_seek($rs,2);
?>
    <table style="font-family: verdanan;font-size: 20px; background-image: url(Photo0104.jpg)" border="1" width="50%" align="center">
        <caption> Employee Details</caption>
        <tr height="60" style="text-align: center" >
            <th>EmpNO</th>
            <th>Ename</th>
            <th>Salary</th>
        </tr>
        <?php
        while($row = mysql_fetch_row($rs))
        {
            echo "<tr height=50>";
            echo "<td>".$row[0]."</td>";
            echo"<td>".$row[1]."</td>";
            echo"<td>".$row[2]."</td>";
        }
    echo"</table>";
    mysql_free_result($rs);
    mysql_close($con);
    ?>
</body>

0 comments:

Post a Comment