Monday 12 March 2012

Mysql function


Ø       mysql_connect

  • This function is used for creating the connection with MYSQL. It opens the connection for inserting, updating, deleting or selecting record.

Syntax

resource mysql_connect(string server, string username, string password)

Parameter
Description
Server
The mysql server.
Username
The username. Default value is the name of the user that owns the server.
Password
The password. Default value is an empty password.

Example

<?php 
mysql_connect('localhost','root','');
?>

Ø       mysql_select_db

  • Select a mysql database.
  • It return Boolean value. If the database exists then it returns true else will return false.

Syntax

boolean mysql_select_db(string database, resource handler)

Parameter
Description
Database
The name of the database that is to be selected.
Resource handler
The mysql connection.

Example

<?php 
mysql_connect('localhost','root','');
mysql_select_db("test",$link);
?>

Ø       mysql_query

  • Now after selecting the database, the query is used for selecting, updating, inserting, deleting the records.
  • The query which will be written will be in string format, so to execute that query this function is used.

Syntax

Resource  mysql_query(string query)

Example

<?php 
mysql_connect('localhost','root','');
mysql_select_db("test",$link);
mysql_query("insert into info(stud_name) values(‘hemangi’) ");
?>

Ø       mysql_num_rows

  • This function is used to count the number of rows present in the given table.it returns integer value. This function is only valid for select statements.

Syntax

integer  mysql_num_rows(resource handler)
         
Example

<?php 
$link=mysql_connect('localhost','root','');
mysql_select_db("test",$link);
$rs=mysql_query("select * from info");
echo "number of rows in database is : ".mysql_num_rows($rs);
?>

Ø       mysql_affected_rows

  • Get number of affected rows in previous MYSQL operation.
  • Returns the number of affected rows on success, and -1 if the last query failed.

Syntax

integer mysql_affected_rows()

Example

<?php 
$link=mysql_connect('localhost','root','');
          mysql_select_db("test",$link);
          mysql_query("insert into info(id,stud_name) values(2,'shital') ");
          echo "number of affected rows ".mysql_affected_rows();
?>

Ø       mysql_fetch_array

  • Fetch a result row as an associative array, a numeric array or both.
  • Returns an array that corresponds to the fetched row, or false if there are no more row.
Syntax
array mysql_fetch_array(resource result [, int result_type])

Parameter
Description
Result
The result resource that is being evaluated. This result comes from a call to mysql_query().
Result_type
The type of array that is to be fetched.  It is a constant and can take the following value. MYSQL_ASSOC, MYSQL_NUM, and the default value is MYSQL_BOTH,

Example – MYSQL_BOTH

<?php 
$link=mysql_connect('localhost','root','');
          mysql_select_db("test",$link);
          $rs=mysql_query("select * from info");
          while($row=mysql_fetch_array($rs,MYSQL_BOTH))
          {
                   echo $row["id"]." ";
                   echo $row["stud_name"]."<br/>";
          }
?>

Example – MYSQL_NUM

<?php 
$link=mysql_connect('localhost','root','');
          mysql_select_db("test",$link);
          $rs=mysql_query("select * from info");
          while($row=mysql_fetch_array($rs,MYSQL_NUM))
          {
                   echo $row[0]." ";
                   echo $row[1]."<br/>";
          }
?>

Example – MYSQL_ASSOC

<?php 
$link=mysql_connect('localhost','root','');
          mysql_select_db("test",$link);
          $rs=mysql_query("select * from info");
          while($row=mysql_fetch_array($rs,MYSQL_ASSOC))
          {
                   echo $row["id"]." ";
                   echo $row["stud_name"]."<br/>";
          }
?>

Ø       mysql_fetch_row

  • To get one row at a time from the table this function is used. Using this function after getting the row, the column in the table are referred using the index and not the name.

Syntax

array mysql_fetch_row(resource handler)

Example

<?php 
$link=mysql_connect('localhost','root','');
          mysql_select_db("test",$link);
          $rs=mysql_query("select * from info");
          $row = mysql_fetch_row($rs);
          echo "ID : ".$row[0]."<br/>";
          echo "Name : ".$row[1];
?>

Ø       mysql_close

  • This function is used to close the connection which was opened earlier. It returns Boolean value that is either true or false.
Syntax
boolean mysql_close(resource handler)

Example

<?php 
$link=mysql_connect('localhost','root','');
          mysql_close($link);
?>

Ø       mysql_error

  • This function return the error message from the previous MYSQL process. If there is any error, then it display string value otherwise display empty string.

Syntax

string mysql_error(resource handler)

Example
<?php 
$link=mysql_connect('localhost','abc’,'');
          echo  mysql_error($link);
?>

Ø       mysql_field_type

  • This function will return the name of the data type of the field which is specified. It returns string value.

Syntax

string mysql_field_type(resource handler,int index)

Example

<?php 
$link=mysql_connect('localhost','root','');
          mysql_select_db("test",$link);
          $rs=mysql_query("select * from info");
          echo mysql_field_type($rs,0);
?>

Ø       mysql_field_len

  • This function will return the size of the data type of the field which is specified. It return integer value.

Syntax

integer mysql_field_len(resource handler,int index)

Example

<?php 
$link=mysql_connect('localhost','root','');
          mysql_select_db("test",$link);
          $rs=mysql_query("select * from info");
          echo mysql_field_len($rs,0);
?>

Ø       mysql_list_dbs

  • It will list all the database names created in MYSQL server.

Syntax

resource mysql_list_dbs(resource handler)

Example

<?php 
$link=mysql_connect('localhost','root','');
          $rs= mysql_list_dbs($link);
          $row = mysql_fetch_row($rs);
          echo $row[0];
?>

Ø       mysql_list_tables

  • This function lists out all the tables in the given MYSQL database.

Syntax

resource mysql_list_dbs(resource handler)

Example

<?php 
$link=mysql_connect('localhost','root','');
          $rs= mysql_list_tables("test",$link);
          $n = mysql_fetch_row($rs);
          echo $n[0];
?>

Ø       mysql_free_result

  • It is used to free the memory of the result. The return type is Boolean. If the  memory is free successfully, then it returns true else return false.

Syntax

boolean mysql_free_result(resource handler)

Example

<?php 
$link=mysql_connect('localhost','root','');
          mysql_select_db("test",$link);
          $rs=mysql_query("select * from info");
          Mysql_free_result($rs);
?>

 

          Posted By : Hemangi Zala

No comments:

Post a Comment