connect mysql using php function

Connect with my sql using php function and get data using function from mysql, mysql select query retrieve data using php function with select query and how to include in php with how to create function in php

STEP 1. create table in mysql  for retrieve data





 1. Create a Data.php file 



    <?php 

      function DB_Mysql() {

          $dbhost = 'localhost';
          $dbuser = 'root';
          $dbpass = 'root';
          $conn = mysql_connect($dbhost, $dbuser, $dbpass);
          if(! $conn ) {
              die('Could not connect: ' . mysql_error());
         }
          echo "Connected successfully";
return $conn;
     }

     function getData($conn,$tableName,$db) {

         $sql = "SELECT * FROM $tableName"; 
             mysql_select_db(''.$db.'');
         $retval = mysql_query( $sql, $conn );

        return $retval;
         }


2. Create employee.php

  
      
    <?php

       include_once('data.php');

      $conn=DB_Mysql();

      $tableName="employee";
      $db="erp";

      //get employee details using getData function
      $retval=getData($conn,$tableName,$db);

      if(! $retval ) {
          die('Could not get data: ' . mysql_error());
      }
   
      while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "<br> No :{$row['id']}   ";
echo "Name :{$row['name']}   ";
echo "Email :{$row['email']}  <br> ";

}

      ?>

    

No comments:

Post a Comment