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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化