查询未在php中运行

hm2xizp9  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(324)

嗨,我正在尝试用php创建一个屏幕。在其中,我选择场景并相应地显示屏幕。但我陷入了一个简单的问题,即我的简单select查询不起作用

$deptQuery = "Select * from mcb_department";
echo mysql_real_escape_string($deptQuery);
mysql_query($deptQuery) or die("adfasdf");

在相同的代码中,如果更改表名,它就可以正常工作,而且这个表也是用我共享的相同名称在db中创建的。这是我的完整代码。

<?php       
    include "include/conn.php";
    include "include/session.php";

    if(isset($_SESSION['logged_user']) && $_SESSION['logged_user'] != '99999'){
        header('location: login.php');
    }

    $query = mysql_query("select curdate() as todayDate");  
    $show = mysql_fetch_array($query);  
    if(isset($show)){                                 
        $todayDate=  $show['todayDate'];
    }

    $group[] = array();
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta Content="no-cache, no-store, must-revalidate" http-Equiv="Cache-Control" />
<meta Content="no-cache" http-Equiv="Pragma" />
<meta Content="0" http-Equiv="Expires" />

    <link href="styles/style.css" type="text/css" rel="stylesheet" />
    <link href="styles/popupstyle.css" type="text/css" rel="stylesheet" />   
    <link href="styles/ts.css" type="text/css" rel="stylesheet" />
    <link href="styles/calendar.css" type="text/css" rel="stylesheet" />         
      <style>
table {
    font-family: arial;
    border-collapse: collapse;
    width: 100%;
    font-size: 11px;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 3px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
</style>
</head> 
<body >

</body>

    <select id='select_opt'>
        <option> Select Assigment </option>
        <option value="1"> assign quiz to all Employees </option>
        <option value="2"> assign quiz to Sapcific Group </option>
        <option value="3"> assign quiz to Sapcific Department </option>
        <option value="4"> assign quiz to Sapcific Employee </option>
    </select>

    <!-- all Users -->

    <div id='allUsers' style='margin-left:20px; margin-top: 20px;  width: 50%; height:100px; display: none;' >
        <form action="" mathod="post">
        <select>
            <option value=""> select Quiz</option>
        </select>
        <input type="submit"  >
        </form>
    </div>

    <!-- group -->

    <div id='group' style='margin-left:20px; margin-top: 20px;  width: 50%; height:100px; display: none;' >

        <form action='group_assigment.php' mathod="post">
        <table>
        <tr>
          <th>All <input type="checkbox"> </th>
          <th>Group Name</th>
          <th>Group Code</th>
        </tr>
         <?php  
               $group[] = array();
               $groupQuery = "Select * from mcb_groups";
               $query = mysql_query($groupQuery);
            ?>
        <tr>
            <?php  if($query){
                   while($group = mysql_fetch_array($query)){
                ?>
         <td><input type="checkbox" value="<?php echo $group['group_name']; ?>"></td>
          <td><?php echo $group['group_name']; ?></td>
          <td><?php echo $group['group_code']; ?></td>
        </tr>
            <?php }

                   }  else{ echo "";} ?>
        </table>

            </form>

    </div>

    <!-- 
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    department 
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    -->
 <div id='Department' style='margin-left:20px; margin-top: 20px;  width: 50%; height:100px; display: none;' >

   <form action='group_assigment.php' mathod="post">

       <table>
           <tr>
             <th>all <input type="checkbox"> </th>
             <th>name</th>
             <th>code</th>
             <th>group</th>
           </tr>
           <tr>
               <?php
                    $deptQuery = "Select * from mcb_department";
                    echo mysql_real_escape_string($deptQuery);
                    mysql_query($deptQuery);
               ?>
               <td><input type="checkbox"></td>
               <td>code</td>
               <td>name</td>
               <td>group</td>
           </tr>

         </table>
        <input type="submit" >
        </form>
    </div>

    <!-- 
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
        Employee 
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    -->    

        <div id='employee' style='margin-left:20px; margin-top: 20px;  width: 50%; height:100px; display: none;' >
         <form action="" mathod="post">
            <label>employee id : </label><input type="text" >
            <input type="submit" >
         </form>
    </div>

<script language="javascript" type="text/javascript">
 var elem = document.getElementById("select_opt");
            elem.onchange = function(){

                            console.log("yes i am running");
                            if( document.getElementById("select_opt").value == "1" ){

                                document.getElementById("allUsers").style.display = "Block";
                                document.getElementById("group").style.display = "none";
                                document.getElementById("Department").style.display = "none";
                                document.getElementById("employee").style.display = "none";

                            }
                            else if( document.getElementById("select_opt").value == "2" ){

                                document.getElementById("group").style.display = "Block";
                                document.getElementById("allUsers").style.display = "none";
                                document.getElementById("Department").style.display = "none";
                                document.getElementById("employee").style.display = "none";

                            }
                            else if( document.getElementById("select_opt").value == "3" ){

                                document.getElementById("Department").style.display = "block";
                                document.getElementById("group").style.display = "none";
                                document.getElementById("allUsers").style.display = "none";
                                document.getElementById("employee").style.display = "none";

                            }
                            else if( document.getElementById("select_opt").value == "4" ){

                                document.getElementById("employee").style.display = "block";
                                document.getElementById("Department").style.display = "none";
                                document.getElementById("group").style.display = "none";
                                document.getElementById("allUsers").style.display = "none";
                            }

                            else{

                            }

            };
 </script> 

</

html>
你好,沙菲·扬

j2qf4p5b

j2qf4p5b1#

在相同的代码中,如果更改表名,它就可以正常工作
然后我会确保你连接到正确的数据库。对于开发人员来说,在不同的mysql示例上或者在同一示例上以不同的模式名拥有多个版本的数据库是非常常见的。然后他们就搞混了,用mysql workbench连接到一个数据库,而他们的应用程序连接到另一个数据库。
我建议您暂时将查询添加到要运行的页面 SHOW TABLES 然后将查询结果转储到日志中,以确认 mcb_department 表存在于php脚本所连接的数据库中。

$deptQuery = "Select * from mcb_department";
echo mysql_real_escape_string($deptQuery);
mysql_query($deptQuery);

你的错误检查在哪里?您需要检查 mysql_query() 每次运行查询时,如果有问题,就将错误消息输出到日志中。只有这样你才能开始解决其中的一些问题。

$result = mysql_query($deptQuery);
if (!$result) {
    trigger_error("Error in file " . __FILE__ . " near line " . __LINE__ 
      . " for query $deptQuery: " . mysql_error());
    die("Database error");
}

附言:一些评论者的建议是 mysql_* 函数已弃用是正确的,但可能与您的问题无关。当你说api在工作时,那些关注api的人只是在迂腐。

相关问题