Searching...
Friday, 2 November 2012

My Sql Functions With Examples

9:41 am

My Sql Functions With Examples

Program to get the details of all the columns of “emp” table?
<?php
echo "<h1>";
$con = mysql_connect('localhost','root','');
mysql_select_db("emp");
$res = mysql_query("select * from emp1");
$i =0;
while($i<  mysql_num_fields($res))
{
    echo "<u> information for columns".($i+1).":</u><br>";
    $meta = mysql_fetch_field($res,$i);
    echo "<pre>
        Blob            :   $meta->blob
        Max_length      :   $meta->max_length
        Name            :   $meta->name
        Not-Null        :   $meta->not_null
        Numeric         :   $meta->numeric
        Primary-key     :   $meta->primary_key
        Table           :   $meta->table
        Type            :   $meta->type
        Default         :   $meta->def
        Unique-key      :   $meta->unique_key
        Unsigned        :   $meta->unsigned
        Zerofill        :   $meta->zerofill
            </pre>";
        $i++;       
}

mysql_close();

?>
OutPut:
information for columns1:

        Blob            :   0
        Max_length      :   3
        Name            :   empno
        Not-Null        :   1
        Numeric         :   1
        Primary-key     :   1
        Table           :   emp1
        Type            :   int
        Default         :  
        Unique-key      :   0
        Unsigned        :   0
        Zerofill        :   0
           
information for columns2:

        Blob            :   0
        Max_length      :   12
        Name            :   ename
        Not-Null        :   0
        Numeric         :   0
        Primary-key     :   0
        Table           :   emp1
        Type            :   string
        Default         :  
        Unique-key      :   0
        Unsigned        :   0
        Zerofill        :   0
           
information for columns3:

        Blob            :   0
        Max_length      :   7
        Name            :   sal
        Not-Null        :   0
        Numeric         :   1
        Primary-key     :   0
        Table           :   emp1
        Type            :   real
        Default         :  
        Unique-key      :   0
        Unsigned        :   0
        Zerofill        :   0
7.mysql_fetch_lengths(mywql result): get the length of each ouput in a result.
<?php
echo "<h1>";
$con = mysql_connect('localhost','root','');
mysql_select_db('emp');
$res = mysql_query("select * from emp1 where empno=101");
$record = mysql_fetch_assoc($res);
print_r($record);
echo "<br><br>";
$length = mysql_fetch_lengths($res);
print_r($length);

?>
OutPut:
Array ( [empno] => 101 [ename] => penna [sal] => 123.45 )

Array ( [0] => 3 [1] => 5 [2] => 6 )
8.mysql_field_len(mysql result , int $field_offset) : returns the length of the specified field.
9.mysql_field_name(mysql result , int $field_offset) : get the name of the specified field in a result.
10.mysql_field_type(mysql result,int $field_offset) : get the type of the specified field in a result.
<?php
echo "<h1>";
$con = mysql_connect('localhost','root','');
mysql_select_db('emp');
$rs = mysql_query("select * from emp1");
$i=0;
while($i<  mysql_num_fields($rs))
{
    echo mysql_field_name($rs, $i)."--".mysql_field_table($rs, $i)."--".mysql_field_len($rs, $i)."<br>";
    $i++;
}
?>
OutPut:
empno--emp1--10
ename--emp1--20
sal--emp1—10
1.      Mysql_free_result(mysql result) :  free mysql result memory.
2.      Mysql_get_client_info()
3.      Mysql_get_host_info()
4.      Mysql_get_proto_info()
5.      Mysql_get_server_info()
6.      Mysql_info()
7.      Mysql_num_fields(mysql result):  get number of fields in result resource.
8.      Mysql_num_rows(mysql result): get number of rows/records in result resource.
9.      Mysql_ping($conn) : ping a server connection or reconnect if there is no connection.

5 comments:

  1. Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well as giving..
    Study Abroad Consultants in Chennai | Study in Germany Consultants in Chennai | Germany Education Consultants in Chennai

    ReplyDelete
  2. Superb explanation & it's too clear to understand the concept as well, keep sharing admin with some updated information with right examples.Keep update more posts.

    Digital Marketing Company in India

    ReplyDelete
  3. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.
    Sharepoint Training in Chennai

    ReplyDelete
  4. This is a great article, I have been always to read something with specific tips! I will have to work on the time for scheduling my learning.
    Jobs in Kolkata
    Jobs in Mumbai

    ReplyDelete
  5. Thank you for sharing such a nice and interesting blog with us. I have seen that all will say the same thing repeatedly. But in your blog, I had a chance to get some useful and unique information. I would like to suggest your blog in my dude circle.
    Jobs in Chennai
    Jobs in Bangalore
    Jobs in Delhi
    Jobs in Hyderabad
    Jobs in Kolkata
    Jobs in Mumbai
    Jobs in Noida
    Jobs in Pune

    ReplyDelete