douben7493 2016-06-09 10:24
浏览 8
已采纳

在mysql中获取查询

Actually, I want to fetch data from my table according to the below condition, table contains 5 column, name, user_id, role, reporting_manager, password. I wrote one Html page for giving role.

  <html>
<head>
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title><!--Dquip--></title>
<link href="http://fonts.googleapis.com/css?family=Abel|Arvo" rel="stylesheet" type="text/css" />
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.dropotron-1.0.js"></script>
<script type="text/javascript" src="jquery.slidertron-1.0.js"></script>
<style>
label
{
    display:inline-block;
    width:150px;
    margin-right:10px;
    text-align:;
}
table,tr,th,td
{
  border: 2px solid dodgerblue;
  border-collapse: separate;
}
table
{
  width:75%;
  margin-top: 8%;
}
th,td
{
  height: 50px;
}
td
{
  text-align:center;
  vertical-align: middle;
}
.button
{
    width:75px;
    height: 25px;
    background-color:dodgerblue;
    color:white;
    border:1px solid transparent;
}
</style>
<center>


</head>
<body>
<h4 align="right"><a href="loginpage.php">Logout</a></h4>
<body>
<div id="wrapper">
    <div id="header-wrapper">
        <div id="header">
            <div id="logo">
                <h1><a href="#">Dquip..</a></h1>

            </div>
        </div>
        <div id="menu-wrapper">
            <ul id="menu">
                <li class="current_page_item"><a href="calenderindex.html"><span>Homepage</span></a></li>
                <li><span>Blog</span>
                    <ul>
                        <li class="first"> <a href="index">About US</a> </li>
                        <li> <a href="search">Function Area</a> </li>
                        <li class="last"> <a href="about">Contact US</a> </li>
                    </ul>
                </li>
                <li><a href="#"><span>Photos</span></a></li>
                <li><a href="#"><span>About</span></a></li>
                <li><span>Links</span>
                    <ul>
                        <li class="first"> <a href="index.php">Add Details</a> </li>
                        <li> <a href="map.php">Map view</a> </li>
                        <li class="last"> <a href="fetchinghome.php">view Details</a> </li>
                    </ul>
                </li>
                <li><a href="#"><span>Contact</span></a></li>
            </ul>
            <script type="text/javascript">
            $('#menu').dropotron();
        </script> 
        </div></br>
        <h1><font color="white">Enter the dates to retrieve the data</font></h1></br></br>
<form method="POST" action="fetchinghome.php">
<label>Your user ID       :</label><input type="text" name="role" placeholder="Enter the starting date"> 

<input type="submit" id="submit" name="submit" value="Go" class="button">
</form>
</body>
</html>

<?php
//include "loginpage.php";
if ($_SERVER["REQUEST_METHOD"] == "POST") 
{
    if(isset($_POST['submit']))
    {
   $servername = "localhost";
   $username = "root";
   $password = "";
   $dbname = "calender";
   $role=$_REQUEST['role'];


   $conn = mysqli_connect($servername, $username, $password, $dbname);



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

       $sql="SELECT * FROM registration where reporting_manager='$role'";

   $retval = mysqli_query( $conn,$sql );

   if(! $retval )
   {
      die('Could not get data: ' . mysqli_error($conn));
   }



       echo "<table id='example' class='display' cellspacing='0' width='100%'>
          <tr>

            <th>Name</th>
            <th>Email</th>
            <th>Mobile Number</th>
            <th>Address</th>
            <th>Role</th>
            <th>Action</th>
            </tr>";

   while($row = mysqli_fetch_array($retval))
    {
        echo "<tr><td>";
        echo $row[0]."</td><td>";
        echo $row[1]."</td><td>";
        echo $row[2]."</td><td>";
        echo $row[3]."</td><td>";
        echo $row[4]."</td>";

        echo "</tr>";
    }
    echo "</table>";


   }

   echo "Fetched data successfully
";

   mysqli_close($conn);
}   

?>

if i give role as admin, need to show all the data from the database. if i give role as manager1, then show the data of users whose reporting_manager is manger1 plus that manager1 person's details. and if i gave role as user1 fetch only that user details. is that possible to write this query in mysql using if else. if any one have any idea about this please tell me.

  • 写回答

1条回答 默认 最新

  • dongzong7467 2016-06-09 10:52
    关注

    You could set the $sql variable inside if conditions, such as

    if ($role == 'admin')
    {
        $sql="SELECT * FROM registration";
    }
    else
    {
        $sql="SELECT * FROM registration where reporting_manager='$role'";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿