douqiu0796 2014-02-23 21:54
浏览 43
已采纳

从MySQL Query获取而不是表

Using phpmyadmin (MySQL)

I used the following code to generate a query table:

SELECT
 s.WO_NUM,
 s.WO_LINE,
 s.DESCRIPTION,
 s.CUS_LOC_NAME,
 s.MFG_LINE,
 u1.username,
 u1.lastname AS MECHANICAL,
 u2.lastname AS APPLICATIONS,
 u3.lastname AS DESIGN
FROM production_schedule s, users u1, users u2, users u3

WHERE s.PM_MECHANICAL = u1.USERID
AND s.PM_APPLICATIONS = u2.USERID
AND s.PM_DESIGN = u3.USERID

The query table came out exactly how I wanted it to. At the bottom where it says "Bookmark this SQL Query" I gave it a label 'eng_schedule' and checked the box that says "Let every user access this bookmark". Then I clicked "Bookmark this Query"

I was under the assumption that I can now treat this query like a table (correct me if I'm wrong)

So in my php file I did this:

<table class='tablesorter tablesorter-jobs'>
    <thead>
    <tr>
      <th>JOB NUMBER</th>
      <th>MODEL</th>
      <th>LINE</th>
      <th>CUSTOMER</th>
      <th>AE</th>
      <th>PE</th>
      <th>DE</th>
      <th>CE</th>
    </tr>
    </thead>
    <tbody>
    <?php
        require_once ('../config.php');
        $user_name = $_SESSION['user_name'];
        $db = null;
        $limit = 10;
        $counter = 0;
            while (true) {
            try {
                $db = new PDO($dsn, $uname, $pword);
                $db->exec( "SET CHARACTER SET utf8" );
                $db->setAttribute( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC ); 
                $db->setAttribute( PDO::ATTR_PERSISTENT, true );
                break;
            }
                catch (Exception $e) {
                    $db = null;
                    $counter++;
                    if ($counter == $limit)
                        throw $e;
                }
            }

        //$current_date = new DateTime();           
        $result = $db->prepare("SELECT * FROM eng_schedule");
        $result->execute();
        while ($row = $result->fetch(PDO::FETCH_ASSOC)){
            $wonum=$row["WO_NUM"];
            $x=$row["WO_LINE"];
            $model=$row["DESCRIPTION"];
            $a=$row["MFG_LINE"];
            $customer=$row["CUS_LOC_NAME"];
            $proj_eng=$row["PM_MECHANICAL"];
            $app_eng= $row["PM_APPLICATIONS"];
            $design_eng=$row["PM_DESIGN"];
            $controls_eng=$row["PM_PROGRAM"];

            $olddate = DateTime::createFromFormat('m/d/y', $date_initiated);    // for example, see manual for formats
            $today = new DateTime();
            $diff = $today->diff($olddate);
            $age = $diff->format('%a');

            $x = ltrim($x, '0');

            if (strlen($x) > 1) {
                $woline = $x;
            } else {
                $woline = "-0".$x;
            }
            $jobnum = $wonum ."". $woline;
            if ($a == 1) {
                $prod = "A"; 
            } else if ($a == 2) {
                $prod = "B";
            } else if ($a == 3) {
                $prod = "C"; 
            } else {
                $prod = "N/A"; 
            }
            $jobnum = $wonum . "" . $woline;

            echo "<tr><td>".$jobnum."</td>";
            echo "<td>".$model."</td>";
            echo "<td>".$prod."</td>";
            echo "<td>".$customer."</td>";
            echo "<td>".$app_eng."</td>";
            echo "<td>".$proj_eng."</td>";
            echo "<td>".$design_eng."</td>";
            echo "<td>".$controls_eng."</td></tr>";
            }
            ?>
        </tbody>
  </table>

But I'm getting NOTHING in the tbody tags. I checked the code and tried to use the master table 'production_schedule' and it worked great. But I can't query 'eng_schedule'.

Am I missing something here? Why can't I fetch from the query?

  • 写回答

3条回答 默认 最新

  • dpvm7231 2014-02-23 21:57
    关注

    Bookmarking a query helps you to reuse a query in the administrator tool, but it won't let you use that alias from other applications that use the database.

    The think you are looking for, is a view. See: http://dev.mysql.com/doc/refman/5.0/en/create-view.html

    The syntax would be:

    CREATE VIEW eng_schedule AS
    SELECT
     s.WO_NUM,
     s.WO_LINE,
     s.DESCRIPTION,
     s.CUS_LOC_NAME,
     s.MFG_LINE,
     u1.username,
     u1.lastname AS MECHANICAL,
     u2.lastname AS APPLICATIONS,
     u3.lastname AS DESIGN
    FROM production_schedule s, users u1, users u2, users u3
    
    WHERE s.PM_MECHANICAL = u1.USERID
    AND s.PM_APPLICATIONS = u2.USERID
    AND s.PM_DESIGN = u3.USERID
    

    After that, you can use the name of the view as a table name in select queries.

    B.t.w. if executing a query in PDO fails, you might want to inspect the error information that PDO returns. See: http://nl3.php.net/pdo.errorinfo In this case, you would probably see an error about a missing table, confirming that bookmarking the query indeed didn't work. The error information is useful for yourself and for people helping you, because you will get an actual error message, so you can quickly rule out silly typing errors in the query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测