Link to home
Start Free TrialLog in
Avatar of Akhil Vinay Mandava
Akhil Vinay MandavaFlag for India

asked on

How to fetch single row ?

I want to fetch the data but while fetching the data i'm getting the whole data but i want only single employee details.
for example if employee id's are GI1006,Gi1007,GI1008 are in my database but i want only GI1008 so how can i chnage my code ??

<?php
 $dbhost = "localhost";
 $dbuser = "root";
 $dbpass = "Gemini@123";
 $db = "employee database";

    $conn = mysql_connect($dbhost, $dbuser, $dbpass,$db);

   if(! $conn ) {
      die('Could not connect: ' . mysql_error());
   }

   $sql = 'SELECT EmployeeID,FirstName,MiddleName,LastName,FatherName,SpouseName,MotherName,DOB,Age,BloodGroup,MaritalStatus,Gender,ContactNumber,EmergencyContactNumber,Email,DateOfJoiningatGemini,TotalYearOfExperience,ReleventYearOfExperience,PANnumber,PassportNumber,UANnumber,AadharNumber,PresentAddress,PermanentAddress
      FROM employee_inforamtion';

   mysql_select_db('employee database');
   $retval = mysql_query( $sql, $conn );
   if(! $retval ) {
      die('Could not get data: ' . mysql_error());
   }

   while($row = mysql_fetch_assoc($retval, MYSQL_ASSOC)){

   echo "<tr>";
   
   
      echo " <tr>";
      echo  "Employee ID :{$row['EmployeeID']}</br></br> ";
      echo "<tr>";
      echo  "First Name :{$row['FirstName']}</br></br> ";
      echo "<tr>";    
      echo  "Middle Name :{$row['MiddleName']}</br></br>";
      echo " <tr>";
      echo  "Last Name :{$row['LastName']}</br></br>";
      echo " <tr>";
      echo     "Father Name :{$row['FatherName']}</br></br>";
      echo " <tr>";
      echo     "Spouse Name :{$row['SpouseName']}</br></br> ";
      echo " <tr>";
      echo     "Mother Name :{$row['MotherName']}</br></br> ";
      echo " <tr>";
      echo     "Date of Birth :{$row['DOB']}</br></br> ";
      echo " <tr>";
      echo     "Age :{$row['Age']}</br></br> ";
      echo " <tr>";
      echo     "Blood Group :{$row['BloodGroup']}</br></br> ";
      echo " <tr>";
      echo    "Marital Status :{$row['MaritalStatus']}</br></br> ";
      echo " <tr>";
      echo     "Gender :{$row['Gender']}</br></br> ";
      echo " <tr>";
      echo     "Contact Number :{$row['ContactNumber']}</br></br> ";
      echo " <tr>";
      echo    "Emergency Contact Number :{$row['EmergencyContactNumber']}</br></br> ";
      echo " <tr>";
      echo     "Email :{$row['Email']}</br></br> ";
      echo " <tr>";
      echo     "Date Of Joining in Gemini :{$row['DateOfJoiningatGemini']}</br></br> ";
      echo " <tr>";
      echo     "Total Years of Experience :{$row['TotalYearOfExperience']}</br></br> ";
      echo " <tr>";
      echo    "Relevent Years of Experience :{$row['ReleventYearOfExperience']}</br></br> ";
      echo " <tr>";
      echo     "PAN number :{$row['PANnumber']}</br></br> ";
      echo " <tr>";
      echo     "Passport Number :{$row['PassportNumber']}</br></br> ";
      echo " <tr>";
      echo    "UAN number :{$row['UANnumber']}</br></br> ";
      echo " <tr>";
      echo     "Aadhar Number :{$row['AadharNumber']} </br></br>";
      echo " <tr>";
      echo    "Present Address :{$row['PresentAddress']}</br></br> ";
      echo " <tr>";
      echo    "Permanent Address :{$row['PermanentAddress']}</br></br> ";
  echo "</tr>";
   
 }  
      $qry='SELECT EmployeeID,Qualification,YearOfPassing,University,Percentage FROM employee_personal_details' ;

      mysql_select_db('employee database');
   $retval = mysql_query( $qry, $conn );
   if(! $retval ) {
      die('Could not get data: ' . mysql_error());
   } 
   
   echo "<table border='5px solid block', width='50%' padding='12px 20px' >";

     while($row = mysql_fetch_assoc($retval, MYSQL_ASSOC))  { 
      ?>
      <tr>
            <td><?php echo $row['Qualification']; ?></td> 
            <td><?php echo $row['YearOfPassing']; ?></td> 
            <td><?php echo $row['University']; ?></td>
            <td><?php echo $row['Percentage']; ?></td> 
      </tr>

        <?php 
        }
         echo "</table></br></br>";  

   $qry='SELECT Organization,Fro,Till FROM employee_personal_details' ;

      mysql_select_db('employee database');
   $retval = mysql_query( $qry, $conn );
   if(! $retval ) {
      die('Could not get data: ' . mysql_error());
   } 
   
   echo "<table border='5px solid block', width='50%' padding='12px 20px' >";

     while($row = mysql_fetch_assoc($retval, MYSQL_ASSOC))  { 
      ?>
      <tr>
            <td><?php echo $row['Organization']; ?></td> 
            <td><?php echo $row['Fro']; ?></td> 
            <td><?php echo $row['Till']; ?></td> 
      </tr> 
        
        <?php 
        }
         echo "</table></br></br>"; 


    $qry='SELECT Name,ag,Relationship,Occupation FROM employee_personal_details' ;

      mysql_select_db('employee database');
   $retval = mysql_query( $qry, $conn );
   if(! $retval ) {
      die('Could not get data: ' . mysql_error());
   } 
   
   echo "<table border='5px solid block', width='50%' padding='12px 20px' >";

     while($row = mysql_fetch_assoc($retval, MYSQL_ASSOC))  { 
      ?>
      <tr>
            <td><?php echo $row['Name']; ?></td> 
            <td><?php echo $row['ag']; ?></td> 
            <td><?php echo $row['Relationship']; ?></td>
            <td><?php echo $row['Occupation']; ?></td> 
      </tr>
      
        
        <?php 
        }
         echo "</table>";               
   echo "Fetched data successfully";
   mysql_close($conn);
?>

Open in new window

Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

but i want only GI1008 so how can i chnage my code ??

the principle is to select only the data needed.

to limit only 1 record selected, you can use Limit in your select SQL clause.

PHP Limit Data Selections From MySQL
https://www.w3schools.com/php/php_mysql_select_limit.asp
ASKER CERTIFIED SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Akhil Vinay Mandava

ASKER

i got like this

Parse error: syntax error, unexpected 'GI1008' (T_STRING) in C:\xampp\htdocs\login\fetch.php on line 14
$sql = 'SELECT EmployeeID,FirstName,MiddleName,LastName,FatherName,SpouseName,MotherName,DOB,Age,BloodGroup,MaritalStatus,Gender,ContactNumber,EmergencyContactNumber,Email,DateOfJoiningatGemini,TotalYearOfExperience,ReleventYearOfExperience,PANnumber,PassportNumber,UANnumber,AadharNumber,PresentAddress,PermanentAddress
      FROM employee_inforamtion WHERE EmployeeID = 'GI1008'';

Open in new window

You have to embed the single quotes into the SQL string

I don't think you can do that with what you tried, there are a billion examples of how to do this

but I haven't used any PHP in ... well so long I cannot even remember
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also, as you don't always want employee 'GI1008', you would put in a variable, and that will make the quotes within quotes problem obsolete anyway.

First off, your code is legacy and would need to be ported to use mysqli or PDO, it's also only possible to really parameterize queries in these modules, not with the native and obsolete PHP mysql functions you use.

That means you have to change your code more drastically, than just replacing every mysql with mysqli.

You can somewhat remain in the procedural style code, but mysqli_query() as replacement of mysdql_query() will not allow you to prepare a statement, which is necessary to parameterize it in an ideal way, not just by putting in variable values within the query string.

Bye, Olaf.